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