-
Notifications
You must be signed in to change notification settings - Fork 32
/
slice_test.go
73 lines (66 loc) · 1.57 KB
/
slice_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
package core_test
import (
"fmt"
"github.com/synapsecns/sanguine/core"
"reflect"
"testing"
)
func TestRandomItem(t *testing.T) {
intSlice := []int{1, 2, 3, 4, 5}
stringSlice := []string{"apple", "banana", "cherry"}
randInt, err := core.RandomItem(intSlice)
if err != nil {
t.Fatalf("Error getting random item from intSlice: %v", err)
}
fmt.Printf("Random int: %v\n", randInt)
randString, err := core.RandomItem(stringSlice)
if err != nil {
t.Fatalf("Error getting random item from stringSlice: %v", err)
}
fmt.Printf("Random string: %v\n", randString)
var emptySlice []int
_, err = core.RandomItem(emptySlice)
if err == nil {
t.Fatalf("Expected error when getting random item from empty slice, got nil")
}
}
func TestChunkSlice(t *testing.T) {
tests := []struct {
name string
slice []int
chunkSize int
want [][]int
}{
{
name: "Empty slice",
slice: []int{},
chunkSize: 2,
want: [][]int{},
},
{
name: "Slice smaller than chunk size",
slice: []int{1, 2},
chunkSize: 5,
want: [][]int{{1, 2}},
},
{
name: "Slice size equal to chunk size",
slice: []int{1, 2, 3},
chunkSize: 3,
want: [][]int{{1, 2, 3}},
},
// Add more test cases here
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
if got := core.ChunkSlice(tt.slice, tt.chunkSize); !reflect.DeepEqual(got, tt.want) {
//nolint: gocritic
if len(got) == len(tt.want) && len(got) == 0 {
return
}
t.Errorf("ChunkSlice() = %v, want %v", got, tt.want)
}
})
}
}