-
Notifications
You must be signed in to change notification settings - Fork 6
/
chunk_test.go
98 lines (95 loc) · 1.84 KB
/
chunk_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
package go2linq
import (
"errors"
"fmt"
"iter"
"slices"
"testing"
)
func TestChunk_int(t *testing.T) {
type args struct {
source iter.Seq[int]
size int
}
tests := []struct {
name string
args args
want iter.Seq[[]int]
wantErr bool
expectedErr error
}{
{name: "01",
wantErr: true,
expectedErr: ErrNilSource,
},
{name: "02",
args: args{
size: 2,
},
wantErr: true,
expectedErr: ErrNilSource,
},
{name: "03",
args: args{
source: Empty[int](),
size: 0,
},
wantErr: true,
expectedErr: ErrSizeOutOfRange,
},
{name: "EmptySource",
args: args{
source: Empty[int](),
size: 2,
},
want: slices.Values([][]int{}),
},
{name: "1",
args: args{
source: VarToSeq(1, 2),
size: 2,
},
want: VarToSeq([]int{1, 2}),
},
{name: "2",
args: args{
source: VarToSeq(1, 2, 3),
size: 2,
},
want: VarToSeq([]int{1, 2}, []int{3}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Chunk(tt.args.source, tt.args.size)
if (err != nil) != tt.wantErr {
t.Errorf("Chunk() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if !errors.Is(err, tt.expectedErr) {
t.Errorf("Chunk() error = %v, expectedErr %v", err, tt.expectedErr)
}
return
}
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("Chunk() = %v, want %v", StringDef(got), StringDef(tt.want))
}
})
}
}
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/partitioning-data#example
func ExampleChunk() {
chunkNumber := 0
rng, _ := Range(0, 8)
chunk, _ := Chunk(rng, 3)
for ii := range chunk {
chunkNumber++
fmt.Printf("Chunk %d:%v\n", chunkNumber, ii)
}
// Output:
// Chunk 1:[0 1 2]
// Chunk 2:[3 4 5]
// Chunk 3:[6 7]
}