-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtable_manipulation_test.go
230 lines (195 loc) · 5.95 KB
/
table_manipulation_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
package herschel
import "testing"
func TestSubTable(t *testing.T) {
orig := NewTable(3, 3)
orig.PutValuesAtRow(0, "a", "b", "c")
orig.PutValuesAtRow(1, "d", "e", "f")
orig.PutValuesAtRow(2, "g", "h", "i")
t.Run("SuccessfulSlicing", func(t *testing.T) {
s, err := orig.SubTable(1, 1, 1, 2)
if err != nil {
t.Fatal(err)
}
t.Run("SizeOfNewTable", func(t *testing.T) {
if s.rows != 1 || s.cols != 2 {
t.Errorf("Sub table should have dimension %dx%d, got: %dx%d", 1, 2, s.rows, s.cols)
}
})
t.Run("ValuesOfNewTable", func(t *testing.T) {
if s.GetValue(0, 0) != "e" {
t.Errorf("Value at %d, %d should be %s, got: %s", 0, 0, "e", s.GetValue(0, 0))
}
if s.GetValue(0, 1) != "f" {
t.Errorf("Value at %d, %d should be %s, got: %s", 0, 1, "f", s.GetValue(0, 1))
}
})
})
}
func TestSubTableByFilteringRows(t *testing.T) {
orig := NewTable(5, 3)
orig.PutValuesAtRow(0, "a", "b", "c")
orig.PutValuesAtRow(1, "d", "e", "f")
orig.PutValuesAtRow(2, "g", "h", "i")
orig.PutValuesAtRow(3, "j", "a", "b")
orig.PutValuesAtRow(4, "x", "x", "a")
st := orig.SubTableByFilteringRows(func(row []interface{}) bool {
for _, r := range row {
if r == "a" {
return true
}
}
return false
})
if st.GetRows() != 3 {
t.Errorf("SubTable should have 3 rows. Got %d rows.", st.GetRows())
}
}
func TestRemovingColumn(t *testing.T) {
orig := NewTable(5, 3)
orig.PutValuesAtRow(0, "a", "b", "c")
orig.PutValuesAtRow(1, "d", "e", "f")
orig.PutValuesAtRow(2, "g", "h", "i")
orig.PutValuesAtRow(3, "j", "a", "b")
orig.PutValuesAtRow(4, "x", "x", "a")
if orig.cols != 3 {
t.Fatalf("Unexpected number of columns. 3 expected, got: %d", orig.cols)
}
if err := orig.RemoveColAtIndex(1); err != nil {
t.Fatal(err)
}
expectedValues := []struct {
row int
col int
value string
}{
{0, 0, "a"},
{0, 1, "c"},
{1, 0, "d"},
{1, 1, "f"},
}
for _, expectedValue := range expectedValues {
actualValue := orig.GetValue(expectedValue.row, expectedValue.col)
if actualValue != expectedValue.value {
t.Errorf("Unexpected value at (%d,%d). %s expected, got: %s", expectedValue.row, expectedValue.col, expectedValue.value, actualValue)
}
}
}
func TestClearValues(t *testing.T) {
orig := NewTable(3, 3)
orig.PutValuesAtRow(0, "a", "b", "c")
orig.PutValuesAtRow(1, "d", "e", "f")
orig.PutValuesAtRow(2, "g", "h", "i")
if err := orig.ClearValues(); err != nil {
t.Fatal(err)
}
for row := 0; row < orig.rows; row++ {
for col := 0; col < orig.cols; col++ {
if value := orig.GetValue(row, col); value != "" {
t.Errorf("The value of cell at (%d,%d) is not empty.", row, col)
}
}
}
}
func TestClearValuesInRange(t *testing.T) {
orig := NewTable(5, 4)
orig.PutValuesAtRow(0, "a", "b", "c", "d")
orig.PutValuesAtRow(1, "e", "f", "g", "h")
orig.PutValuesAtRow(2, "i", "j", "k", "l")
orig.PutValuesAtRow(3, "m", "n", "o", "p")
orig.PutValuesAtRow(4, "q", "r", "s", "t")
if err := orig.ClearValuesInRange(1, 1, 3, 2); err != nil {
t.Fatal(err)
}
exp := NewTable(5, 4)
exp.PutValuesAtRow(0, "a", "b", "c", "d")
exp.PutValuesAtRow(1, "e", "", "", "h")
exp.PutValuesAtRow(2, "i", "", "", "l")
exp.PutValuesAtRow(3, "m", "", "", "p")
exp.PutValuesAtRow(4, "q", "r", "s", "t")
for row := 0; row < exp.rows; row++ {
for col := 0; col < exp.cols; col++ {
expectedValue := exp.GetValue(row, col)
actualValue := orig.GetValue(row, col)
if expectedValue != actualValue {
t.Errorf("The cell value of (% d,% d) is Unexpected.", row, col)
}
}
}
}
func TestInsertCol(t *testing.T) {
t.Run("First", func(t *testing.T) {
orig := NewTable(3, 2)
orig.PutValuesAtRow(0, "a", "b")
orig.PutValuesAtRow(1, "c", "d")
orig.PutValuesAtRow(2, "e", "f")
if err := orig.InsertColAtIndex(0); err != nil {
t.Fatal(err)
}
if orig.GetRows() != 3 || orig.GetCols() != 3 {
t.Fatalf("Table should have dimension of 3 x 3, got: %d x %d", orig.GetRows(), orig.GetCols())
}
exp := NewTable(3, 3)
exp.PutValuesAtRow(0, nil, "a", "b")
exp.PutValuesAtRow(1, nil, "c", "d")
exp.PutValuesAtRow(2, nil, "e", "f")
for row := 0; row < exp.rows; row++ {
for col := 0; col < exp.cols; col++ {
expectedValue := exp.GetValue(row, col)
actualValue := orig.GetValue(row, col)
if expectedValue != actualValue {
t.Errorf("The cell value of (% d,% d) is Unexpected.", row, col)
}
}
}
})
t.Run("Middle", func(t *testing.T) {
orig := NewTable(3, 2)
orig.PutValuesAtRow(0, "a", "b")
orig.PutValuesAtRow(1, "c", "d")
orig.PutValuesAtRow(2, "e", "f")
if err := orig.InsertColAtIndex(1); err != nil {
t.Fatal(err)
}
if orig.GetRows() != 3 || orig.GetCols() != 3 {
t.Fatalf("Table should have dimension of 3 x 3, got: %d x %d", orig.GetRows(), orig.GetCols())
}
exp := NewTable(3, 3)
exp.PutValuesAtRow(0, "a", nil, "b")
exp.PutValuesAtRow(1, "c", nil, "d")
exp.PutValuesAtRow(2, "e", nil, "f")
for row := 0; row < exp.rows; row++ {
for col := 0; col < exp.cols; col++ {
expectedValue := exp.GetValue(row, col)
actualValue := orig.GetValue(row, col)
if expectedValue != actualValue {
t.Errorf("The cell value of (% d,% d) is Unexpected.", row, col)
}
}
}
})
t.Run("Last", func(t *testing.T) {
orig := NewTable(3, 2)
orig.PutValuesAtRow(0, "a", "b")
orig.PutValuesAtRow(1, "c", "d")
orig.PutValuesAtRow(2, "e", "f")
if err := orig.InsertColAtIndex(2); err != nil {
t.Fatal(err)
}
if orig.GetRows() != 3 || orig.GetCols() != 3 {
t.Fatalf("Table should have dimension of 3 x 3, got: %d x %d", orig.GetRows(), orig.GetCols())
}
exp := NewTable(3, 3)
exp.PutValuesAtRow(0, "a", "b", nil)
exp.PutValuesAtRow(1, "c", "d", nil)
exp.PutValuesAtRow(2, "e", "f", nil)
for row := 0; row < exp.rows; row++ {
for col := 0; col < exp.cols; col++ {
expectedValue := exp.GetValue(row, col)
actualValue := orig.GetValue(row, col)
if expectedValue != actualValue {
t.Errorf("The cell value of (% d,% d) is Unexpected.", row, col)
}
}
}
})
}