-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector_test.go
64 lines (58 loc) · 1.26 KB
/
vector_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
package collection
import "testing"
func TestAppendAll(t *testing.T) {
var a Vec[int]
a = a.AppendAll(1, 2, 3)
if a.Len() != 3 {
t.Errorf("expected 3, got %d", a.Len())
}
}
func TestAppenIter(t *testing.T) {
var a Vec[int]
i := -1
a = a.AppendIter(func() (int, bool) {
i++
return 1, i < 3
})
if a.Len() != 3 {
t.Errorf("expected 3, got %d", a.Len())
}
}
func TestIter(t *testing.T) {
var a Vec[int]
a = a.AppendAll(1, 2, 3)
it := a.Iter()
// consume the iterator
for i := 0; i < 3; i++ {
if v, ok := it(); !ok {
t.Errorf("expected ok, got %v", ok)
} else if v != i+1 {
t.Errorf("expected %d, got %d", i+1, v)
}
}
// returns zero value and false, end of iteration
if v, ok := it(); ok {
t.Errorf("expected !ok, got %v", ok)
} else if v != 0 {
t.Errorf("expected 0, got %d", v)
}
}
func TestReverseIter(t *testing.T) {
var a Vec[int]
a = a.AppendAll(1, 2, 3)
it := a.ReverseIter()
// consume the iterator
for i := 3; i > 0; i-- {
if v, ok := it(); !ok {
t.Errorf("expected ok, got %v", ok)
} else if v != i {
t.Errorf("expected %d, got %d", i, v)
}
}
// returns zero value and false, end of iteration
if v, ok := it(); ok {
t.Errorf("expected !ok, got %v", ok)
} else if v != 0 {
t.Errorf("expected 0, got %d", v)
}
}