-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_test.go
65 lines (60 loc) · 1.22 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
// Copyright 2023 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>
package iter_test
import (
"testing"
"golang.design/x/iter"
)
func TestNewBatchIterFromSlice(t *testing.T) {
tests := []struct {
name string
slice []int
batchSize int
}{
{
name: "empty slice",
slice: []int{},
batchSize: 1,
},
{
name: "non-empty slice with batch size 1",
slice: []int{
1, 2, 3, 4, 5,
},
batchSize: 1,
},
{
name: "non-empty slice with batch size 2",
slice: []int{
1, 2, 3, 4, 5,
},
batchSize: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
it := iter.NewBatchFromSlice(tt.slice, tt.batchSize)
var got []int
for batch, ok := it.Next(); ok; batch, ok = it.Next() {
got = append(got, batch...)
}
if !sliceEqual(got, tt.slice) {
t.Errorf("NewBatchIterFromSlice() = %v, want %v", got, tt.slice)
}
})
}
}
func sliceEqual[E comparable](s1, s2 []E) bool {
if len(s1) != len(s2) {
return false
}
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}
return true
}