You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

322 lines
5.4 KiB

  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package bitset
  4. import (
  5. rand "math/rand"
  6. "testing"
  7. )
  8. func TestNewBitset(t *testing.T) {
  9. tests := [][]bool{
  10. {},
  11. {b1},
  12. {b0},
  13. {b1, b0},
  14. {b1, b0, b1},
  15. {b0, b0, b1},
  16. }
  17. for _, v := range tests {
  18. result := New(v...)
  19. if !equal(result.Bits(), v) {
  20. t.Errorf("%s", result.String())
  21. t.Errorf("%v => %v, want %v", v, result.Bits(), v)
  22. }
  23. }
  24. }
  25. func TestAppend(t *testing.T) {
  26. randomBools := make([]bool, 128)
  27. rng := rand.New(rand.NewSource(1))
  28. for i := 0; i < len(randomBools); i++ {
  29. randomBools[i] = rng.Intn(2) == 1
  30. }
  31. for i := 0; i < len(randomBools)-1; i++ {
  32. a := New(randomBools[0:i]...)
  33. b := New(randomBools[i:]...)
  34. a.Append(b)
  35. if !equal(a.Bits(), randomBools) {
  36. t.Errorf("got %v, want %v", a.Bits(), randomBools)
  37. }
  38. }
  39. }
  40. func TestAppendByte(t *testing.T) {
  41. tests := []struct {
  42. initial *Bitset
  43. value byte
  44. numBits int
  45. expected *Bitset
  46. }{
  47. {
  48. New(),
  49. 0x01,
  50. 1,
  51. New(b1),
  52. },
  53. {
  54. New(b1),
  55. 0x01,
  56. 1,
  57. New(b1, b1),
  58. },
  59. {
  60. New(b0),
  61. 0x01,
  62. 1,
  63. New(b0, b1),
  64. },
  65. {
  66. New(b1, b0, b1, b0, b1, b0, b1),
  67. 0xAA, // 0b10101010
  68. 2,
  69. New(b1, b0, b1, b0, b1, b0, b1, b1, b0),
  70. },
  71. {
  72. New(b1, b0, b1, b0, b1, b0, b1),
  73. 0xAA, // 0b10101010
  74. 8,
  75. New(b1, b0, b1, b0, b1, b0, b1, b1, b0, b1, b0, b1, b0, b1, b0),
  76. },
  77. }
  78. for _, test := range tests {
  79. test.initial.AppendByte(test.value, test.numBits)
  80. if !equal(test.initial.Bits(), test.expected.Bits()) {
  81. t.Errorf("Got %v, expected %v", test.initial.Bits(),
  82. test.expected.Bits())
  83. }
  84. }
  85. }
  86. func TestAppendUint32(t *testing.T) {
  87. tests := []struct {
  88. initial *Bitset
  89. value uint32
  90. numBits int
  91. expected *Bitset
  92. }{
  93. {
  94. New(),
  95. 0xAAAAAAAF,
  96. 4,
  97. New(b1, b1, b1, b1),
  98. },
  99. {
  100. New(),
  101. 0xFFFFFFFF,
  102. 32,
  103. New(b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1,
  104. b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1, b1),
  105. },
  106. {
  107. New(),
  108. 0x0,
  109. 32,
  110. New(b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0,
  111. b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0),
  112. },
  113. {
  114. New(),
  115. 0xAAAAAAAA,
  116. 32,
  117. New(b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1,
  118. b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0),
  119. },
  120. {
  121. New(),
  122. 0xAAAAAAAA,
  123. 31,
  124. New(b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1,
  125. b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0, b1, b0),
  126. },
  127. }
  128. for _, test := range tests {
  129. test.initial.AppendUint32(test.value, test.numBits)
  130. if !equal(test.initial.Bits(), test.expected.Bits()) {
  131. t.Errorf("Got %v, expected %v", test.initial.Bits(),
  132. test.expected.Bits())
  133. }
  134. }
  135. }
  136. func TestAppendBools(t *testing.T) {
  137. randomBools := make([]bool, 128)
  138. rng := rand.New(rand.NewSource(1))
  139. for i := 0; i < len(randomBools); i++ {
  140. randomBools[i] = rng.Intn(2) == 1
  141. }
  142. for i := 0; i < len(randomBools)-1; i++ {
  143. result := New(randomBools[0:i]...)
  144. result.AppendBools(randomBools[i:]...)
  145. if !equal(result.Bits(), randomBools) {
  146. t.Errorf("got %v, want %v", result.Bits(), randomBools)
  147. }
  148. }
  149. }
  150. func BenchmarkShortAppend(b *testing.B) {
  151. bitset := New()
  152. for i := 0; i < b.N; i++ {
  153. bitset.AppendBools(b0, b1, b0, b1, b0, b1, b0)
  154. }
  155. }
  156. func TestLen(t *testing.T) {
  157. randomBools := make([]bool, 128)
  158. rng := rand.New(rand.NewSource(1))
  159. for i := 0; i < len(randomBools); i++ {
  160. randomBools[i] = rng.Intn(2) == 1
  161. }
  162. for i := 0; i < len(randomBools)-1; i++ {
  163. result := New(randomBools[0:i]...)
  164. if result.Len() != i {
  165. t.Errorf("Len = %d, want %d", result.Len(), i)
  166. }
  167. }
  168. }
  169. func TestAt(t *testing.T) {
  170. test := []bool{b0, b1, b0, b1, b0, b1, b1, b0, b1}
  171. bitset := New(test...)
  172. for i, v := range test {
  173. result := bitset.At(i)
  174. if result != test[i] {
  175. t.Errorf("bitset[%d] => %t, want %t", i, result, v)
  176. }
  177. }
  178. }
  179. func equal(a []bool, b []bool) bool {
  180. if len(a) != len(b) {
  181. return false
  182. }
  183. for i := 0; i < len(a); i++ {
  184. if a[i] != b[i] {
  185. return false
  186. }
  187. }
  188. return true
  189. }
  190. func TestExample(t *testing.T) {
  191. b := New() // {}
  192. b.AppendBools(true, true, false) // {1, 1, 0}
  193. b.AppendBools(true) // {1, 1, 0, 1}
  194. b.AppendByte(0x02, 4) // {1, 1, 0, 1, 0, 0, 1, 0}
  195. expected := []bool{b1, b1, b0, b1, b0, b0, b1, b0}
  196. if !equal(b.Bits(), expected) {
  197. t.Errorf("Got %v, expected %v", b.Bits(), expected)
  198. }
  199. }
  200. func TestByteAt(t *testing.T) {
  201. data := []bool{b0, b1, b0, b1, b0, b1, b1, b0, b1}
  202. tests := []struct {
  203. index int
  204. expected byte
  205. }{
  206. {
  207. 0,
  208. 0x56,
  209. },
  210. {
  211. 1,
  212. 0xad,
  213. },
  214. {
  215. 2,
  216. 0x2d,
  217. },
  218. {
  219. 5,
  220. 0x0d,
  221. },
  222. {
  223. 8,
  224. 0x01,
  225. },
  226. }
  227. for _, test := range tests {
  228. b := New()
  229. b.AppendBools(data...)
  230. result := b.ByteAt(test.index)
  231. if result != test.expected {
  232. t.Errorf("Got %#x, expected %#x", result, test.expected)
  233. }
  234. }
  235. }
  236. func TestSubstr(t *testing.T) {
  237. data := []bool{b0, b1, b0, b1, b0, b1, b1, b0}
  238. tests := []struct {
  239. start int
  240. end int
  241. expected []bool
  242. }{
  243. {
  244. 0,
  245. 8,
  246. []bool{b0, b1, b0, b1, b0, b1, b1, b0},
  247. },
  248. {
  249. 0,
  250. 0,
  251. []bool{},
  252. },
  253. {
  254. 0,
  255. 1,
  256. []bool{b0},
  257. },
  258. {
  259. 2,
  260. 4,
  261. []bool{b0, b1},
  262. },
  263. }
  264. for _, test := range tests {
  265. b := New()
  266. b.AppendBools(data...)
  267. result := b.Substr(test.start, test.end)
  268. expected := New()
  269. expected.AppendBools(test.expected...)
  270. if !result.Equals(expected) {
  271. t.Errorf("Got %s, expected %s", result.String(), expected.String())
  272. }
  273. }
  274. }