-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_test.go
178 lines (167 loc) · 4.45 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
package collections
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestShape(t *testing.T) {
cases := []struct {
matrix Matrix
wantRows, wantCols int
}{
{
Matrix{Row{1.0, 1.0, 1.0}, Row{0.0, 2.0, 1.0}, Row{-1.0, 0.0, 1.0}},
3,
3,
},
{
Matrix{Row{1.0, 1.0}, Row{0.0, 2.0}, Row{-1.0, 0.0}},
3,
2,
},
}
for _, c := range cases {
gotRows, gotCols := c.matrix.Shape()
if gotRows != c.wantRows {
t.Errorf("Shape(%v) want_rows: %v; got_rows: %v",
c.matrix, c.wantRows, gotRows)
}
if gotCols != c.wantCols {
t.Errorf("Shape(%v) want_cols: %v; got_cols: %v",
c.matrix, c.wantCols, gotCols)
}
}
}
func TestShape_panicWhenTheSizeOfRowsAreDifferents(t *testing.T) {
matrix := Matrix{Row{1.0, 2.0, 3.0}, Row{1.0, 2.0, 3.0}, Row{1.0, 2.0}}
defer func() {
if r := recover(); r == nil {
t.Errorf("Shape(%v) did not panic.", matrix)
} else if !strings.HasPrefix(fmt.Sprint(r), "The dimentions are differents:") {
t.Errorf("Shape(%v): unexpected panic %q", matrix, r)
}
}()
matrix.Shape()
}
func TestRowAt(t *testing.T) {
cases := []struct {
matrix Matrix
position int
want Row
}{
{
Matrix{
Row{1.0, 1.0, 1.0},
Row{0.0, 2.0, 1.0},
Row{-1.0, 0.0, 1.0},
},
2,
Row{0.0, 2.0, 1.0},
},
{
Matrix{Row{1.0, 2.0, 3.0}},
1,
Row{1.0, 2.0, 3.0},
},
}
for _, c := range cases {
got := c.matrix.RowAt(c.position)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("%v.RowAt(%v) want: %v; got: %v", c.matrix, c.position, c.want,
got)
}
}
}
func TestRowAt_panicWhenTheRowDoesNotExists(t *testing.T) {
matrix := Matrix{Row{1.0, 2.0, 3.0}}
position := 2
defer func() {
if r := recover(); r == nil {
t.Errorf("%v.RowAt(%v) dit not panic", matrix, position)
} else if !strings.HasPrefix(fmt.Sprint(r), "Does not exist row in position") {
t.Errorf("%v.RowAt(%v): unexpected panic %q", matrix, position, r)
}
}()
matrix.RowAt(position)
}
func TestRow_panicWhenTheMatrixIsWrong(t *testing.T) {
matrix := Matrix{Row{1.0}, Row{1.0, 1.0}}
column := 0
defer func() {
if r := recover(); r == nil {
t.Errorf("%v.RowAt(%v) did not panic.", matrix, column)
} else if !strings.HasPrefix(fmt.Sprint(r), "The dimentions are differents:") {
t.Errorf("%v.RowAt(%v): unexpected panic %q", matrix, column, r)
}
}()
matrix.RowAt(column)
}
func TestColumnAt(t *testing.T) {
cases := []struct {
matrix Matrix
col int
want Vector
}{
{Matrix{Row{1.0, 1.0}, {0.0, 0.0}}, 2, Vector{1.0, 0.0}},
{Matrix{Row{1.0, 1.0}, {0.0, 0.0}, {3.0, 2.0}}, 1, Vector{1.0, 0.0, 3.0}},
}
for _, c := range cases {
got := c.matrix.ColumnAt(c.col)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("%v.ColumnAt(%v) want: %v; got: %v", c.matrix, c.col, c.want, got)
}
}
}
func TestColumnAt_panicWhenTheColumnDoesNotExist(t *testing.T) {
matrix := Matrix{Row{1.0, 1.0}}
column := 3
defer func() {
if r := recover(); r == nil {
t.Errorf("%v.ColumnAt(%v) did not panic.", matrix, column)
} else if !strings.HasPrefix(fmt.Sprint(r), "Does not exist column in position") {
t.Errorf("%v.ColumnAt(%v): unexpected panic %q", matrix, column, r)
}
}()
matrix.ColumnAt(column)
}
func TestColumn_panicWhenTheMatrixIsWrong(t *testing.T) {
matrix := Matrix{Row{1.0}, Row{1.0, 1.0}}
column := 0
defer func() {
if r := recover(); r == nil {
t.Errorf("%v.ColumnAt(%v) did not panic.", matrix, column)
} else if !strings.HasPrefix(fmt.Sprint(r), "The dimentions are differents:") {
t.Errorf("%v.ColumnAt(%v): unexpected panic %q", matrix, column, r)
}
}()
matrix.ColumnAt(column)
}
func TestMakeMatrixDiagonal(t *testing.T) {
cases := []struct {
numRows int
numCols int
want Matrix
}{
{2, 2, Matrix{Row{1.0, 0.0}, Row{0.0, 1.0}}},
{3, 3, Matrix{Row{1.0, 0.0, 0.0}, Row{0.0, 1.0, 0.0}, Row{0.0, 0.0, 1.0}}},
}
for _, c := range cases {
got := MakeMatrixDiagonal(c.numRows, c.numCols)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("MakeMatrixDiagonal(%v, %v) want: %v; got: %v",
c.numRows, c.numCols, c.want, got)
}
}
}
func TestMakeMatrixDiagonal_panicWhenNumRowAndNumColsAreDifferents(t *testing.T) {
numRows, numCols := 2, 3
defer func() {
if r := recover(); r == nil {
t.Errorf("MakeMatrixDiagonal(%v, %v) did not panic.", numRows, numCols)
} else if !strings.HasPrefix(fmt.Sprint(r), "The dimentions are differents:") {
t.Errorf("MakeMatrixDiagonal(%v, %v): unexpected panic %q", numRows, numCols, r)
}
}()
MakeMatrixDiagonal(numRows, numCols)
}