encoder_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package qrcode
  4. import (
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. bitset "github.com/skip2/go-qrcode/bitset"
  9. )
  10. func TestClassifyDataMode(t *testing.T) {
  11. type Test struct {
  12. }
  13. tests := []struct {
  14. data []byte
  15. actual []segment
  16. }{
  17. {
  18. []byte{0x30},
  19. []segment{
  20. {
  21. dataModeNumeric,
  22. []byte{0x30},
  23. },
  24. },
  25. },
  26. {
  27. []byte{0x30, 0x41, 0x42, 0x43, 0x20, 0x00, 0xf0, 0xf1, 0xf2, 0x31},
  28. []segment{
  29. {
  30. dataModeNumeric,
  31. []byte{0x30},
  32. },
  33. {
  34. dataModeAlphanumeric,
  35. []byte{0x41, 0x42, 0x43, 0x20},
  36. },
  37. {
  38. dataModeByte,
  39. []byte{0x00, 0xf0, 0xf1, 0xf2},
  40. },
  41. {
  42. dataModeNumeric,
  43. []byte{0x31},
  44. },
  45. },
  46. },
  47. }
  48. for _, test := range tests {
  49. encoder := newDataEncoder(dataEncoderType1To9)
  50. encoder.encode(test.data)
  51. if !reflect.DeepEqual(test.actual, encoder.actual) {
  52. t.Errorf("Got %v, expected %v", encoder.actual, test.actual)
  53. }
  54. }
  55. }
  56. func TestByteModeLengthCalculations(t *testing.T) {
  57. tests := []struct {
  58. dataEncoderType dataEncoderType
  59. dataMode dataMode
  60. numSymbols int
  61. expectedLength int
  62. }{}
  63. for i, test := range tests {
  64. encoder := newDataEncoder(test.dataEncoderType)
  65. var resultLength int
  66. resultLength, err := encoder.encodedLength(test.dataMode, test.numSymbols)
  67. if test.expectedLength == -1 {
  68. if err == nil {
  69. t.Errorf("Test %d: got length %d, expected error", i, resultLength)
  70. }
  71. } else if resultLength != test.expectedLength {
  72. t.Errorf("Test %d: got length %d, expected length %d", i, resultLength,
  73. test.expectedLength)
  74. }
  75. }
  76. }
  77. func TestSingleModeEncodings(t *testing.T) {
  78. tests := []struct {
  79. dataEncoderType dataEncoderType
  80. dataMode dataMode
  81. data string
  82. expected *bitset.Bitset
  83. }{
  84. {
  85. dataEncoderType1To9,
  86. dataModeNumeric,
  87. "01234567",
  88. bitset.NewFromBase2String("0001 0000001000 0000001100 0101011001 1000011"),
  89. },
  90. {
  91. dataEncoderType1To9,
  92. dataModeAlphanumeric,
  93. "AC-42",
  94. bitset.NewFromBase2String("0010 000000101 00111001110 11100111001 000010"),
  95. },
  96. {
  97. dataEncoderType1To9,
  98. dataModeByte,
  99. "123",
  100. bitset.NewFromBase2String("0100 00000011 00110001 00110010 00110011"),
  101. },
  102. {
  103. dataEncoderType10To26,
  104. dataModeByte,
  105. "123",
  106. bitset.NewFromBase2String("0100 00000000 00000011 00110001 00110010 00110011"),
  107. },
  108. {
  109. dataEncoderType27To40,
  110. dataModeByte,
  111. "123",
  112. bitset.NewFromBase2String("0100 00000000 00000011 00110001 00110010 00110011"),
  113. },
  114. }
  115. for _, test := range tests {
  116. encoder := newDataEncoder(test.dataEncoderType)
  117. encoded := bitset.New()
  118. encoder.encodeDataRaw([]byte(test.data), test.dataMode, encoded)
  119. if !test.expected.Equals(encoded) {
  120. t.Errorf("For %s got %s, expected %s", test.data, encoded.String(),
  121. test.expected.String())
  122. }
  123. }
  124. }
  125. type testModeSegment struct {
  126. dataMode dataMode
  127. numChars int
  128. }
  129. func TestOptimiseEncoding(t *testing.T) {
  130. tests := []struct {
  131. dataEncoderType dataEncoderType
  132. actual []testModeSegment
  133. optimised []testModeSegment
  134. }{
  135. // Coalescing multiple segments.
  136. {
  137. dataEncoderType1To9,
  138. []testModeSegment{
  139. {dataModeAlphanumeric, 1}, // length = 4 + 9 + 6 = 19 bits
  140. {dataModeNumeric, 1}, // length = 4 + 10 + 4 = 18 bits
  141. {dataModeAlphanumeric, 1}, // 19 bits.
  142. {dataModeNumeric, 1}, // 18 bits.
  143. {dataModeAlphanumeric, 1}, // 19 bits.
  144. // total = 93 bits.
  145. },
  146. []testModeSegment{
  147. {dataModeAlphanumeric, 5}, // length = 4 + 9 + 22 + 6 = 41.
  148. },
  149. },
  150. // Coalesing not necessary.
  151. {
  152. dataEncoderType1To9,
  153. []testModeSegment{
  154. {dataModeAlphanumeric, 1},
  155. {dataModeNumeric, 20},
  156. },
  157. []testModeSegment{
  158. {dataModeAlphanumeric, 1},
  159. {dataModeNumeric, 20},
  160. },
  161. },
  162. // Switch to more general dataMode.
  163. {
  164. dataEncoderType1To9,
  165. []testModeSegment{
  166. {dataModeAlphanumeric, 100},
  167. {dataModeByte, 1},
  168. {dataModeNumeric, 1},
  169. },
  170. []testModeSegment{
  171. {dataModeAlphanumeric, 100},
  172. {dataModeByte, 2},
  173. },
  174. },
  175. // Sometimes encoding everything as bytes is more efficient.
  176. {
  177. dataEncoderType1To9,
  178. []testModeSegment{
  179. {dataModeAlphanumeric, 1},
  180. {dataModeByte, 1},
  181. {dataModeNumeric, 1},
  182. },
  183. []testModeSegment{
  184. {dataModeByte, 3},
  185. },
  186. },
  187. // https://www.google.com/123456789012345678901234567890
  188. // BBBBBAAABBBABBBBBBABBBANNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  189. {
  190. dataEncoderType1To9,
  191. []testModeSegment{
  192. {dataModeByte, 5},
  193. {dataModeAlphanumeric, 3},
  194. {dataModeByte, 3},
  195. {dataModeAlphanumeric, 1},
  196. {dataModeByte, 6},
  197. {dataModeAlphanumeric, 1},
  198. {dataModeAlphanumeric, 4},
  199. {dataModeNumeric, 30},
  200. },
  201. []testModeSegment{
  202. {dataModeByte, 23},
  203. {dataModeNumeric, 30},
  204. },
  205. },
  206. // https://www.google.com/123
  207. // BBBBBAAABBBABBBBBBABBBANNN
  208. // Small segments are inefficient because of additional metadata.
  209. {
  210. dataEncoderType1To9,
  211. []testModeSegment{
  212. {dataModeByte, 5},
  213. {dataModeAlphanumeric, 3},
  214. {dataModeByte, 3},
  215. {dataModeAlphanumeric, 1},
  216. {dataModeByte, 6},
  217. {dataModeAlphanumeric, 1},
  218. {dataModeAlphanumeric, 4},
  219. {dataModeNumeric, 3},
  220. },
  221. []testModeSegment{
  222. {dataModeByte, 26},
  223. },
  224. },
  225. // HTTPS://WWW.GOOGLE.COM/123
  226. // AAAAAAAAAAAAAAAAAAAAAAANNN
  227. {
  228. dataEncoderType1To9,
  229. []testModeSegment{
  230. {dataModeAlphanumeric, 23},
  231. {dataModeNumeric, 3},
  232. },
  233. []testModeSegment{
  234. {dataModeAlphanumeric, 26},
  235. },
  236. },
  237. {
  238. dataEncoderType27To40,
  239. []testModeSegment{
  240. {dataModeByte, 1},
  241. {dataModeNumeric, 1},
  242. {dataModeByte, 1},
  243. {dataModeNumeric, 1},
  244. {dataModeByte, 1},
  245. {dataModeNumeric, 1},
  246. {dataModeByte, 1},
  247. {dataModeNumeric, 1},
  248. },
  249. []testModeSegment{
  250. {dataModeByte, 8},
  251. },
  252. },
  253. // HTTPS://ABC.DE/Q/393AABB6998877XYZ0518AUQCRVJN25
  254. // AAAAAAAAAAAAAAAAANNNAAAANNNNNNNAAANNNNAAAAAAAANN
  255. // different to below---------^--------------------
  256. {
  257. dataEncoderType1To9,
  258. []testModeSegment{
  259. {dataModeAlphanumeric, 17},
  260. {dataModeNumeric, 3},
  261. {dataModeAlphanumeric, 4},
  262. {dataModeNumeric, 7},
  263. {dataModeAlphanumeric, 3},
  264. {dataModeNumeric, 4},
  265. {dataModeAlphanumeric, 8},
  266. {dataModeNumeric, 2},
  267. },
  268. []testModeSegment{
  269. {dataModeAlphanumeric, 48},
  270. },
  271. },
  272. // HTTPS://ABC.DE/Q/393AABB699E877XYZ0518AUQCRVJN25
  273. // AAAAAAAAAAAAAAAAANNNAAAANNNANNNAAANNNNAAAAAAAANN
  274. // different to above---------^--------------------
  275. {
  276. dataEncoderType1To9,
  277. []testModeSegment{
  278. {dataModeAlphanumeric, 17},
  279. {dataModeNumeric, 3},
  280. {dataModeAlphanumeric, 4},
  281. {dataModeNumeric, 3},
  282. {dataModeAlphanumeric, 1},
  283. {dataModeNumeric, 3},
  284. {dataModeAlphanumeric, 3},
  285. {dataModeNumeric, 4},
  286. {dataModeAlphanumeric, 8},
  287. {dataModeNumeric, 2},
  288. },
  289. []testModeSegment{
  290. {dataModeAlphanumeric, 48},
  291. },
  292. },
  293. // 0123456789
  294. // NNNNNNNNNN
  295. {
  296. dataEncoderType1To9,
  297. []testModeSegment{
  298. {dataModeNumeric, 10},
  299. },
  300. []testModeSegment{
  301. {dataModeNumeric, 10},
  302. },
  303. },
  304. }
  305. for _, test := range tests {
  306. numTotalChars := 0
  307. for _, v := range test.actual {
  308. numTotalChars += v.numChars
  309. }
  310. data := make([]byte, numTotalChars)
  311. i := 0
  312. for _, v := range test.actual {
  313. for j := 0; j < v.numChars; j++ {
  314. switch v.dataMode {
  315. case dataModeNumeric:
  316. data[i] = '1'
  317. case dataModeAlphanumeric:
  318. data[i] = 'A'
  319. case dataModeByte:
  320. data[i] = '#'
  321. default:
  322. t.Fatal("Unrecognised data mode")
  323. }
  324. i++
  325. }
  326. }
  327. encoder := newDataEncoder(test.dataEncoderType)
  328. _, err := encoder.encode(data)
  329. if err != nil {
  330. t.Errorf("Got %s, expected valid encoding", err.Error())
  331. } else {
  332. ok := true
  333. if len(encoder.optimised) != len(test.optimised) {
  334. ok = false
  335. } else {
  336. for i, s := range test.optimised {
  337. if encoder.optimised[i].dataMode != s.dataMode ||
  338. len(encoder.optimised[i].data) != s.numChars {
  339. ok = false
  340. break
  341. }
  342. }
  343. }
  344. if !ok {
  345. t.Errorf("got %s, expected %s", segmentsString(encoder.optimised),
  346. testModeSegmentsString(test.optimised))
  347. }
  348. }
  349. }
  350. }
  351. func testModeSegmentsString(segments []testModeSegment) string {
  352. result := "["
  353. for i, segment := range segments {
  354. if i > 0 {
  355. result += ", "
  356. }
  357. result += fmt.Sprintf("%d*%s", segment.numChars,
  358. dataModeString(segment.dataMode))
  359. }
  360. result += "]"
  361. return result
  362. }
  363. func segmentsString(segments []segment) string {
  364. result := "["
  365. for i, segment := range segments {
  366. if i > 0 {
  367. result += ", "
  368. }
  369. result += fmt.Sprintf("%d*%s", len(segment.data),
  370. dataModeString(segment.dataMode))
  371. }
  372. result += "]"
  373. return result
  374. }