-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_test.go
170 lines (153 loc) · 4.22 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
package matrix
import (
"errors"
"reflect"
"testing"
)
func TestRow(t *testing.T) {
var matrix *Matrix
matrix = New(7, 12, nil)
r := matrix.Row()
if r != 7 {
t.Errorf("want %v got %v", 7, r)
}
}
func TestColumn(t *testing.T) {
var matrix *Matrix
matrix = New(7, 12, nil)
c := matrix.Column()
if c != 12 {
t.Errorf("want %v got %v", 12, c)
}
}
func TestSize(t *testing.T) {
var matrix *Matrix
matrix = New(2, 3, nil)
r, c := matrix.Size()
if r != 2 || c != 3 {
t.Errorf("want %v %v got %v %v", 2, 3, r, c)
}
}
func ExampleShow() {
var matrix *Matrix
matrix = &Matrix{2, 3, []float64{1, 2, 3, 4, 5, 6}, nil}
matrix.Show()
matrix = &Matrix{3, 2, []float64{1.1, 2.1234, 3.12345, 4.123456, 5.123455, 0.6}, nil}
matrix.err = newError("Something happen", "Show", matrix, nil)
matrix.Show()
// Output:
// size: 2 x 3
// 1.00000 2.00000 3.00000
// 4.00000 5.00000 6.00000
//
// size: 3 x 2
// 1.10000 2.12340
// 3.12345 4.12346
// 5.12345 0.60000
// Size: opt1: (3, 2), opt2: No matrix
// [function: Show] Something happen
}
func TestErr(t *testing.T) {
var matrix *Matrix
matrix = New(1, 2, nil)
matrix.err = errors.New("error")
if matrix.Err() == nil {
t.Errorf("Should get error but got nil")
}
}
func TestAt(t *testing.T) {
var err error
var matrix *Matrix
matrix = New(2, 3, nil)
matrix.matrix = []float64{1, 2, 3, 4, 5, 6}
count := 0
for i := 1; i <= 2; i++ {
for j := 1; j <= 3; j++ {
if val, _ := matrix.At(i, j); val != matrix.matrix[count] {
t.Errorf("At(%v, %v) should be %v but got %v", i, j, count, val)
}
count++
}
}
_, err = matrix.At(2, 4)
if err == nil {
t.Errorf("At(2, 4) is out of range thus should return error")
}
_, err = matrix.At(3, 3)
if err == nil {
t.Errorf("At(3, 3) is out of range thus should return error")
}
}
func TestSet(t *testing.T) {
var err error
var matrix *Matrix
var answer *Matrix
matrix = New(2, 3, nil)
answer = New(2, 3, nil)
answer.matrix = []float64{1, 2, 3, 4, 5, 6}
count := 0
for i := 1; i <= 2; i++ {
for j := 1; j <= 3; j++ {
count++
matrix.Set(i, j, float64(count))
}
}
if !reflect.DeepEqual(answer, matrix) {
t.Errorf("want %#v got %#v", answer, matrix)
}
err = matrix.Set(2, 4, 1)
if err == nil {
t.Errorf("At(2, 4) is out of range thus should return error")
}
err = matrix.Set(3, 3, 1)
if err == nil {
t.Errorf("At(3, 3) is out of range thus should return error")
}
}
func TestSetMatrix(t *testing.T) {
var matrix *Matrix
var answer *Matrix
matrix = createUniformMatrix(2, 3, 4)
answer = createUniformMatrix(3, 5, 6)
matrix.SetMatrix(answer)
if matrix.Err() != nil {
t.Errorf("Should be error nil but got %v", matrix.Err())
}
if !reflect.DeepEqual(answer, matrix) {
t.Errorf("want %#v got %#v", answer, matrix)
}
answer = &Matrix{-1, 2, []float64{}, nil}
matrix.SetMatrix(answer)
if matrix.Err() == nil {
t.Errorf("Should get error but got nil")
}
}
func TestInt(t *testing.T) {
var matrix *Matrix
var answer *Matrix
matrix = New(3, 3, []float64{0.4, 0.5, 0.6, 1.4, 1.5, 1.6, 2.4, 2.5, 2.9})
answer = New(3, 3, []float64{0, 0, 0, 1, 1, 1, 2, 2, 2})
matrix = matrix.Int()
if !reflect.DeepEqual(answer, matrix) {
t.Errorf("want %#v got %#v", answer, matrix)
}
}
func TestTextAsOneLine(t *testing.T) {
var matrix *Matrix
var text string
matrix = New(4, 3, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
text = matrix.TextAsOneLine(",")
if text != "1.000000,2.000000,3.000000,4.000000,5.000000,6.000000,7.000000,8.000000,9.000000,10.000000,11.000000,12.000000" {
t.Errorf("want %q got %q", "1.000000,2.000000,3.000000,4.000000,5.000000,6.000000,7.000000,8.000000,9.000000,10.000000,11.000000,12.000000", text)
}
matrix = New(4, 1, []float64{1, 2, 3, 4})
text = matrix.TextAsOneLine(",")
if text != "1.000000,2.000000,3.000000,4.000000" {
t.Errorf("want %q got %q", "1.000000,2.000000,3.000000,4.000000", text)
}
matrix = New(4, 3, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
text = matrix.TextAsOneLine(" ")
if text != "1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 11.000000 12.000000" {
t.Errorf("want %q got %q", "1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 11.000000 12.000000", text)
}
}