-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter_test.go
More file actions
149 lines (139 loc) · 2.26 KB
/
iter_test.go
File metadata and controls
149 lines (139 loc) · 2.26 KB
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
package flatmap
import (
"slices"
"testing"
)
func TestFlatMapAll(t *testing.T) {
fm := FlatMap[int, string]{
data: []Pair[int, string]{
{1, "4"},
{2, "3"},
{3, "2"},
{4, "1"},
},
}
count := 0
for k, v := range fm.All() {
count++
if v2, ok := fm.Get(k); !ok || v2 != v {
t.Errorf("got: %v, want: %v", v, v2)
}
}
if count != fm.Length() {
t.Errorf("length got: %v, want: %v", count, fm.Length())
}
}
func TestFlatMapAllContinueBreak(t *testing.T) {
fm := FlatMap[int, string]{
data: []Pair[int, string]{
{1, "4"},
{2, "3"},
{3, "2"},
{4, "1"},
},
}
count := 0
for range fm.All() {
count++
break
}
if count != 1 {
t.Error("break did not work")
}
count = 0
for k := range fm.All() {
if k%2 == 0 {
continue
}
count++
}
if count != 2 {
t.Error("continue did not work")
}
}
func TestFlatMapKeys(t *testing.T) {
fm := FlatMap[int, string]{
data: []Pair[int, string]{
{1, "4"},
{2, "3"},
{3, "2"},
{4, "1"},
},
}
values := []int{1, 2, 3, 4}
got := slices.Collect(fm.Keys())
if !slices.Equal(got, values) {
t.Errorf("keys got: %v, want: %v", got, values)
}
}
func TestFlatMapKeysContinueBreak(t *testing.T) {
fm := FlatMap[int, string]{
data: []Pair[int, string]{
{1, "4"},
{2, "3"},
{3, "2"},
{4, "1"},
},
}
count := 0
for range fm.Keys() {
count++
break
}
if count != 1 {
t.Error("break did not work")
}
count = 0
for k := range fm.Keys() {
if k%2 == 0 {
continue
}
count++
}
if count != 2 {
t.Error("continue did not work")
}
}
func TestFlatMapValues(t *testing.T) {
fm := FlatMap[int, string]{
data: []Pair[int, string]{
{1, "4"},
{2, "3"},
{3, "2"},
{4, "1"},
},
}
values := []string{"4", "3", "2", "1"}
got := slices.Collect(fm.Values())
if !slices.Equal(got, values) {
t.Errorf("values got: %v, want: %v", got, values)
}
}
func TestFlatMapValuesContinueBreak(t *testing.T) {
fm := FlatMap[int, int]{
data: []Pair[int, int]{
{1, 4},
{2, 3},
{3, 2},
{4, 1},
},
}
count := 0
for range fm.Values() {
count++
break
}
if count != 1 {
t.Error("break did not work")
}
count = 0
for v := range fm.Values() {
if v%2 == 0 {
continue
}
count++
}
if count != 2 {
t.Error("continue did not work")
}
}