-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_test.go
118 lines (103 loc) · 2.29 KB
/
tree_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
package kway
import (
"iter"
"slices"
"strings"
"testing"
)
func words[T any](values ...T) iter.Seq2[[]T, error] {
return func(yield func([]T, error) bool) {
var v [1]T
for _, v[0] = range values {
if !yield(v[:], nil) {
break
}
}
}
}
func TestTree(t *testing.T) {
tests := []struct {
scenario string
sequences [][]string
}{
{
scenario: "empty tree",
sequences: [][]string{},
},
{
scenario: "three sequences with no elements",
sequences: [][]string{{}, {}, {}},
},
{
scenario: "one sequence with one element",
sequences: [][]string{{"a"}},
},
{
scenario: "one sequence with three elements",
sequences: [][]string{{"a", "b", "c"}},
},
{
scenario: "three sequences with one element",
sequences: [][]string{{"a"}, {"b"}, {"c"}},
},
{
scenario: "three sequences of three elements",
sequences: [][]string{
{"a", "d", "g"},
{"b", "e", "h"},
{"c", "f", "i"},
},
},
{
scenario: "one sequence with the first element and a second sequence with the other elements",
sequences: [][]string{
{"a"},
{"b", "c", "d", "e", "f", "g", "h", "i"},
},
},
{
scenario: "one sequence with the last element and a second sequence with the other elements",
sequences: [][]string{
{"z"},
{"a", "b", "c", "d", "e", "f", "g", "h", "i"},
},
},
}
for _, test := range tests {
t.Run(test.scenario, func(t *testing.T) {
var seqs = make([]iter.Seq2[[]string, error], len(test.sequences))
for i, seq := range test.sequences {
seqs[i] = words(seq...)
}
var tree = makeTree(seqs...)
var values []string
var buffer [1]string
for {
n, err := tree.next(buffer[:], strings.Compare)
if err != nil {
t.Fatal(err)
}
if n == 0 {
break
}
values = append(values, buffer[0])
}
var want []string
for _, seq := range test.sequences {
want = append(want, seq...)
}
slices.Sort(want)
if !slices.Equal(values, want) {
t.Errorf("expected replayed values to be in order, got %v, want %v", values, want)
}
})
}
}
func TestParent(t *testing.T) {
if p := parent((2 * 10) + 1); p != 10 {
t.Errorf("expected parent of 21 to be 10, got %d", p)
}
if p := parent((2 * 10) + 2); p != 10 {
t.Errorf("expected parent of 22 to be 10, got %d", p)
}
}