-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_ordered_test.go
84 lines (76 loc) · 1.45 KB
/
merge_ordered_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
package iterx_test
import (
"iter"
"slices"
"strconv"
"testing"
"github.com/ndx-technologies/iterx"
)
func TestMergeOrdered(t *testing.T) {
type S struct {
c string
v int
t int
}
cmp := func(a, b S) bool { return a.t < b.t }
tests := []struct {
vs [][]S
merged []S
}{
{
vs: [][]S{
{{c: "r", v: 1, t: 1}, {c: "r", v: 3, t: 2}, {c: "r", v: 5, t: 5}},
{{c: "g", v: 2, t: 3}},
{{c: "b", v: 0, t: 4}, {c: "g", v: 7, t: 7}, {c: "g", v: 8, t: 9}},
},
merged: []S{
{c: "r", v: 1, t: 1},
{c: "r", v: 3, t: 2},
{c: "g", v: 2, t: 3},
{c: "b", v: 0, t: 4},
{c: "r", v: 5, t: 5},
{c: "g", v: 7, t: 7},
{c: "g", v: 8, t: 9},
},
},
{
vs: [][]S{
{{c: "r", v: 1, t: 1}, {c: "r", v: 3, t: 2}, {c: "r", v: 5, t: 5}},
nil,
nil,
},
merged: []S{
{c: "r", v: 1, t: 1},
{c: "r", v: 3, t: 2},
{c: "r", v: 5, t: 5},
},
},
{
vs: [][]S{
nil,
nil,
nil,
},
merged: nil,
},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
iters := make([]iter.Seq[S], len(test.vs))
for i, vs := range test.vs {
iters[i] = slices.Values(vs)
}
var merged []S
for q := range iterx.MergeOrdered(cmp, iters...) {
merged = append(merged, q)
}
total := 0
for _, vs := range test.vs {
total += len(vs)
}
if !slices.Equal(merged, test.merged) {
t.Errorf("got %v, want %v", merged, test.merged)
}
})
}
}