-
Notifications
You must be signed in to change notification settings - Fork 1
/
searching_test.go
153 lines (111 loc) · 3.48 KB
/
searching_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package ranges
import "testing"
func TestAll(t *testing.T) {
t.Parallel()
if !All(Only(2, 4), func(x int) bool { return x%2 == 0 }) {
t.Fatal("All did not return a positive result.")
}
if All(Only(2, 1), func(x int) bool { return x%2 == 0 }) {
t.Fatal("All did not return a negative result.")
}
}
func TestAny(t *testing.T) {
t.Parallel()
if Any(Only(1, 3), func(x int) bool { return x%2 == 0 }) {
t.Fatal("Any did not return a negative result.")
}
if !Any(Only(2, 1), func(x int) bool { return x%2 == 0 }) {
t.Fatal("Any did not return a positive result.")
}
}
func TestAmong(t *testing.T) {
t.Parallel()
if !Among(func(a, b int) bool { return a == b }, 1, 1, 2, 3) {
t.Fatal("1 is not in (1, 2, 3)")
}
if Among(func(a, b int) bool { return a == b }, 4, 1, 2, 3) {
t.Fatal("4 was in (1, 2, 3)")
}
if Among(func(a, b int) bool { return a == b }, 1) {
t.Fatal("1 is in ()")
}
}
func TestAmongEq(t *testing.T) {
t.Parallel()
if !AmongEq(1, 1, 2, 3) {
t.Fatal("1 is not in (1, 2, 3)")
}
if AmongEq(4, 1, 2, 3) {
t.Fatal("4 was in (1, 2, 3)")
}
}
func TestStartsWith(t *testing.T) {
t.Parallel()
if !StartsWith[int, float64](Null[int](), Null[float64](), nil) {
t.Fatal("Two empty ranges do not satisfy StartsWith")
}
if !StartsWith[int, float64](Only(1), Null[float64](), nil) {
t.Fatal("A non-empty range and empty range did not satisfy StartsWith")
}
if StartsWith[int, float64](Null[int](), Only(1.0), nil) {
t.Fatal("An empty range should result in false if the search range is non-empty")
}
if !StartsWith[int, float64](Only(1, 2, 3), Only(1.0, 2.0), func(x int, y float64) bool { return x == int(y) }) {
t.Fatal("[]int{1, 2, 3} did not contain []float64{1.0, 2.0}")
}
if StartsWith[int, float64](Only(1, 2, 3), Only(2.0), func(x int, y float64) bool { return x == int(y) }) {
t.Fatal("[]int{1, 2, 3} should not start with []float64{2.0}")
}
}
func TestSkipOver(t *testing.T) {
t.Parallel()
empty := ForwardRange[rune](Runes(""))
if !SkipOver(&empty, Runes("xyz"), Eq[rune]) {
t.Error("SkipOver(\"\", \"xyz\") != true")
}
if !SkipOver(nil, Runes("xyz"), Eq[rune]) {
t.Error("SkipOver(nil, \"xyz\") != true")
}
r1 := ForwardRange[rune](Runes("Hello world"))
if SkipOver(&r1, Runes("Ha"), Eq[rune]) {
t.Fatal("SkipOver(\"Hello world\", \"Ha\") != false")
}
if !SkipOver(&r1, Runes("Hell"), Eq[rune]) {
t.Fatal("SkipOver(\"Hello world\", \"Hell\") != true")
}
assertEqual(t, String(r1), "o world")
}
func TestLength(t *testing.T) {
t.Parallel()
assertEqual(t, Length(Only[int]()), 0)
assertEqual(t, Length(Only(1)), 1)
assertEqual(t, Length(Only(4, 5, 6)), 3)
}
func TestCount(t *testing.T) {
t.Parallel()
r := Only(1, 2, 3, 4, 5)
res1 := Count[int](r, func(x int) bool { return x%2 == 0 })
if res1 != 2 {
t.Fatalf("Count counted %d instead of 2 even elements", res1)
}
if !r.Empty() {
t.Fatal("The range was not exhaustively searched")
}
res2 := Count[int](Only(1, 2, 3, 4, 5), func(x int) bool { return x%2 == 1 })
if res2 != 3 {
t.Fatalf("Count counted %d instead of 3 odd elements", res2)
}
}
func TestCountUntil(t *testing.T) {
t.Parallel()
r := Only(4, 2, 3, 4, 5)
res1 := CountUntil[int](r, func(x int) bool { return x%2 == 1 })
if res1 != 2 {
t.Fatalf("CountUntil counted %d instead of 2 even elements", res1)
}
assertHasFront(t, r, 3)
res2 := CountUntil[int](Only(1, 1, 3, 4, 5), func(x int) bool { return x%2 == 0 })
if res2 != 3 {
t.Fatalf("CountUntil counted %d instead of 3 odd elements", res2)
}
}