-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange_test.go
54 lines (51 loc) · 1.23 KB
/
range_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
package iter_test
import (
"testing"
"github.com/patrickhuber/go-iter"
)
func TestRange(t *testing.T) {
run := func(t *testing.T, includeStart bool, start, end int, includeEnd bool) {
var rng iter.Iterator[int]
if includeStart && !includeEnd {
rng = iter.Range(start, end)
} else {
rng = iter.RangeWith(includeStart, start, end, includeEnd)
}
index := start
if !includeStart {
index += 1
}
for op := rng.Next(); op.IsSome(); op = rng.Next() {
if op.Unwrap() != index {
t.Fatalf("expected %d to equal %d", index, op.Unwrap())
}
index++
}
index -= 1
if index != end && includeEnd {
t.Fatalf("expected %d to equal %d", index, end)
}
if index != end-1 && !includeEnd {
t.Fatalf("expected %d to equal %d", index, end-1)
}
}
type test struct {
name string
includeStart bool
start int
end int
includeEnd bool
}
tests := []test{
{"[0..10)", true, 0, 10, false},
{"[15..30)", true, 15, 30, false},
{"[15..30]", true, 15, 30, true},
{"(15..30)", false, 15, 30, false},
{"(15..30]", false, 15, 30, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
run(t, test.includeStart, test.start, test.end, test.includeEnd)
})
}
}