-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrix_test.go
116 lines (91 loc) · 1.7 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
package matrix_test
import (
"testing"
"github.com/EclesioMeloJunior/matrix"
"github.com/stretchr/testify/assert"
)
func TestZeroMatrix(t *testing.T) {
expectedM := matrix.Matrix(
[][]int64{
{0, 0},
{0, 0},
},
)
z := matrix.Zero(2, 2)
assert.Equal(t, expectedM, *z)
}
func TestTransposeMatrix(t *testing.T) {
expectedM := matrix.Matrix(
[][]int64{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
)
m := matrix.Matrix(
[][]int64{
{1, 4, 7},
{2, 5, 8},
{3, 6, 9},
},
)
m.Transpose()
assert.Equal(t, expectedM, m)
}
func benchmarckTranspose(r, c int, b *testing.B) {
for n := 0; n < b.N; n++ {
m := matrix.RandInt(r, c)
m.Transpose()
}
}
func BenchmarkTranspose3by3(b *testing.B) {
benchmarckTranspose(3, 3, b)
}
func BenchmarkTranspose5by5(b *testing.B) {
benchmarckTranspose(5, 5, b)
}
func BenchmarkTranspose10by10(b *testing.B) {
benchmarckTranspose(10, 10, b)
}
func TestMatrixIsEqualsToAnother(t *testing.T) {
a := matrix.RandInt(3, 3)
b := a.Copy()
assert.True(t, a.Equals(b))
}
func TestMatrixIsNotEqualsToAnother(t *testing.T) {
a := matrix.RandInt(3, 3)
b := a.Copy()
*b = (*b)[:b.Rows()-1]
assert.False(t, a.Equals(b))
}
func TestMatrixSum(t *testing.T) {
a := matrix.Matrix(
[][]int64{
{1, 2, 3},
{1, 2, 3},
},
)
b := matrix.Matrix(
[][]int64{
{1, 2, 3},
{1, 2, 3},
},
)
expected := matrix.Matrix(
[][]int64{
{2, 4, 6},
{2, 4, 6},
},
)
c, err := a.Sum(&b)
assert.Nil(t, err)
assert.Equal(t, &expected, c)
}
func TestMatrixSumWhenDifferentSizes(t *testing.T) {
a := matrix.RandInt(3, 3)
b := a.Copy()
*b = (*b)[1:]
c, err := a.Sum(b)
assert.Nil(t, c)
assert.Equal(t, matrix.ErrMatrixOfDifferentSizes, err)
}