versioninfo_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package qr
  2. import "testing"
  3. var testvi = &versionInfo{7, M, 0, 1, 10, 2, 5} // Fake versionInfo to run some of the tests
  4. func Test_ErrorCorrectionStringer(t *testing.T) {
  5. tests := map[ErrorCorrectionLevel]string{
  6. L: "L", M: "M", Q: "Q", H: "H", ErrorCorrectionLevel(99): "unknown",
  7. }
  8. for ecl, str := range tests {
  9. if ecl.String() != str {
  10. t.Fail()
  11. }
  12. }
  13. }
  14. func Test_CharCountBits(t *testing.T) {
  15. v1 := &versionInfo{5, M, 0, 0, 0, 0, 0}
  16. v2 := &versionInfo{15, M, 0, 0, 0, 0, 0}
  17. v3 := &versionInfo{30, M, 0, 0, 0, 0, 0}
  18. if v1.charCountBits(numericMode) != 10 {
  19. t.Fail()
  20. }
  21. if v1.charCountBits(alphaNumericMode) != 9 {
  22. t.Fail()
  23. }
  24. if v1.charCountBits(byteMode) != 8 {
  25. t.Fail()
  26. }
  27. if v1.charCountBits(kanjiMode) != 8 {
  28. t.Fail()
  29. }
  30. if v2.charCountBits(numericMode) != 12 {
  31. t.Fail()
  32. }
  33. if v2.charCountBits(alphaNumericMode) != 11 {
  34. t.Fail()
  35. }
  36. if v2.charCountBits(byteMode) != 16 {
  37. t.Fail()
  38. }
  39. if v2.charCountBits(kanjiMode) != 10 {
  40. t.Fail()
  41. }
  42. if v3.charCountBits(numericMode) != 14 {
  43. t.Fail()
  44. }
  45. if v3.charCountBits(alphaNumericMode) != 13 {
  46. t.Fail()
  47. }
  48. if v3.charCountBits(byteMode) != 16 {
  49. t.Fail()
  50. }
  51. if v3.charCountBits(kanjiMode) != 12 {
  52. t.Fail()
  53. }
  54. if v1.charCountBits(encodingMode(3)) != 0 {
  55. t.Fail()
  56. }
  57. }
  58. func Test_TotalDataBytes(t *testing.T) {
  59. if testvi.totalDataBytes() != 20 {
  60. t.Fail()
  61. }
  62. }
  63. func Test_ModulWidth(t *testing.T) {
  64. if testvi.modulWidth() != 45 {
  65. t.Fail()
  66. }
  67. }
  68. func Test_FindSmallestVersionInfo(t *testing.T) {
  69. if findSmallestVersionInfo(H, alphaNumericMode, 10208) != nil {
  70. t.Error("there should be no version with this capacity")
  71. }
  72. test := func(cap int, tVersion byte) {
  73. v := findSmallestVersionInfo(H, alphaNumericMode, cap)
  74. if v == nil || v.Version != tVersion {
  75. t.Errorf("version %d should be returned.", tVersion)
  76. }
  77. }
  78. test(10191, 40)
  79. test(5591, 29)
  80. test(5592, 30)
  81. test(190, 3)
  82. test(200, 4)
  83. }
  84. type aligmnentTest struct {
  85. version byte
  86. patterns []int
  87. }
  88. var allAligmnentTests = []*aligmnentTest{
  89. &aligmnentTest{1, []int{}},
  90. &aligmnentTest{2, []int{6, 18}},
  91. &aligmnentTest{3, []int{6, 22}},
  92. &aligmnentTest{4, []int{6, 26}},
  93. &aligmnentTest{5, []int{6, 30}},
  94. &aligmnentTest{6, []int{6, 34}},
  95. &aligmnentTest{7, []int{6, 22, 38}},
  96. &aligmnentTest{8, []int{6, 24, 42}},
  97. &aligmnentTest{9, []int{6, 26, 46}},
  98. &aligmnentTest{10, []int{6, 28, 50}},
  99. &aligmnentTest{11, []int{6, 30, 54}},
  100. &aligmnentTest{12, []int{6, 32, 58}},
  101. &aligmnentTest{13, []int{6, 34, 62}},
  102. &aligmnentTest{14, []int{6, 26, 46, 66}},
  103. &aligmnentTest{15, []int{6, 26, 48, 70}},
  104. &aligmnentTest{16, []int{6, 26, 50, 74}},
  105. &aligmnentTest{17, []int{6, 30, 54, 78}},
  106. &aligmnentTest{18, []int{6, 30, 56, 82}},
  107. &aligmnentTest{19, []int{6, 30, 58, 86}},
  108. &aligmnentTest{20, []int{6, 34, 62, 90}},
  109. &aligmnentTest{21, []int{6, 28, 50, 72, 94}},
  110. &aligmnentTest{22, []int{6, 26, 50, 74, 98}},
  111. &aligmnentTest{23, []int{6, 30, 54, 78, 102}},
  112. &aligmnentTest{24, []int{6, 28, 54, 80, 106}},
  113. &aligmnentTest{25, []int{6, 32, 58, 84, 110}},
  114. &aligmnentTest{26, []int{6, 30, 58, 86, 114}},
  115. &aligmnentTest{27, []int{6, 34, 62, 90, 118}},
  116. &aligmnentTest{28, []int{6, 26, 50, 74, 98, 122}},
  117. &aligmnentTest{29, []int{6, 30, 54, 78, 102, 126}},
  118. &aligmnentTest{30, []int{6, 26, 52, 78, 104, 130}},
  119. &aligmnentTest{31, []int{6, 30, 56, 82, 108, 134}},
  120. &aligmnentTest{32, []int{6, 34, 60, 86, 112, 138}},
  121. &aligmnentTest{33, []int{6, 30, 58, 86, 114, 142}},
  122. &aligmnentTest{34, []int{6, 34, 62, 90, 118, 146}},
  123. &aligmnentTest{35, []int{6, 30, 54, 78, 102, 126, 150}},
  124. &aligmnentTest{36, []int{6, 24, 50, 76, 102, 128, 154}},
  125. &aligmnentTest{37, []int{6, 28, 54, 80, 106, 132, 158}},
  126. &aligmnentTest{38, []int{6, 32, 58, 84, 110, 136, 162}},
  127. &aligmnentTest{39, []int{6, 26, 54, 82, 110, 138, 166}},
  128. &aligmnentTest{40, []int{6, 30, 58, 86, 114, 142, 170}},
  129. }
  130. func Test_AlignmentPatternPlacements(t *testing.T) {
  131. for _, at := range allAligmnentTests {
  132. vi := &versionInfo{at.version, M, 0, 0, 0, 0, 0}
  133. res := vi.alignmentPatternPlacements()
  134. if len(res) != len(at.patterns) {
  135. t.Errorf("number of alignmentpatterns missmatch for version %d", at.version)
  136. }
  137. for i := 0; i < len(res); i++ {
  138. if res[i] != at.patterns[i] {
  139. t.Errorf("alignmentpatterns for version %d missmatch on index %d", at.version, i)
  140. }
  141. }
  142. }
  143. }