-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
123 lines (116 loc) · 2.47 KB
/
iterator_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
package stream
import (
"github.com/chinalhr/go-stream/types"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func TestSliceIterator(t *testing.T) {
testCases := []func(it iterator){
func(it iterator) {
size := it.GetSize()
assert.Equal(t, 5, size)
},
func(it iterator) {
for i := 1; i <= 5; i++ {
e := it.Next()
assert.Equal(t, i, e)
}
},
func(it iterator) {
hasNext := it.HasNext()
assert.Equal(t, false, hasNext)
},
}
sliceIterator := buildSliceIterator(1, 2, 3, 4, 5)
for _, testCase := range testCases {
t.Run("testCase", func(t *testing.T) {
testCase(sliceIterator)
})
}
}
func TestSliceReflectIterator(t *testing.T) {
testData := [5]int{1, 2, 3, 4, 5}
testCases := []func(it iterator){
func(it iterator) {
size := it.GetSize()
assert.Equal(t, 5, size)
},
func(it iterator) {
for i := 1; i <= 5; i++ {
e := it.Next()
assert.Equal(t, i, e)
}
},
func(it iterator) {
hasNext := it.HasNext()
assert.Equal(t, false, hasNext)
},
}
sliceReflectIterator := buildSliceReflectIterator(reflect.ValueOf(testData))
for _, testCase := range testCases {
t.Run("testCase", func(t *testing.T) {
testCase(sliceReflectIterator)
})
}
}
func TestMapReflectIterator(t *testing.T) {
testData := map[int]string{
1: "A",
2: "B",
3: "C",
4: "D",
5: "E",
}
testCases := []func(it iterator){
func(it iterator) {
size := it.GetSize()
assert.Equal(t, 5, size)
},
func(it iterator) {
for i := 1; i <= 5; i++ {
e := it.Next()
delete(testData, e.(types.KV).KEY.(int))
}
},
func(it iterator) {
hasNext := it.HasNext()
assert.Equal(t, false, hasNext)
assert.Empty(t, testData)
},
}
mapReflectIterator := buildMapReflectIterator(reflect.ValueOf(testData))
for _, testCase := range testCases {
t.Run("testCase", func(t *testing.T) {
testCase(mapReflectIterator)
})
}
}
func TestSupplierIterator(t *testing.T) {
var testData = 0
testCases := []func(it iterator){
func(it iterator) {
size := it.GetSize()
assert.Equal(t, -1, size)
},
func(it iterator) {
for i := 1; i <= 5; i++ {
e := it.Next()
assert.Equal(t, i, e)
}
},
func(it iterator) {
hasNext := it.HasNext()
assert.Equal(t, true, hasNext)
},
}
supplierIterator := buildSupplierIterator(func() types.T {
testData = testData + 1
return testData
})
for _, testCase := range testCases {
t.Run("testCase", func(t *testing.T) {
testCase(supplierIterator)
})
}
}