packedlist_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package bitutil
  10. import (
  11. "fmt"
  12. "math"
  13. "testing"
  14. )
  15. func TestListPacking(t *testing.T) {
  16. mylist := make([]uint64, 7)
  17. mylist[0] = 3
  18. mylist[1] = 7
  19. mylist[2] = 63
  20. mylist[3] = math.MaxUint8
  21. mylist[4] = math.MaxUint16
  22. mylist[5] = math.MaxUint32
  23. mylist[6] = math.MaxUint64
  24. res := UnpackList(PackList(mylist, 3))
  25. if res[0] != 3 {
  26. t.Error("Unexpected result:", res)
  27. return
  28. }
  29. res = UnpackList(PackList(mylist, 7))
  30. if fmt.Sprint(res[:2]) != "[3 7]" {
  31. t.Error("Unexpected result:", res[:2])
  32. return
  33. }
  34. res = UnpackList(PackList(mylist, 63))
  35. if fmt.Sprint(res[:3]) != "[3 7 63]" {
  36. t.Error("Unexpected result:", res[:3])
  37. return
  38. }
  39. res = UnpackList(PackList(mylist, math.MaxUint8))
  40. if fmt.Sprint(res[:4]) != "[3 7 63 255]" {
  41. t.Error("Unexpected result:", res[:4])
  42. return
  43. }
  44. res = UnpackList(PackList(mylist, math.MaxUint16))
  45. if fmt.Sprint(res[:5]) != "[3 7 63 255 65535]" {
  46. t.Error("Unexpected result:", res[:5])
  47. return
  48. }
  49. res = UnpackList(PackList(mylist, math.MaxUint32))
  50. if fmt.Sprint(res[:6]) != "[3 7 63 255 65535 4294967295]" {
  51. t.Error("Unexpected result:", res[:6])
  52. return
  53. }
  54. res = UnpackList(PackList(mylist, math.MaxUint64))
  55. if fmt.Sprint(res[:7]) != "[3 7 63 255 65535 4294967295 18446744073709551615]" {
  56. t.Error("Unexpected result:", res[:7])
  57. return
  58. }
  59. res = UnpackList(PackList([]uint64{10, 12, 80}, 80))
  60. if fmt.Sprint(res) != "[10 12 80]" {
  61. t.Error("Unexpected result:", res)
  62. return
  63. }
  64. }
  65. func TestListPacking8(t *testing.T) {
  66. list1 := PackList3Bit([]byte{1, 2, 3, 4, 5, 6, 7})
  67. list2 := PackList16Bit([]uint16{1, 2, 3, 4})
  68. if len(list1) != 4 || len(list2) != 9 {
  69. t.Error("Unexpected lengths:", len(list1), len(list2))
  70. return
  71. }
  72. res1 := UnpackList(list1)
  73. res2 := UnpackList(list2)
  74. if fmt.Sprint(res1) != "[1 2 3 4 5 6 7]" {
  75. t.Error("Unexpected result:", res1)
  76. return
  77. }
  78. if fmt.Sprint(res2) != "[1 2 3 4]" {
  79. t.Error("Unexpected result:", res2)
  80. return
  81. }
  82. if UnpackList("") != nil {
  83. t.Error("Unexpected result")
  84. return
  85. }
  86. }
  87. func TestVarBitListPacking8(t *testing.T) {
  88. scale := 3
  89. testlist := make([]uint8, scale)
  90. for i := 0; i < scale; i++ {
  91. testlist[i] = math.MaxUint8
  92. }
  93. res := PackList8Bit(testlist)
  94. if len(res) != scale+1 {
  95. t.Error("Unexpected length:", len(res))
  96. return
  97. }
  98. res2 := UnpackBigList(res)
  99. for i := 0; i < scale; i++ {
  100. if testlist[i] != uint8(res2[i]) {
  101. t.Error("Unexpected result at:", i)
  102. }
  103. }
  104. }
  105. func TestVarBitListPacking16(t *testing.T) {
  106. scale := 3
  107. testlist := make([]uint16, scale)
  108. for i := 0; i < scale; i++ {
  109. testlist[i] = math.MaxUint16
  110. }
  111. res := PackList16Bit(testlist)
  112. if len(res) != scale*2+1 {
  113. t.Error("Unexpected length:", len(res))
  114. return
  115. }
  116. res2 := UnpackBigList(res)
  117. for i := 0; i < scale; i++ {
  118. if testlist[i] != uint16(res2[i]) {
  119. t.Error("Unexpected result at:", i)
  120. }
  121. }
  122. }
  123. func TestVarBitListPacking32(t *testing.T) {
  124. scale := 3
  125. testlist := make([]uint32, scale)
  126. for i := 0; i < scale; i++ {
  127. testlist[i] = math.MaxUint32
  128. }
  129. res := PackList32Bit(testlist)
  130. if len(res) != scale*4+1 {
  131. t.Error("Unexpected length:", len(res))
  132. return
  133. }
  134. res2 := UnpackBigList(res)
  135. for i := 0; i < scale; i++ {
  136. if testlist[i] != uint32(res2[i]) {
  137. t.Error("Unexpected result at:", i)
  138. }
  139. }
  140. }
  141. func TestVarBitListPacking64(t *testing.T) {
  142. scale := 3
  143. testlist := make([]uint64, scale)
  144. for i := 0; i < scale; i++ {
  145. testlist[i] = math.MaxUint64
  146. }
  147. res := PackList64Bit(testlist)
  148. if len(res) != scale*8+1 {
  149. t.Error("Unexpected length:", len(res))
  150. return
  151. }
  152. res2 := UnpackBigList(res)
  153. for i := 0; i < scale; i++ {
  154. if testlist[i] != uint64(res2[i]) {
  155. t.Error("Unexpected result at:", i)
  156. }
  157. }
  158. }
  159. func TestSmallListPacking(t *testing.T) {
  160. // Test simple cases
  161. if PackList2Bit([]byte{}) != "" {
  162. t.Error("Unexpected result")
  163. return
  164. }
  165. if PackList3Bit([]byte{}) != "" {
  166. t.Error("Unexpected result")
  167. return
  168. }
  169. if PackList6Bit([]byte{}) != "" {
  170. t.Error("Unexpected result")
  171. return
  172. }
  173. if string(UnpackSmallList("")) != "" {
  174. t.Error("Unexpected result")
  175. return
  176. }
  177. // Simulates a gob encoded string
  178. if string(UnpackSmallList(string([]byte{0x00}))) != string([]byte{0x00}) {
  179. t.Error("Unexpected result")
  180. return
  181. }
  182. // Test normal cases
  183. checkListAndPresentation2bit(t, []byte{1, 2, 3, 1, 2, 3}, []byte{0x5b, 0x6c}, 2)
  184. checkListAndPresentation2bit(t, []byte{1}, []byte{0x50}, 1)
  185. checkListAndPresentation2bit(t, []byte{1, 2}, []byte{0x58}, 1)
  186. checkListAndPresentation2bit(t, []byte{1, 2, 3}, []byte{0x5B}, 1)
  187. checkListAndPresentation2bit(t, []byte{1, 2, 3, 3}, []byte{0x5B, 0xC0}, 2)
  188. checkListAndPresentation2bit(t, []byte{1, 2, 3, 3, 2}, []byte{0x5B, 0xE0}, 2)
  189. checkListAndPresentation2bit(t, []byte{1, 2, 3, 3, 2, 1, 3}, []byte{0x5B, 0xE7}, 2)
  190. checkListAndPresentation3bit(t, []byte{1, 2, 3, 1, 2, 3}, []byte{0x8A, 0x19, 0x13}, 3)
  191. checkListAndPresentation3bit(t, []byte{1}, []byte{0x88}, 1)
  192. checkListAndPresentation3bit(t, []byte{1, 2}, []byte{0x8A}, 1)
  193. checkListAndPresentation3bit(t, []byte{1, 2, 3}, []byte{0x8A, 0x18}, 2)
  194. checkListAndPresentation3bit(t, []byte{1, 2, 3, 3}, []byte{0x8A, 0x1B}, 2)
  195. checkListAndPresentation3bit(t, []byte{1, 2, 3, 4, 5, 6, 7}, []byte{0x8A, 0x1C, 0x2E, 0x38}, 4)
  196. checkListAndPresentation6bit(t, []byte{1, 2, 3, 1, 2, 3})
  197. checkListAndPresentation6bit(t, []byte{1})
  198. checkListAndPresentation6bit(t, []byte{1, 2})
  199. checkListAndPresentation6bit(t, []byte{1, 2, 3})
  200. checkListAndPresentation6bit(t, []byte{1, 2, 3, 3})
  201. checkListAndPresentation6bit(t, []byte{1, 2, 3, 4, 35, 45, 63})
  202. }
  203. func checkListAndPresentation2bit(t *testing.T, list []byte, packedlist []byte, packedLen int) {
  204. res := PackList2Bit(list)
  205. if res != string(packedlist) {
  206. t.Errorf("Unexpected result: %X", []byte(res))
  207. return
  208. }
  209. if len(res) != packedLen {
  210. t.Error("Unexpected size", len(res))
  211. return
  212. }
  213. if dres := UnpackSmallList(res); string(dres) != string(list) {
  214. t.Errorf("Unexpected result: %X", []byte(dres))
  215. return
  216. }
  217. }
  218. func checkListAndPresentation3bit(t *testing.T, list []byte, packedlist []byte, packedLen int) {
  219. res := PackList3Bit(list)
  220. if res != string(packedlist) {
  221. t.Errorf("Unexpected result: %X", []byte(res))
  222. return
  223. }
  224. if len(res) != packedLen {
  225. t.Error("Unexpected size", len(res))
  226. return
  227. }
  228. if dres := UnpackSmallList(res); string(dres) != string(list) {
  229. t.Errorf("Unexpected result: %X", []byte(dres))
  230. return
  231. }
  232. }
  233. func checkListAndPresentation6bit(t *testing.T, list []byte) {
  234. res := PackList6Bit(list)
  235. packedlist := make([]byte, len(list))
  236. copy(packedlist, list)
  237. packedlist[0] = packedlist[0] | 0xC0
  238. if res != string(packedlist) {
  239. t.Errorf("Unexpected result: %X vs %X", []byte(res), packedlist)
  240. return
  241. }
  242. if len(res) != len(list) {
  243. t.Error("Unexpected size", len(res))
  244. return
  245. }
  246. if dres := UnpackSmallList(res); string(dres) != string(list) {
  247. t.Errorf("Unexpected result: %X", []byte(dres))
  248. return
  249. }
  250. }
  251. func TestList2byte2bit(t *testing.T) {
  252. if res := list2byte2bit(0x01, 0x2, 0x03, 0x01); res != 0x6D {
  253. t.Errorf("Unexpected result: %X", res)
  254. return
  255. }
  256. if res := list2byte3bitAndHeader(0x00, 0x07, 0x03); res != 0x3B {
  257. t.Errorf("Unexpected result: %X", res)
  258. return
  259. }
  260. }
  261. func TestByte2list2bit(t *testing.T) {
  262. if a, b, c, d := byte2list2bit(0x30); a != 00 || b != 03 || c != 00 || d != 00 {
  263. t.Error("Unexpected result:", a, b, c, d)
  264. return
  265. }
  266. if a, b, c, d := byte2list2bit(0x80); a != 02 || b != 00 || c != 00 || d != 00 {
  267. t.Error("Unexpected result:", a, b, c, d)
  268. return
  269. }
  270. if a, b, c, d := byte2list2bit(0x01); a != 00 || b != 00 || c != 00 || d != 01 {
  271. t.Error("Unexpected result:", a, b, c, d)
  272. return
  273. }
  274. if a, b, c, d := byte2list2bit(0x31); a != 00 || b != 03 || c != 00 || d != 01 {
  275. t.Error("Unexpected result:", a, b, c, d)
  276. return
  277. }
  278. if a, b, c, d := byte2list2bit(0x05); a != 00 || b != 00 || c != 01 || d != 01 {
  279. t.Error("Unexpected result:", a, b, c, d)
  280. return
  281. }
  282. }
  283. func TestByte2list3bit(t *testing.T) {
  284. if a, b := byte2list3bit(0x01); a != 00 || b != 01 {
  285. t.Error("Unexpected result:", a, b)
  286. return
  287. }
  288. if a, b := byte2list3bit(0x31); a != 06 || b != 01 {
  289. t.Error("Unexpected result:", a, b)
  290. return
  291. }
  292. if a, b := byte2list3bit(0x05); a != 00 || b != 05 {
  293. t.Error("Unexpected result:", a, b)
  294. return
  295. }
  296. }