-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_test.go
408 lines (358 loc) · 13 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package array
import (
"fmt"
"reflect"
"testing"
)
// TestContain test the function Contain with array of type int, float, string and boolean
func TestContains(t *testing.T) {
arr := []any{1, 2, 3.3, 4.4, "five", "six", true, false}
// Test int
check1 := Contains(arr, 1)
check2 := Contains(arr, 2)
check3 := Contains(arr, 3)
if check1 == true && check2 == true && check3 == false {
fmt.Println("Yes (1/4) | Array Find is set successfully for int")
} else {
t.Error("No (1/4) | Array Find isn't set successfully for int")
}
// Test float
check1 = Contains(arr, 3.3)
check2 = Contains(arr, 4.4)
check3 = Contains(arr, 5.5)
if check1 == true && check2 == true && check3 == false {
fmt.Println("Yes (2/4) | Array Find is set successfully for float64")
} else {
t.Error("No (2/4) | Array Find isn't set successfully for float64")
}
// Test string
check1 = Contains(arr, "five")
check2 = Contains(arr, "six")
check3 = Contains(arr, "seven")
if check1 == true && check2 == true && check3 == false {
fmt.Println("Yes (3/4) | Array Find is set successfully for string")
} else {
t.Error("No (3/4) | Array Find isn't set successfully for string")
}
// Test boolean
check1 = Contains(arr, true)
check2 = Contains(arr, false)
if check1 == true && check2 == true {
fmt.Println("Yes (4/4) | Array Find is set successfully for boolean")
} else {
t.Error("No (4/4) | Array Find isn't set successfully for boolean")
}
}
// TestFind test the function Find with array of type int, float, string and boolean
func TestFind(t *testing.T) {
arr := []any{1, 2, 3.3, 4.4, "five", "six", true, false}
// Test int
index1 := Find(arr, 1)
index2 := Find(arr, 2)
if index1 == 0 && index2 == 1 {
fmt.Println("Yes (1/5) | Array Find is set successfully for int")
} else {
t.Error("No (1/5) | Array Find isn't set successfully for int")
}
// Test float
index1 = Find(arr, 3.3)
index2 = Find(arr, 4.4)
if index1 == 2 && index2 == 3 {
fmt.Println("Yes (2/5) | Array Find is set successfully for float64")
} else {
t.Error("No (2/5) | Array Find isn't set successfully for float64")
}
// Test string
index1 = Find(arr, "five")
index2 = Find(arr, "six")
if index1 == 4 && index2 == 5 {
fmt.Println("Yes (3/5) | Array Find is set successfully for string")
} else {
t.Error("No (3/5) | Array Find isn't set successfully for string")
}
// Test boolean
index1 = Find(arr, true)
index2 = Find(arr, false)
if index1 == 6 && index2 == 7 {
fmt.Println("Yes (4/5) | Array Find is set successfully for boolean")
} else {
t.Error("No (4/5) | Array Find isn't set successfully for boolean")
}
// Test index out of range
index1 = Find(arr, 8)
if index1 == -1 {
fmt.Println("Yes (5/5) | Array Find is set successfully for index out of range")
} else {
t.Error("No (5/5) | Array Find isn't set successfully for index out of range")
}
}
// TestIsEqual test the function IsEqual with array of type int, float64, string and boolean
func TestIsEqual(t *testing.T) {
arrOne := []any{1, 2, 3.3, 4.4, "five", "six", true, false}
arrTwo := []any{1, 2, 3.3, 4.4, "five", "six", true, false}
arrThree := []any{1, 2, 3, 4, 5, 6}
arrFour := []any{2, 1, 3.3, 4.4, "five", "six", false, true}
equal := IsEqual(arrOne, arrTwo)
if equal {
fmt.Println("Yes (1/2) | Array IsEqual is set successfully when same")
} else {
t.Error("No (1/2) | Array IsEqual isn't set successfully when same")
}
equal1 := IsEqual(arrOne, arrThree)
equal2 := IsEqual(arrOne, arrFour)
if !equal1 && !equal2 {
fmt.Println("Yes (2/2) | Array IsEqual is set successfully when different")
} else {
t.Error("No (2/2) | Array IsEqual isn't set successfully when different")
}
}
// TestMax test the function Max with array of type int, float64 and string (number of char in the string)
func TestMax(t *testing.T) {
arrInt := []int{1, 20, 300, 44, 56}
arrFloat := []float64{100.1, 20.2, 3.346598, 458.9, 5.5}
arrString := []string{"one", "two", "three", "fours", "five"}
var arrEmpty []int
// Test int
intMax, _ := Max(arrInt)
if intMax == 300 {
fmt.Println("Yes (1/4) | Array Max is set successfully for int")
} else {
t.Error("No (1/4) | Array Max isn't set successfully for int")
}
// Test float
float64Max, _ := Max(arrFloat)
if float64Max == 458.9 {
fmt.Println("Yes (2/4) | Array Max is set successfully for float64")
} else {
t.Error("No (2/4) | Array Max isn't set successfully for float64")
}
// Test string
stringMax, _ := Max(arrString)
if stringMax == "three" {
fmt.Println("Yes (3/4) | Array Max is set successfully for string")
} else {
t.Error("No (3/4) | Array Max isn't set successfully for string")
}
// Test empty array
_, err := Max(arrEmpty)
if err != nil {
fmt.Println("Yes (4/4) | Array Max is set successfully for NaN")
} else {
t.Error("No (4/4) | Array Max isn't set successfully for NaN")
}
}
// TestMin test the function Min with array of type int, float64 and string (number of char in the string)
func TestMin(t *testing.T) {
arrInt := []int{1, 20, 300, 44, 56}
arrFloat := []float64{100.1, 20.2, 3.346598, 458.9, 5.5}
arrString := []string{"zero", "one", "two", "three", "four", "five"}
var arrEmpty []int
// Test int
intMin, _ := Min(arrInt)
if intMin == 1 {
fmt.Println("Yes (1/4) | Array Min is set successfully for int")
} else {
t.Error("No (1/4) | Array Min isn't set successfully for int")
}
// Test float
float64Min, _ := Min(arrFloat)
if float64Min == 3.346598 {
fmt.Println("Yes (2/4) | Array Min is set successfully for float64")
} else {
t.Error("No (2/4) | Array Min isn't set successfully for float64")
}
// Test string
stringMin, _ := Min(arrString)
if stringMin == "one" {
fmt.Println("Yes (3/4) | Array Min is set successfully for string")
} else {
t.Error("No (3/4) | Array Min isn't set successfully for string")
}
// Test empty array
_, err := Min(arrEmpty)
if err != nil {
fmt.Println("Yes (4/4) | Array Min is set successfully for NaN")
} else {
t.Error("No (4/4) | Array Min isn't set successfully for NaN")
}
}
// TestRemove test the function Remove with array of type int, float64, string and boolean
func TestRemove(t *testing.T) {
arrInt := []int{1, 2, 3, 4, 5}
arrFloat := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
arrString := []string{"one", "two", "three", "four", "five"}
arrBool := []bool{true, false, true, false, true}
arrAny := []any{1, 2, "a", true}
arrInt = Remove(arrInt, 2)
arrFloat = Remove(arrFloat, 2)
arrString = Remove(arrString, 2)
arrBool = Remove(arrBool, 2)
arrAny = Remove(arrAny, 1)
newArrInt := Remove(arrInt, -1)
newArrFloat := Remove(arrFloat, -1)
newArrString := Remove(arrString, -1)
newArrBool := Remove(arrBool, -1)
newArrAny := Remove(arrAny, -1)
// Test array int
if arrInt[2] == 4 {
fmt.Println("Yes (1/10) | Array Remove is set successfully for int")
} else {
t.Error("No (1/10) | Array Remove isn't set successfully for int")
}
if reflect.DeepEqual(newArrInt, arrInt) {
fmt.Println("Yes (2/10) | Array Remove is set successfully for wrong index")
} else {
t.Error("No (2/10) | Array Remove isn't set successfully for wrong index")
}
// Test array float32
if arrFloat[2] == 4.4 {
fmt.Println("Yes (3/10) | Array Remove is set successfully for float64")
} else {
t.Error("No (3/10) | Array Remove isn't set successfully for float64")
}
if reflect.DeepEqual(newArrFloat, arrFloat) {
fmt.Println("Yes (4/10) | Array Remove is set successfully for wrong index")
} else {
t.Error("No (4/10) | Array Remove isn't set successfully for wrong index")
}
// Test array string
if arrString[2] == "four" {
fmt.Println("Yes (5/10) | Array Remove is set successfully for string")
} else {
t.Error("No (5/10) | Array Remove isn't set successfully for string")
}
if reflect.DeepEqual(newArrString, arrString) {
fmt.Println("Yes (6/10) | Array Remove is set successfully for wrong index")
} else {
t.Error("No (6/10) | Array Remove isn't set successfully for wrong index")
}
// Test array bool
if arrBool[2] == false {
fmt.Println("Yes (7/10) | Array Remove is set successfully for boolean")
} else {
t.Error("No (7/10) | Array Remove isn't set successfully for boolean")
}
if reflect.DeepEqual(newArrBool, arrBool) {
fmt.Println("Yes (8/10) | Array Remove is set successfully for wrong index")
} else {
t.Error("No (8/10) | Array Remove isn't set successfully for wrong index")
}
// Test array any
if arrAny[1] == "a" {
fmt.Println("Yes (9/10) | Array Remove is set successfully for any")
} else {
t.Error("No (9/10) | Array Remove isn't set successfully for any")
}
if reflect.DeepEqual(newArrAny, arrAny) {
fmt.Println("Yes (10/10) | Array Remove is set successfully for wrong index")
} else {
t.Error("No (10/10) | Array Remove isn't set successfully for wrong index")
}
}
// TestSlice test the function Slice with array of type int, float64, string and boolean
func TestSlice(t *testing.T) {
arrInt := []any{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
arrIntExpected := []any{8, 7, 6, 5, 4}
arrFloat := []any{9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0}
arrFloatExpected := []any{9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2}
arrString := []any{"eeeee", "dddd", "ccc", "bb", "a"}
arrStringExpected := []any{"dddd", "ccc", "bb"}
arrBool := []any{true, false, true, true, false, false, true}
arrBoolExpected := []any{false, false, true}
arrInt, _ = Slice(arrInt, 1, 5)
arrFloat, _ = Slice(arrFloat, -10, 7)
arrString, _ = Slice(arrString, 1, 3)
arrBool, _ = Slice(arrBool, 4, 50)
_, errSlice := Slice(arrInt, 4, 1)
equal := IsEqual(arrInt, arrIntExpected)
if equal {
fmt.Println("Yes (1/5) | Array Slice is set successfully for int")
} else {
t.Error("No (1/5) | Array Slice isn't set successfully for int")
}
equal = IsEqual(arrFloat, arrFloatExpected)
if equal {
fmt.Println("Yes (2/5) | Array Slice is set successfully for float64")
} else {
t.Error("No (2/5) | Array Slice isn't set successfully for float64")
}
equal = IsEqual(arrString, arrStringExpected)
if equal {
fmt.Println("Yes (3/5) | Array Slice is set successfully for string")
} else {
t.Error("No (3/5) | Array Slice isn't set successfully for string")
}
equal = IsEqual(arrBool, arrBoolExpected)
if equal {
fmt.Println("Yes (4/5) | Array Slice is set successfully for boolean")
} else {
t.Error("No (4/5) | Array Slice isn't set successfully for boolean")
}
if errSlice != nil {
fmt.Println("Yes (5/5) | Array Slice is set successfully for wrong start and end values")
} else {
t.Error("No (5/5) | Array Slice isn't set successfully for wrong start and end values")
}
}
// TestSort test the function Sort with array of type int, float64 and string
func TestSort(t *testing.T) {
arrInt := []int{3, 6, 9, 2, 5, 8, 0, 1, 4, 7}
arrIntExpectedAscending := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
arrIntExpectedDescending := []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
arrFloat := []float64{3.3, 6.6, 9.9, 2.2, 5.5, 8.8, 0.0, 1.1, 4.4, 7.7}
arrFloatExpectedAscending := []float64{0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}
arrFloatExpectedDescending := []float64{9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0}
arrString := []string{"a", "ccc", "bb", "eeeee", "dddd"}
arrStringExpectedAscending := []string{"a", "bb", "ccc", "dddd", "eeeee"}
arrStringExpectedDescending := []string{"eeeee", "dddd", "ccc", "bb", "a"}
// Test ascending order
Sort(arrInt, 0)
Sort(arrFloat, 0)
Sort(arrString, 0)
// Test array int
equal := IsEqual(arrInt, arrIntExpectedAscending)
if equal {
fmt.Println("Yes (1/6) | Array Sort is set successfully for int in ascending order")
} else {
t.Error("No (1/6) | Array SortAsc isn't set successfully for int in ascending order")
}
// Test array float64
equal = IsEqual(arrFloat, arrFloatExpectedAscending)
if equal {
fmt.Println("Yes (2/6) | Array SortAsc is set successfully for float64 in ascending order")
} else {
t.Error("No (2/6) | Array SortAsc isn't set successfully for float64 in ascending order")
}
// Test array string
equal = IsEqual(arrString, arrStringExpectedAscending)
if equal {
fmt.Println("Yes (3/6) | Array SortAsc is set successfully for string in ascending order")
} else {
t.Error("No (3/6) | Array SortAsc isn't set successfully for string in ascending order")
}
// Test descending order
Sort(arrInt, 1)
Sort(arrFloat, 1)
Sort(arrString, 1)
// Test array int
equal = IsEqual(arrInt, arrIntExpectedDescending)
if equal {
fmt.Println("Yes (4/6) | Array Sort is set successfully for int in descending order")
} else {
t.Error("No (4/6) | Array Sort isn't set successfully for int in descending order")
}
// Test array float64
equal = IsEqual(arrFloat, arrFloatExpectedDescending)
if equal {
fmt.Println("Yes (5/6) | Array Sort is set successfully for float64 in descending order")
} else {
t.Error("No (5/6) | Array Sort isn't set successfully for float64 in descending order")
}
// Test array string
equal = IsEqual(arrString, arrStringExpectedDescending)
if equal {
fmt.Println("Yes (6/6) | Array Sort is set successfully for string in descending order")
} else {
t.Error("No (6/6) | Array Sort isn't set successfully for string in descending order")
}
}