-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat_test.go
350 lines (311 loc) · 6.25 KB
/
mat_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package z2z
import (
"fmt"
"math/rand"
"testing"
)
var (
s8 = "00000000"
s16 = s8 + s8
s32 = s16 + s16
s64 = s32 + s32
n = "\n"
)
func TestNbWordsPerLine(t *testing.T) {
type td struct {
l int
c int
nb int
}
tdata := []td{
{0, 0, 1},
{1, 1, 1},
{0, 1, 1},
{1, 0, 1},
{0, 0, 1},
{1, 63, 1},
{1, 64, 1},
{1, 65, 2},
{2, 63, 1},
{2, 64, 1},
{2, 65, 2},
{0, 63, 1},
{0, 64, 1},
{0, 65, 2},
{2, 127, 2},
{2, 128, 2},
{2, 129, 3},
}
for i, tt := range tdata {
m := NewMat(tt.l, tt.c)
if m.nbOfWordsPerLine() != tt.nb {
t.Fatalf("invalid nbOfWords, tdata index %d, want : %d, got %d", i, tt.nb, m.nbOfWordsPerLine())
}
}
}
func TestBitCoord(t *testing.T) {
type td struct {
l int
c int
word int
shift int
}
tdata := []td{
{0, 0, 0, 0},
{3, 65, 7, 1},
{3, 64, 7, 0},
{3, 63, 6, 63},
{0, 7, 0, 7},
}
m := NewMat(5, 80)
for i, td := range tdata {
w, s := m.bitCoordinates(td.l, td.c)
if w != td.word || s != td.shift {
fmt.Printf("tdata : %d, want : %d, %d\t got : %d, %d", i, td.word, td.shift, w, s)
t.Fatal("bit coordinates do not match")
}
}
}
func TestVectString(t *testing.T) {
type td struct {
c int // nb columns
z string // expected string result
}
tdata := []td{
{0, n},
{1, "0" + n},
{2, "00" + n},
{32, s32 + n},
{64, s64 + n},
{63, s64[1:] + n},
{65, s64 + "0" + n},
}
tdata1 := []td{
{1, "1" + n},
{2, "01" + n},
{64, s32 + s16 + s8 + "00000001" + n},
{63, s32 + s16 + s8 + "0000001" + n},
{65, s32 + s16 + s8 + "000000001" + n},
{66, s32 + s16 + s8 + "0000000001" + n},
}
// test zeros
for _, d := range tdata {
v := NewVect(d.c)
ss := v.String()
if d.z != ss {
fmt.Printf("test zeros - length : %d\nExpected : %s\nGot : %s\n", d.c, d.z, ss)
t.Fatal()
}
}
// test with one
for _, d := range tdata1 {
v := NewVect(d.c)
v.d[0] = 1
ss := v.String()
if d.z != ss {
fmt.Printf("test ones - length : %d\nExpected : %s\nGot : %s\n", d.c, d.z, ss)
t.Fatal()
}
}
}
func TestString(t *testing.T) {
//t.Skip()
for i := 0; i < 64; i++ {
//fmt.Print(NewVectUint64(1 << i).String())
fmt.Print(NewVectInt(1 << i).String())
}
}
func TestNormalize(t *testing.T) {
m := NewMat(3, 5)
m.c = 64
m.Ones()
fmt.Println(m)
m.c = 5
m.Normalize()
m.c = 64
fmt.Println(m)
}
func TestSetGet(t *testing.T) {
m := NewMat(3, 63*3)
m.Randomize()
for i := 0; i < 100; i++ {
l, c := rand.Intn(m.l), rand.Intn(m.c)
m.Set(l, c, 0)
v := m.Get(l, c)
if v != 0 {
t.Fatal("should read 0")
}
m.Set(l, c, 1)
v = m.Get(l, c)
if v != 1 {
t.Fatal("should read 1")
}
v = rand.Intn(2)
m.Set(l, c, v)
if m.Get(l, c) != v {
t.Fatalf("Random write/read failed")
}
}
m.Ones()
for i := 0; i < 100; i++ {
l, c := rand.Intn(m.l), rand.Intn(m.c)
if m.Get(l, c) != 1 {
t.Fatal("should read 1")
}
}
m.Zeros()
for i := 0; i < 100; i++ {
l, c := rand.Intn(m.l), rand.Intn(m.c)
if m.Get(l, c) != 0 {
t.Fatal("should read 0")
}
}
}
func TestNewLine(t *testing.T) {
m := NewMat(4, 5)
m.Ones()
fmt.Println(m)
for l := 0; l < 4; l++ {
m.NewLine()
fmt.Println(m)
}
}
func TestNewCol1(t *testing.T) {
m := NewMat(4, 5)
m.Ones()
fmt.Println(m)
for l := 0; l < 4; l++ {
m.NewCol()
fmt.Println(m)
}
}
func TestNewCol2(t *testing.T) {
checkAddedCols(t, 10, 3, 5)
checkAddedCols(t, 10, 63, 5)
checkAddedCols(t, 10, 2*63, 6)
checkAddedCols(t, 10, 3*63, 7)
checkAddedCols(t, 10, 4*63, 8)
checkAddedCols(t, 1, 4*63, 8)
checkAddedCols(t, 10, 2, 300)
}
func checkAddedCols(t *testing.T, l int, c int, adds int) {
m := NewMat(l, c)
m.Randomize()
ref := m.Clone()
//fmt.Println(m)
for l := 0; l < 6; l++ {
m.NewCol()
//fmt.Println(m)
}
//Check result
for l := 0; l < ref.l; l++ {
for c := 0; c < ref.c; c++ {
if ref.Get(l, c) != m.Get(l, c) {
fmt.Println("dims", ref.l, ref.c, "-->", m.l, m.c)
fmt.Println(ref)
fmt.Println(m)
t.Fatalf("Sub matrix does not match after adding cols at l=%d, c=%d", l, c)
}
}
for c := ref.c; c < m.c; c++ {
if m.Get(l, c) != 0 {
fmt.Println("dims", ref.l, ref.c, "-->", m.l, m.c)
fmt.Println(ref)
fmt.Println(m)
t.Fatal("Added columns should be zero")
}
}
}
}
func TestTranspose(t *testing.T) {
var m, tm, ttm *Mat
m = NewId(200)
tm = m.T()
if !m.Equal(tm) {
t.Fatal("ID should self transpose")
}
m = NewMat(3, 7)
tm = m.T()
if m.Equal(tm) {
t.Fatal("unexpected equal transpose")
}
ttm = tm.T()
if !ttm.Equal(m) {
t.Fatal("Double transpose should be idempotent")
}
m = NewMat(80, 7)
tm = m.T()
ttm = tm.T()
if !ttm.Equal(m) {
t.Fatal("Double transpose should be idempotent")
}
m = NewMat(230, 7)
tm = m.T()
ttm = tm.T()
if !ttm.Equal(m) {
t.Fatal("Double transpose should be idempotent")
}
m = NewMat(3, 277)
tm = m.T()
ttm = tm.T()
if !ttm.Equal(m) {
t.Fatal("Double transpose should be idempotent")
}
}
func TestMatMul(t *testing.T) {
if NewId(127).OnesCount() != 127 {
t.Fatal("error constructing Id, wrong nb of bits set")
}
m := NewMat(5, 10)
m.Randomize()
n := NewMat(10, 3)
n.Randomize()
p := m.MatMul(n)
fmt.Println(m)
fmt.Println(n)
fmt.Println(p)
l, c := p.Dimensions()
if l != m.l || c != n.c {
t.Fatal("mismatched dimensions")
}
tm := m.T()
tn := n.T()
tp := p.T()
if !tp.Equal(tn.MatMul(tm)) {
t.Fatal("Transposed multiplicatio does not work")
}
if !m.MatMul(NewId(10)).Equal(m) {
t.Fatal("multiplication by ID failed")
}
if !NewId(5).MatMul(m).Equal(m) {
t.Fatal("multiplication by ID failed")
}
}
func TestNewFromBytes(t *testing.T) {
type td struct {
bs []byte
st string
}
data := []td{
{[]byte{0}, "00000000"},
{[]byte{1}, "00000001"},
{[]byte{128}, "10000000"},
{[]byte{0, 0}, "0000000000000000"},
{[]byte{0, 1}, "0000000100000000"},
{[]byte{1, 0}, "0000000000000001"},
{[]byte{1, 2}, "0000001000000001"},
{[]byte{0, 3, 0}, "000000000000001100000000"},
{[]byte{128, 3, 0}, "000000000000001110000000"},
{[]byte{0, 3, 128}, "100000000000001100000000"},
}
for i, d := range data {
v := NewFromBytes(d.bs)
if v.l != 1 || v.c != len(d.bs)*8 {
t.Fatal("dims are mismatched")
}
if v.stringL(0) != d.st {
fmt.Printf("%d) : bytes %b\ngot : %s\nwant: %s\n", i, d.bs, v, d.st)
t.Fatal("incorrect result")
}
}
}