qrcode_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package qr
  2. import (
  3. "image/color"
  4. "testing"
  5. )
  6. func Test_NewQRCode(t *testing.T) {
  7. bc := newBarcode(2)
  8. if bc == nil {
  9. t.Fail()
  10. }
  11. if bc.data.Len() != 4 {
  12. t.Fail()
  13. }
  14. if bc.dimension != 2 {
  15. t.Fail()
  16. }
  17. }
  18. func Test_QRBasics(t *testing.T) {
  19. qr := newBarcode(10)
  20. if qr.ColorModel() != color.Gray16Model {
  21. t.Fail()
  22. }
  23. code, _ := Encode("test", L, Unicode)
  24. if code.Content() != "test" {
  25. t.Fail()
  26. }
  27. if code.Metadata().Dimensions != 2 {
  28. t.Fail()
  29. }
  30. bounds := code.Bounds()
  31. if bounds.Min.X != 0 || bounds.Min.Y != 0 || bounds.Max.X != 21 || bounds.Max.Y != 21 {
  32. t.Fail()
  33. }
  34. if code.At(0, 0) != color.Black || code.At(0, 7) != color.White {
  35. t.Fail()
  36. }
  37. qr = code.(*qrcode)
  38. if !qr.Get(0, 0) || qr.Get(0, 7) {
  39. t.Fail()
  40. }
  41. sum := qr.calcPenaltyRule1() + qr.calcPenaltyRule2() + qr.calcPenaltyRule3() + qr.calcPenaltyRule4()
  42. if qr.calcPenalty() != sum {
  43. t.Fail()
  44. }
  45. }
  46. func Test_Penalty1(t *testing.T) {
  47. qr := newBarcode(7)
  48. if qr.calcPenaltyRule1() != 70 {
  49. t.Fail()
  50. }
  51. qr.Set(0, 0, true)
  52. if qr.calcPenaltyRule1() != 68 {
  53. t.Fail()
  54. }
  55. qr.Set(0, 6, true)
  56. if qr.calcPenaltyRule1() != 66 {
  57. t.Fail()
  58. }
  59. }
  60. func Test_Penalty2(t *testing.T) {
  61. qr := newBarcode(3)
  62. if qr.calcPenaltyRule2() != 12 {
  63. t.Fail()
  64. }
  65. qr.Set(0, 0, true)
  66. qr.Set(1, 1, true)
  67. qr.Set(2, 0, true)
  68. if qr.calcPenaltyRule2() != 0 {
  69. t.Fail()
  70. }
  71. qr.Set(1, 1, false)
  72. if qr.calcPenaltyRule2() != 6 {
  73. t.Fail()
  74. }
  75. }
  76. func Test_Penalty3(t *testing.T) {
  77. runTest := func(content string, result uint) {
  78. code, _ := Encode(content, L, AlphaNumeric)
  79. qr := code.(*qrcode)
  80. if qr.calcPenaltyRule3() != result {
  81. t.Errorf("Failed Penalty Rule 3 for content \"%s\" got %d but expected %d", content, qr.calcPenaltyRule3(), result)
  82. }
  83. }
  84. runTest("A", 80)
  85. runTest("FOO", 40)
  86. runTest("0815", 0)
  87. }
  88. func Test_Penalty4(t *testing.T) {
  89. qr := newBarcode(3)
  90. if qr.calcPenaltyRule4() != 100 {
  91. t.Fail()
  92. }
  93. qr.Set(0, 0, true)
  94. if qr.calcPenaltyRule4() != 70 {
  95. t.Fail()
  96. }
  97. qr.Set(0, 1, true)
  98. if qr.calcPenaltyRule4() != 50 {
  99. t.Fail()
  100. }
  101. qr.Set(0, 2, true)
  102. if qr.calcPenaltyRule4() != 30 {
  103. t.Fail()
  104. }
  105. qr.Set(1, 0, true)
  106. if qr.calcPenaltyRule4() != 10 {
  107. t.Fail()
  108. }
  109. qr.Set(1, 1, true)
  110. if qr.calcPenaltyRule4() != 10 {
  111. t.Fail()
  112. }
  113. qr = newBarcode(2)
  114. qr.Set(0, 0, true)
  115. qr.Set(1, 0, true)
  116. if qr.calcPenaltyRule4() != 0 {
  117. t.Fail()
  118. }
  119. }