-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsparseBinaryMatrix_test.go
163 lines (121 loc) · 3.08 KB
/
sparseBinaryMatrix_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
package htm
import (
"github.com/stretchr/testify/assert"
"testing"
)
//Tests getting/setting values
func TestGetSet(t *testing.T) {
sm := NewSparseBinaryMatrix(10, 10)
sm.Set(2, 4, true)
sm.Set(6, 5, true)
sm.Set(7, 5, false)
if !sm.Get(2, 4) {
t.Errorf("Was false expected true @ [2,4]")
}
if !sm.Get(6, 5) {
t.Errorf("Was false expected true @ [6,5]")
}
if sm.Get(7, 5) {
t.Errorf("Was true expected false @ [7,5]")
}
}
func TestRowReplace(t *testing.T) {
sm := NewSparseBinaryMatrix(10, 10)
sm.Set(2, 4, true)
sm.Set(6, 5, true)
sm.Set(7, 5, true)
sm.Set(8, 8, true)
if !sm.Get(8, 8) {
t.Errorf("Was false expected true @ [8,8]")
}
newRow := make([]bool, 10)
newRow[6] = true
sm.ReplaceRow(8, newRow)
if !sm.Get(8, 6) {
t.Errorf("Was false expected true @ [8,6]")
}
if sm.Get(8, 8) {
t.Errorf("Was true expected false @ [8,8]")
}
}
func TestReplaceRowByIndices(t *testing.T) {
sm := NewSparseBinaryMatrix(10, 10)
indices := make([]int, 3)
indices[0] = 3
indices[1] = 9
indices[2] = 6
sm.ReplaceRowByIndices(4, indices)
if !sm.Get(4, 3) {
t.Errorf("Was false expected true @ [4,3]")
}
if !sm.Get(4, 9) {
t.Errorf("Was false expected true @ [4,9]")
}
if !sm.Get(4, 6) {
t.Errorf("Was false expected true @ [4,6]")
}
if sm.Get(4, 5) {
t.Errorf("Was true expected false @ [4,5]")
}
if sm.Get(4, 0) {
t.Errorf("Was true expected false @ [4,0]")
}
indices = make([]int, 3)
indices[0] = 4
sm.ReplaceRowByIndices(4, indices)
if sm.Get(4, 3) {
t.Errorf("Was true expected false @ [4,3]")
}
if sm.Get(4, 9) {
t.Errorf("Was true expected false @ [4,9]")
}
if !sm.Get(4, 4) {
t.Errorf("Was false expected true @ [4,4]")
}
}
func TestGetRowIndices(t *testing.T) {
sm := NewSparseBinaryMatrix(10, 10)
indices := make([]int, 3)
indices[0] = 3
indices[1] = 6
indices[2] = 9
sm.ReplaceRowByIndices(4, indices)
indResult := sm.GetRowIndices(4)
if len(indResult) != len(indices) {
t.Errorf("Len was %v expected %v", len(indResult), len(indices))
}
for i := 0; i < 3; i++ {
if indResult[i] != indices[i] {
t.Errorf("Was %v expected %v", indResult, indices)
}
}
}
func TestGetRowAndSum(t *testing.T) {
sm := NewSparseBinaryMatrix(4, 5)
sm.SetRowFromDense(0, []bool{true, false, true, true, false})
sm.SetRowFromDense(1, []bool{false, false, false, true, false})
sm.SetRowFromDense(2, []bool{false, false, false, false, false})
sm.SetRowFromDense(3, []bool{true, true, true, true, true})
t.Log(sm.ToString())
t.Log(sm.Entries())
i := []bool{true, false, true, true, false}
result := sm.RowAndSum(i)
assert.Equal(t, 3, result[0])
assert.Equal(t, 1, result[1])
assert.Equal(t, 0, result[2])
assert.Equal(t, 3, result[3])
}
func TestSetRowFromDense(t *testing.T) {
}
func TestNewFromDense(t *testing.T) {
sbm := NewSparseBinaryMatrixFromDense([][]bool{
{true, true, true},
{false, false, false},
{false, true, false},
{true, false, true},
})
assert.Equal(t, 4, sbm.Height)
assert.Equal(t, 3, sbm.Width)
assert.Equal(t, true, sbm.Get(3, 2))
assert.Equal(t, []bool{false, true, false}, sbm.GetDenseRow(2))
}