forked from skip2/go-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrcode_test.go
173 lines (150 loc) · 2.89 KB
/
qrcode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// go-qrcode
// Copyright 2014 Tom Harwood
package qrcode
import (
"strings"
"testing"
)
func TestQRCodeMaxCapacity(t *testing.T) {
if testing.Short() {
t.Skip("Skipping TestQRCodeCapacity")
}
tests := []struct {
string string
numRepetitions int
}{
{
"0",
7089,
},
{
"A",
4296,
},
{
"#",
2953,
},
// Alternate byte/numeric data types. Optimises to 2,952 bytes.
{
"#1",
1476,
},
}
for _, test := range tests {
_, err := New(strings.Repeat(test.string, test.numRepetitions), Low)
if err != nil {
t.Errorf("%d x '%s' got %s expected success", test.numRepetitions,
test.string, err.Error())
}
}
for _, test := range tests {
_, err := New(strings.Repeat(test.string, test.numRepetitions+1), Low)
if err == nil {
t.Errorf("%d x '%s' chars encodable, expected not encodable",
test.numRepetitions+1, test.string)
}
}
}
func TestQRCodeVersionCapacity(t *testing.T) {
tests := []struct {
version int
level RecoveryLevel
maxNumeric int
maxAlphanumeric int
maxByte int
}{
{
1,
Low,
41,
25,
17,
},
{
2,
Low,
77,
47,
32,
},
{
2,
Highest,
34,
20,
14,
},
{
40,
Low,
7089,
4296,
2953,
},
{
40,
Highest,
3057,
1852,
1273,
},
}
for i, test := range tests {
numericData := strings.Repeat("1", test.maxNumeric)
alphanumericData := strings.Repeat("A", test.maxAlphanumeric)
byteData := strings.Repeat("#", test.maxByte)
var n *QRCode
var a *QRCode
var b *QRCode
var err error
n, err = New(numericData, test.level)
if err != nil {
t.Fatal(err.Error())
}
a, err = New(alphanumericData, test.level)
if err != nil {
t.Fatal(err.Error())
}
b, err = New(byteData, test.level)
if err != nil {
t.Fatal(err.Error())
}
if n.VersionNumber != test.version {
t.Fatalf("Test #%d numeric has version #%d, expected #%d", i,
n.VersionNumber, test.version)
}
if a.VersionNumber != test.version {
t.Fatalf("Test #%d alphanumeric has version #%d, expected #%d", i,
a.VersionNumber, test.version)
}
if b.VersionNumber != test.version {
t.Fatalf("Test #%d byte has version #%d, expected #%d", i,
b.VersionNumber, test.version)
}
}
}
func TestQRCodeISOAnnexIExample(t *testing.T) {
var q *QRCode
q, err := New("01234567", Medium)
if err != nil {
t.Fatalf("Error producing ISO Annex I Example: %s, expected success",
err.Error())
}
const expectedMask int = 2
if q.mask != 2 {
t.Errorf("ISO Annex I example mask got %d, expected %d\n", q.mask,
expectedMask)
}
}
func BenchmarkQRCodeURLSize(b *testing.B) {
for n := 0; n < b.N; n++ {
New("http://www.example.org", Medium)
}
}
func BenchmarkQRCodeMaximumSize(b *testing.B) {
for n := 0; n < b.N; n++ {
// 7089 is the maximum encodable number of numeric digits.
New(strings.Repeat("0", 7089), Low)
}
}