version_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package qrcode
  4. import (
  5. "testing"
  6. bitset "github.com/skip2/go-qrcode/bitset"
  7. )
  8. func TestFormatInfo(t *testing.T) {
  9. tests := []struct {
  10. level RecoveryLevel
  11. maskPattern int
  12. expected uint32
  13. }{
  14. { // L=01 M=00 Q=11 H=10
  15. Low,
  16. 1,
  17. 0x72f3,
  18. },
  19. {
  20. Medium,
  21. 2,
  22. 0x5e7c,
  23. },
  24. {
  25. High,
  26. 3,
  27. 0x3a06,
  28. },
  29. {
  30. Highest,
  31. 4,
  32. 0x0762,
  33. },
  34. {
  35. Low,
  36. 5,
  37. 0x6318,
  38. },
  39. {
  40. Medium,
  41. 6,
  42. 0x4f97,
  43. },
  44. {
  45. High,
  46. 7,
  47. 0x2bed,
  48. },
  49. }
  50. for i, test := range tests {
  51. v := getQRCodeVersion(test.level, 1)
  52. result := v.formatInfo(test.maskPattern)
  53. expected := bitset.New()
  54. expected.AppendUint32(test.expected, formatInfoLengthBits)
  55. if !expected.Equals(result) {
  56. t.Errorf("formatInfo test #%d got %s, expected %s", i, result.String(),
  57. expected.String())
  58. }
  59. }
  60. }
  61. func TestVersionInfo(t *testing.T) {
  62. tests := []struct {
  63. version int
  64. expected uint32
  65. }{
  66. {
  67. 7,
  68. 0x007c94,
  69. },
  70. {
  71. 10,
  72. 0x00a4d3,
  73. },
  74. {
  75. 20,
  76. 0x0149a6,
  77. },
  78. {
  79. 30,
  80. 0x01ed75,
  81. },
  82. {
  83. 40,
  84. 0x028c69,
  85. },
  86. }
  87. for i, test := range tests {
  88. var v *qrCodeVersion
  89. v = getQRCodeVersion(Low, test.version)
  90. result := v.versionInfo()
  91. expected := bitset.New()
  92. expected.AppendUint32(test.expected, versionInfoLengthBits)
  93. if !expected.Equals(result) {
  94. t.Errorf("versionInfo test #%d got %s, expected %s", i, result.String(),
  95. expected.String())
  96. }
  97. }
  98. }
  99. func TestNumBitsToPadToCodeoword(t *testing.T) {
  100. tests := []struct {
  101. level RecoveryLevel
  102. version int
  103. numDataBits int
  104. expected int
  105. }{
  106. {
  107. Low,
  108. 1,
  109. 0,
  110. 0,
  111. }, {
  112. Low,
  113. 1,
  114. 1,
  115. 7,
  116. }, {
  117. Low,
  118. 1,
  119. 7,
  120. 1,
  121. }, {
  122. Low,
  123. 1,
  124. 8,
  125. 0,
  126. },
  127. }
  128. for i, test := range tests {
  129. var v *qrCodeVersion
  130. v = getQRCodeVersion(test.level, test.version)
  131. result := v.numBitsToPadToCodeword(test.numDataBits)
  132. if result != test.expected {
  133. t.Errorf("numBitsToPadToCodeword test %d (version=%d numDataBits=%d), got %d, expected %d",
  134. i, test.version, test.numDataBits, result, test.expected)
  135. }
  136. }
  137. }