-
Notifications
You must be signed in to change notification settings - Fork 35
/
matrix_test.go
301 lines (277 loc) · 6.37 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2017 Temple3x (temple3x@gmail.com)
//
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
package reedsolomon
import (
"bytes"
"flag"
"fmt"
"math/bits"
"math/rand"
"testing"
"time"
)
func TestMakeEncodeMatrix(t *testing.T) {
act := makeEncodeMatrix(4, 4)
exp := []byte{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
71, 167, 122, 186,
167, 71, 186, 122,
122, 186, 71, 167,
186, 122, 167, 71}
if !bytes.Equal(act, exp) {
t.Fatal("mismatch")
}
}
func TestMatrixSwap(t *testing.T) {
n := 7
m := make([]byte, n*n)
rand.Seed(time.Now().UnixNano())
fillRandom(m)
exp := make([]byte, n*n)
copy(exp, m)
matrix(exp).swap(0, 1, n)
matrix(exp).swap(0, 1, n)
if !bytes.Equal(exp, m) {
t.Fatalf("swap mismatch")
}
}
func TestMatrixInvert(t *testing.T) {
testCases := []struct {
matrixData []byte
n int
expect []byte
ok bool
expectedErr error
}{
{
[]byte{
56, 23, 98,
3, 100, 200,
45, 201, 123},
3,
[]byte{
175, 133, 33,
130, 13, 245,
112, 35, 126},
true,
nil,
},
{
[]byte{
0, 23, 98,
3, 100, 200,
45, 201, 123},
3,
[]byte{
245, 128, 152,
188, 64, 135,
231, 81, 239},
true,
nil,
},
{
[]byte{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1,
7, 7, 6, 6, 1},
5,
[]byte{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
123, 123, 1, 122, 122,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0},
true,
nil,
},
{
[]byte{
4, 2,
12, 6},
2,
nil,
false,
ErrSingularMatrix,
},
{
[]byte{7, 8, 9},
2,
nil,
false,
ErrNotSquare,
},
}
for i, c := range testCases {
m := matrix(c.matrixData)
actual, actualErr := m.invert(c.n)
if actualErr != nil && c.ok {
t.Errorf("case.%d, expected to pass, but failed with: <ERROR> %s", i+1, actualErr.Error())
}
if actualErr == nil && !c.ok {
t.Errorf("case.%d, expected to fail with <ERROR> \"%s\", but passed", i+1, c.expectedErr)
}
if actualErr != nil && !c.ok {
if c.expectedErr != actualErr {
t.Errorf("case.%d, expected to fail with error \"%s\", but instead failed with error \"%s\"", i+1, c.expectedErr, actualErr)
}
}
if actualErr == nil && c.ok {
if !bytes.Equal(c.expect, actual) {
t.Errorf("case.%d, mismatch", i+1)
}
}
}
}
func TestMakeEncMatrixForReconst(t *testing.T) {
d, p := 4, 4
em := makeEncodeMatrix(d, p)
survivied, _ := genIdxForTest(d, p, d, p)
emr, err := em.makeEncMatrixForReconst(survivied)
if err != nil {
t.Fatal(err)
}
hasM := make([]byte, d*d)
for i, h := range survivied {
copy(hasM[i*d:i*d+d], em[h*d:h*d+d])
}
if !mul(emr, hasM, d).isIdentity(d) {
t.Fatal("make wrong encoding matrix for reconstruction")
}
}
// Check all sub matrices when there is a lost.
// Warn:
// Don't set too big numbers,
// it may have too many combinations, the test will never finish.
func TestEncMatrixInvertibleAll(t *testing.T) {
testEncMatrixInvertible(t, 10, 4)
testEncMatrixInvertible(t, 15, 4)
}
func testEncMatrixInvertible(t *testing.T, d, p int) {
encMatrix := makeEncodeMatrix(d, p)
var bitmap uint64
cnt := 0
// Lost more, bitmap bigger.
var min uint64 = (1 << (d + 1)) - 1 ^ (1 << (d - 1)) // Min value when lost one data row vector.
var max uint64 = ((1 << d) - 1) << p // Max value when lost parity-num data row vectors.
for bitmap = min; bitmap <= max; bitmap++ {
if bits.OnesCount64(bitmap) != d {
continue
}
cnt++
v := bitmap
dpHas := make([]int, d)
c := 0
for i := 0; i < d+p; i++ {
var j uint64 = 1 << i
if j&v == j {
dpHas[c] = i
c++
}
}
m := make([]byte, d*d)
for i := 0; i < d; i++ {
copy(m[i*d:i*d+d], encMatrix[dpHas[i]*d:dpHas[i]*d+d])
}
im, err := matrix(m).invert(d)
if err != nil {
t.Fatalf("encode matrix is singular, d:%d, p:%d, dpHas:%#v", d, p, dpHas)
}
// Check A * A' = I or not,
// ensure nothing wrong in the invert process.
if !mul(im, m, d).isIdentity(d) {
t.Fatalf("matrix invert wrong, d:%d, p:%d, dpHas:%#v", d, p, dpHas)
}
}
t.Logf("%d+%d pass invertible test, total submatrix(with lost): %d", d, p, cnt)
}
var Invertible = flag.Bool("invert-test", false,
"checking encoding matrices' sub-matrices are invertible or not by pick up sub-matrix randomly")
// Check Encoding Matrices' sub-matrices are invertible.
// Randomly pick up sub-matrix every data+parity pair.
//
// This test may cost about 100s, unless modify codes about
// galois field or matrix, there is no need to run it every time,
// so skip the test by default, avoiding waste time in develop process.
func TestEncMatrixInvertibleRandom(t *testing.T) {
if !*Invertible {
t.Skip("skip the test, because it may cost too much time")
}
for d := 1; d < 256; d++ {
for p := 1; p < 256; p++ {
if d+p > 256 {
continue
}
encMatrix := makeEncodeMatrix(d, p)
survived, _ := genIdxForTest(d, p, d, p)
m := make([]byte, d*d)
for i := 0; i < d; i++ {
copy(m[i*d:i*d+d], encMatrix[survived[i]*d:survived[i]*d+d])
}
im, err := matrix(m).invert(d)
if err != nil {
t.Fatalf("encode matrix is singular, d:%d, p:%d, dpHas:%#v", d, p, survived)
}
// Check A * A' = I or not,
// ensure nothing wrong in the invert process.
if !mul(im, m, d).isIdentity(d) {
t.Fatalf("matrix invert wrong, d:%d, p:%d, dpHas:%#v", d, p, survived)
}
}
}
}
// square matrix a * square matrix b = out
func mul(a, b matrix, n int) (out matrix) {
out = make([]byte, n*n)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
d := byte(0)
for k := 0; k < n; k++ {
d ^= gfMul(a[n*i+k], b[n*k+j])
}
out[i*n+j] = d
}
}
return
}
func (m matrix) isIdentity(n int) bool {
im := make([]byte, n*n)
for i := 0; i < n; i++ {
im[i*n+i] = 1
}
return bytes.Equal(m, im)
}
func BenchmarkMakeEncMatrixForReconst(b *testing.B) {
dps := [][2]int{ // data, parity
{4, 4},
{10, 4},
{16, 16},
{64, 64},
{128, 128},
{255, 1},
{256, 0},
}
benchMatrixInvertRun(b, dps)
}
func benchMatrixInvertRun(b *testing.B, dps [][2]int) {
for _, dp := range dps {
d, p := dp[0], dp[1]
b.Run(fmt.Sprintf("(%d+%d)", d, p), func(b *testing.B) {
m := makeEncodeMatrix(d, p)
survived, _ := genIdxForTest(d, p, d, p)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := m.makeEncMatrixForReconst(survived)
if err != nil {
b.Fatal(err)
}
}
})
}
}