-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathmatrix_test.go
173 lines (152 loc) · 3.42 KB
/
matrix_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
package qrcode
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMatrix(t *testing.T) {
m := newMatrix(3, 3)
m.print()
err := m.set(2, 4, QRValue_DATA_V1)
assert.Error(t, err)
err = m.set(2, 2, QRValue_DATA_V1)
assert.NoError(t, err)
v, err2 := m.at(2, 2)
assert.NoError(t, err2)
assert.Equal(t, QRValue_DATA_V1, v)
m.print()
// m.Reset(2, 2)
}
func TestMatrix_Copy(t *testing.T) {
// pre
var (
m1 = newMatrix(3, 3)
m2 = newMatrix(3, 3)
)
_ = m1.set(1, 1, QRValue_DATA_V0)
_ = m1.set(0, 0, QRValue_DATA_V0)
_ = m2.set(1, 1, QRValue_DATA_V0)
_ = m2.set(0, 0, QRValue_DATA_V0)
// do copy
got := m1.Copy()
// change origin
_ = m1.set(2, 2, QRValue_DATA_V1)
assert.Equal(t, m2, got)
s, err := m1.at(2, 2)
assert.NoError(t, err)
assert.Equal(t, QRValue_DATA_V1, s)
s, err = got.at(2, 2)
assert.NoError(t, err)
assert.NotEqual(t, QRValue_DATA_V1, s)
assert.Equal(t, QRValue_INIT_V0, s)
}
//func Test_stateSliceMatched(t *testing.T) {
// type args struct {
// ss1 []qrtype
// ss2 []qrtype
// }
// tests := []struct {
// name string
// args args
// want qrbool
// }{
// {
// name: "case 0",
// args: args{
// ss1: []qrtype{QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V1},
// ss2: []qrtype{QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V1},
// },
// want: true,
// },
// {
// name: "case 0",
// args: args{
// ss1: []qrtype{QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V1},
// ss2: []qrtype{QRValue_DATA_V0, QRValue_DATA_V1, QRValue_DATA_V0, QRValue_DATA_V0, QRValue_DATA_V1},
// },
// want: false,
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// if got := StateSliceMatched(tt.args.ss1, tt.args.ss2); got != tt.want {
// t.Errorf("stateSliceMatched() = %v, want %v", got, tt.want)
// }
// })
// }
//}
// go test -run=NONE -bench Benchmark_Iterate -count 10 > old.txt
// after change to `m.iter(IterDirection_COLUMN, rowIteration)`
// go test -run=NONE -bench Benchmark_Iterate -count 10 > new.txt
// benchstat old.txt new.txt
func Benchmark_Iterate(b *testing.B) {
// initialize
size := 100
m := newMatrix(size, size)
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
_ = m.set(i, j, QRValue_DATA_V1)
}
}
b.ResetTimer()
rowIteration := func(x, y int, s qrvalue) {
_, _ = x, y
_ = s
}
for i := 0; i < b.N; i++ {
//m.iter(IterDirection_ROW, rowIteration)
m.iter(IterDirection_COLUMN, rowIteration)
}
}
func Test_Matrix_RowAndCol(t *testing.T) {
m := newMatrix(3, 3)
for i := 0; i < 3; i++ {
_ = m.set(i, 0, QRValue_DATA_V1)
_ = m.set(i, i, QRValue_DATA_V1)
}
// 1 1 1
// 0 1 0
// 0 0 1
tests := []struct {
name string
rowed bool
cur int
want []qrvalue
}{
{
name: "row[1]",
rowed: true,
cur: 1,
want: []qrvalue{QRValue_INIT_V0, QRValue_DATA_V1, QRValue_INIT_V0},
},
{
name: "col[2]",
rowed: false,
cur: 2,
want: []qrvalue{QRValue_DATA_V1, QRValue_INIT_V0, QRValue_DATA_V1},
},
{
name: "row out",
rowed: true,
cur: 4,
want: nil,
},
{
name: "col out",
rowed: false,
cur: 4,
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got []qrvalue
switch tt.rowed {
case true:
got = m.Row(tt.cur)
default:
got = m.Col(tt.cur)
}
assert.Equal(t, tt.want, got)
})
}
}