File: qrcode_test.go

package info (click to toggle)
golang-barcode 0.0~git20140830-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 280 kB
  • ctags: 270
  • sloc: makefile: 4
file content (126 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (4)
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
package qr

import (
	"image/color"
	"testing"
)

func Test_NewQRCode(t *testing.T) {
	bc := newBarcode(2)
	if bc == nil {
		t.Fail()
	}
	if bc.data.Len() != 4 {
		t.Fail()
	}
	if bc.dimension != 2 {
		t.Fail()
	}
}

func Test_QRBasics(t *testing.T) {
	qr := newBarcode(10)
	if qr.ColorModel() != color.Gray16Model {
		t.Fail()
	}
	code, _ := Encode("test", L, Unicode)
	if code.Content() != "test" {
		t.Fail()
	}
	if code.Metadata().Dimensions != 2 {
		t.Fail()
	}
	bounds := code.Bounds()
	if bounds.Min.X != 0 || bounds.Min.Y != 0 || bounds.Max.X != 21 || bounds.Max.Y != 21 {
		t.Fail()
	}
	if code.At(0, 0) != color.Black || code.At(0, 7) != color.White {
		t.Fail()
	}
	qr = code.(*qrcode)
	if !qr.Get(0, 0) || qr.Get(0, 7) {
		t.Fail()
	}
	sum := qr.calcPenaltyRule1() + qr.calcPenaltyRule2() + qr.calcPenaltyRule3() + qr.calcPenaltyRule4()
	if qr.calcPenalty() != sum {
		t.Fail()
	}
}

func Test_Penalty1(t *testing.T) {
	qr := newBarcode(7)
	if qr.calcPenaltyRule1() != 70 {
		t.Fail()
	}
	qr.Set(0, 0, true)
	if qr.calcPenaltyRule1() != 68 {
		t.Fail()
	}
	qr.Set(0, 6, true)
	if qr.calcPenaltyRule1() != 66 {
		t.Fail()
	}
}

func Test_Penalty2(t *testing.T) {
	qr := newBarcode(3)
	if qr.calcPenaltyRule2() != 12 {
		t.Fail()
	}
	qr.Set(0, 0, true)
	qr.Set(1, 1, true)
	qr.Set(2, 0, true)
	if qr.calcPenaltyRule2() != 0 {
		t.Fail()
	}
	qr.Set(1, 1, false)
	if qr.calcPenaltyRule2() != 6 {
		t.Fail()
	}
}

func Test_Penalty3(t *testing.T) {
	runTest := func(content string, result uint) {
		code, _ := Encode(content, L, AlphaNumeric)
		qr := code.(*qrcode)
		if qr.calcPenaltyRule3() != result {
			t.Errorf("Failed Penalty Rule 3 for content \"%s\" got %d but expected %d", content, qr.calcPenaltyRule3(), result)
		}
	}
	runTest("A", 80)
	runTest("FOO", 40)
	runTest("0815", 0)
}

func Test_Penalty4(t *testing.T) {
	qr := newBarcode(3)
	if qr.calcPenaltyRule4() != 100 {
		t.Fail()
	}
	qr.Set(0, 0, true)
	if qr.calcPenaltyRule4() != 70 {
		t.Fail()
	}
	qr.Set(0, 1, true)
	if qr.calcPenaltyRule4() != 50 {
		t.Fail()
	}
	qr.Set(0, 2, true)
	if qr.calcPenaltyRule4() != 30 {
		t.Fail()
	}
	qr.Set(1, 0, true)
	if qr.calcPenaltyRule4() != 10 {
		t.Fail()
	}
	qr.Set(1, 1, true)
	if qr.calcPenaltyRule4() != 10 {
		t.Fail()
	}
	qr = newBarcode(2)
	qr.Set(0, 0, true)
	qr.Set(1, 0, true)
	if qr.calcPenaltyRule4() != 0 {
		t.Fail()
	}
}