Repository navigation
Expand file tree
/
Copy pathlayout_codewords_test.go
More file actions
85 lines (81 loc) · 2.28 KB
/
Copy pathlayout_codewords_test.go
File metadata and controls
85 lines (81 loc) · 2.28 KB
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
package qr
import (
"fmt"
"math/rand"
"testing"
)
// zigzagCodewords is drawCodewords up to v2.6, which walked the zig-zag
// itself. It fills the non-function modules with the given codeword bytes
// following the QR Code zig-zag traversal.
func (q *builder) zigzagCodewords(data []byte) error {
numRawDataModules := numRawDataModules(q.version) / 8
if len(data) != numRawDataModules {
return fmt.Errorf("%w: codeword length mismatch", ErrInvalidArgument)
}
i := 0
for right := q.size - 1; right >= 1; right -= 2 {
if right == 6 {
right = 5
}
for vert := 0; vert < q.size; vert++ {
for j := 0; j < 2; j++ {
x := right - j
upward := ((right + 1) & 2) == 0
y := vert
if upward {
y = q.size - 1 - vert
}
if !q.isFunction[y][x] && i < len(data)*8 {
q.modules[y][x] = getBit(int(data[i>>3]), 7-(i&7))
i++
}
}
}
}
return nil
}
func TestDrawCodewordsMatchesZigzag(t *testing.T) {
rng := rand.New(rand.NewSource(1))
for v := 1; v <= 40; v++ {
data := make([]byte, numRawDataModules(v)/8)
rng.Read(data)
for _, ecc := range []ECC{ECCLow, ECCHigh} {
want := newBuilder(v, ecc)
want.drawFunctionPatterns()
if err := want.zigzagCodewords(data); err != nil {
t.Fatal(err)
}
got := newBuilder(v, ecc)
got.drawFunctionPatterns()
if err := got.drawCodewords(data); err != nil {
t.Fatal(err)
}
for y := range want.modules {
for x := range want.modules[y] {
if got.modules[y][x] != want.modules[y][x] {
t.Fatalf("version %d: module (%d, %d) differs", v, x, y)
}
}
}
// The packed rows are the modules, format bits aside: the
// template's function rows have those of mask 0 at level L,
// which mask selection redraws.
ref := newPackedModules(got.size)
ref.pack(got.modules)
for y := 0; y < got.size; y++ {
line := got.packed[y]
for x := 0; x < got.size; x++ {
if x != 8 && y != 8 && line.dark(x) != got.modules[y][x] {
t.Fatalf("version %d: packed module (%d, %d) differs", v, x, y)
}
}
if line.dark(got.size) {
t.Fatalf("version %d: packed row %d has bits past the side", v, y)
}
}
}
}
if err := newBuilder(1, ECCLow).drawCodewords(make([]byte, 3)); err == nil {
t.Error("drawCodewords accepted the wrong number of codewords")
}
}