-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_test.go
280 lines (236 loc) · 7.09 KB
/
array_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package godino
import (
"errors"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/maps"
)
func TestAppend(t *testing.T) {
arr := []int{1, 2, 3}
Append(&arr, 4)
assert.Equal(t, []int{1, 2, 3, 4}, arr)
}
func TestClearArray(t *testing.T) {
arr := []int{1, 2, 3}
Clear(&arr)
assert.Empty(t, arr)
}
func TestCopyArray(t *testing.T) {
arr := []int{1, 2, 3}
assert.Equal(t, Copy(arr), arr)
}
func TestContains(t *testing.T) {
arr := []int{1, 2, 3}
assert.True(t, Contains(arr, 3))
assert.False(t, Contains(arr, 0))
}
func TestEvery(t *testing.T) {
// Test case 1: Every element satisfies the condition
arr1 := []int{2, 4, 6, 8, 10}
every1 := Every(arr1, func(num int) bool {
return num%2 == 0
})
assert.True(t, every1, "failed to evaluate correctly for all elements satisfying the condition")
// Test case 2: Not every element satisfies the condition
arr2 := []int{2, 4, 6, 7, 10}
every2 := Every(arr2, func(num int) bool {
return num%2 == 0
})
assert.False(t, every2, "Everyfailed to evaluate correctly for not all elements satisfying the condition")
// Test case 3: Empty list
emptyArr := []int{}
arr3 := Every(emptyArr, func(num int) bool {
return num%2 == 0
})
assert.True(t, arr3, "failed to evaluate correctly for an empty list")
}
func TestExtend(t *testing.T) {
arr := []int{1, 2, 3}
Extend(&arr, []int{4, 5, 6})
assert.Equal(t, []int{1, 2, 3, 4, 5, 6}, arr)
}
func TestForEach(t *testing.T) {
arr := []int{1, 2, 3}
num := 0
f := func(i int, n int) {
num += n + i
}
ForEach(arr, f)
assert.Equal(t, 9, num)
}
func TestForEachRef(t *testing.T) {
arr := []int{1, 2, 3}
f := func(i int, n *int) {
*n += i
}
ForEachRef(arr, f)
assert.Equal(t, []int{1, 3, 5}, arr)
}
func TestFilter(t *testing.T) {
arr := []int{1, 2, 3, 4, 5}
filtered := Filter(arr, func(num int) bool {
return num%2 == 0
})
expected := []int{2, 4}
assert.Equal(t, expected, filtered)
}
func TestFind(t *testing.T) {
arr := []int{1, 2, 3, 4, 5}
t.Run("should find value", func(t *testing.T) {
value, found := Find(arr, func(num int) bool {
return num%2 == 0
})
assert.True(t, found, "failed to find value")
assert.Equal(t, 2, value, "returned incorrect value")
})
t.Run("should not find value", func(t *testing.T) {
value, found := Find(arr, func(num int) bool {
return num > 10
})
assert.False(t, found, "incorrectly found a value")
assert.Equal(t, 0, value, "returned non-zero value for non-existing condition")
})
}
func TestIndex(t *testing.T) {
t.Run("should return value", func(t *testing.T) {
arr := []int{1, 2, 3, 4, 5}
index := Index(arr, 3)
expectedIndex := 2
assert.Equal(t, expectedIndex, index)
})
t.Run("should return -1 (value is not present)", func(t *testing.T) {
arr := []int{1, 2, 3, 4, 5}
index := Index(arr, 6)
expectedIndex := -1
assert.Equal(t, expectedIndex, index)
})
}
func TestInsert(t *testing.T) {
arr := []int{1, 2, 3}
Insert(&arr, 0, 0)
expected := []int{0, 1, 2, 3}
assert.Equal(t, expected, arr)
Insert(&arr, 99, 2)
expected = []int{0, 1, 99, 2, 3}
assert.Equal(t, expected, arr)
Insert(&arr, 100, len(arr))
expected = []int{0, 1, 99, 2, 3, 100}
assert.Equal(t, expected, arr)
}
func TestMap(t *testing.T) {
arr := []int{1, 2, 3, 4, 5}
mapped := Map(arr, func(num int) int {
return num * num
})
expected := []int{1, 4, 9, 16, 25}
assert.Equal(t, expected, mapped, "failed to map numbers to their squares")
mappedString := Map(arr, func(num int) string {
return "Number: " + strconv.Itoa(num)
})
expectedStrings := []string{"Number: 1", "Number: 2", "Number: 3", "Number: 4", "Number: 5"}
assert.Equal(t, expectedStrings, mappedString, "failed to map numbers to strings")
}
func TestMembersEqual(t *testing.T) {
arr1 := []int{1, 2, 3, 4, 3, 2, 1}
arr2 := []int{1, 1, 2, 2, 3, 3, 4}
assert.True(t, MembersMatch(arr1, arr2))
arr2 = append(arr2, 4)
assert.False(t, MembersMatch(arr1, arr2))
}
func TestPopArray(t *testing.T) {
arr := []int{1, 2, 3}
popped := Pop(&arr)
expectedItem := 3
assert.Equal(t, expectedItem, popped, "Failed to return value")
expected := []int{1, 2}
assert.Equal(t, expected, arr, "Failed to modify list")
}
func TestReduce(t *testing.T) {
arr := []int{1, 2, 3, 4, 5}
sum := Reduce(arr, func(acc, num int) int {
return acc + num
}, 0)
expectedSum := 15
assert.Equal(t, expectedSum, sum, "failed to calculate the sum")
product := Reduce(arr, func(acc, num int) int {
return acc * num
}, 1)
expectedProduct := 120
assert.Equal(t, expectedProduct, product, "failed to calculate the product")
m := Reduce(arr, func(acc map[int]int, num int) map[int]int {
acc[num] = num * num
return acc
}, make(map[int]int))
expectedMap := map[int]int{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
assert.True(t, maps.Equal(m, expectedMap), "failed to make square map")
}
func TestReverse(t *testing.T) {
arr := []int{1, 2, 3, 4}
Reverse(&arr)
expected := []int{4, 3, 2, 1}
assert.Equal(t, expected, arr)
}
func TestShift(t *testing.T) {
arr := []int{1, 2, 3}
shifted := Shift(&arr)
expectedItem := 1
assert.Equal(t, expectedItem, shifted, "Failed to return value")
expected := []int{2, 3}
assert.Equal(t, expected, arr, "Failed to modify list")
}
func TestSome(t *testing.T) {
// Test case 1: Some elements satisfy the condition
arr1 := []int{1, 2, 3, 4, 5}
some1 := Some(arr1, func(num int) bool {
return num%2 == 0
})
assert.True(t, some1, "failed to evaluate correctly for some elements satisfying the condition")
// Test case 2: No element satisfies the condition
arr2 := []int{1, 3, 5, 7, 9}
some2 := Some(arr2, func(num int) bool {
return num%2 == 0
})
assert.False(t, some2, "failed to evaluate correctly for no elements satisfying the condition")
// Test case 3: Empty list
emptyArr := []int{}
some3 := Some(emptyArr, func(num int) bool {
return num%2 == 0
})
assert.False(t, some3, "failed to evaluate correctly for an empty list")
}
func TestValueAt(t *testing.T) {
t.Run("should return the value at the given index", func(t *testing.T) {
arr := []int{1, 2, 3}
item := ValueAt(arr, 2)
assert.Equal(t, 3, item)
})
t.Run("should support negative index", func(t *testing.T) {
arr := []int{1, 2, 3}
item := ValueAt(arr, -2)
assert.Equal(t, 2, item)
})
}
func TestZip(t *testing.T) {
// Test case 1: Valid input slices
slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}
slice3 := []int{7, 8, 9}
expectedResult := [][]int{
{1, 4, 7},
{2, 5, 8},
{3, 6, 9},
}
result, err := Zip[[]int, int](slice1, slice2, slice3)
assert.Nil(t, err, "Zip() returned an unexpected error")
assert.Equal(t, expectedResult, result, "Zip() returned an incorrect result")
// Test case 2: Slices with different lengths
slice4 := []int{10, 11}
_, err = Zip[[]int, int](slice1, slice2, slice3, slice4)
expectedError := errors.New("Zip() received slices of different lengths")
assert.EqualError(t, err, expectedError.Error(), "Zip() did not return the expected error")
// Test case 3: No input slices
_, err = Zip[[]int, int]()
expectedError = errors.New("Zip() expected at least 1 argument but got 0")
assert.EqualError(t, err, expectedError.Error(), "Zip() did not return the expected error")
}