-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
113 lines (103 loc) · 2.11 KB
/
util_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
package iterutil_test
import (
"fmt"
"iter"
"testing"
)
func assertEqual[E comparable](
t *testing.T,
got iter.Seq[E],
want []E,
breakWhen func(E) bool,
) {
t.Helper()
var es []E
var i int
for e := range got {
if breakWhen(e) {
return
}
es = append(es, e)
if len(want) <= i {
t.Fatalf("too many elements: got %v...; want %v", es, want)
}
if e != want[i] {
t.Fatalf("unexpected element: got %v...; want %v...", es, want[:i+1])
}
i++
}
// i should now be equal to len(want)
if i != len(want) {
t.Fatalf("not enough elements: got %v; want %v...", es, want)
}
}
func alwaysFalse[E any](_ E) bool {
return false
}
func equal[E comparable](target E) func(E) bool {
return func(e E) bool {
return e == target
}
}
func assertEqual2[K, V comparable](
t *testing.T,
got iter.Seq2[K, V],
want []Pair[K, V],
breakWhen func(K, V) bool,
) {
t.Helper()
var pairs []Pair[K, V]
var i int
for k, v := range got {
if breakWhen(k, v) {
return
}
pairs = append(pairs, Pair[K, V]{k, v})
if len(want) <= i {
t.Fatalf("too many pairs: got %v...; want %v", pairs, want)
}
if k != want[i].k || v != want[i].v {
t.Fatalf("unexpected pair: got %v...; want %v...", pairs, want[:i+1])
}
i++
}
// i should now be equal to len(want)
if i != len(want) {
t.Fatalf("not enough pairs: got %v; want %v...", pairs, want)
}
}
type Pair[K, V any] struct {
k K
v V
}
func (p Pair[K, V]) String() string {
return fmt.Sprintf("(%v,%v)", p.k, p.v)
}
// falseAfterN, n is non-negative, returns a function that returns
// true for the first n invocations and
// false for subsequent invocations,
// regardless of the value of its argument;
// otherwise, it returns a function that invariably returns false.
func falseAfterN[E any](n int) func(E) bool {
if n < 0 {
return func(E) bool {
return true
}
}
var count int
return func(E) bool {
if count < n {
count++
return true
}
return false
}
}
func alwaysFalse2[K, V any](_ K, _ V) bool {
return false
}
func equal2[K, V comparable](key K, value V) func(K, V) bool {
return func(k K, v V) bool {
return k == key && v == value
}
}