Source file
src/reflect/iter_test.go
1
2
3
4
5 package reflect_test
6
7 import (
8 "iter"
9 "maps"
10 . "reflect"
11 "testing"
12 )
13
14 func TestValueSeq(t *testing.T) {
15 m := map[string]int{
16 "1": 1,
17 "2": 2,
18 "3": 3,
19 "4": 4,
20 }
21 c := make(chan int, 3)
22 for i := range 3 {
23 c <- i
24 }
25 close(c)
26 tests := []struct {
27 name string
28 val Value
29 check func(*testing.T, iter.Seq[Value])
30 }{
31 {"int", ValueOf(4), func(t *testing.T, s iter.Seq[Value]) {
32 i := int64(0)
33 for v := range s {
34 if v.Int() != i {
35 t.Fatalf("got %d, want %d", v.Int(), i)
36 }
37 i++
38 }
39 if i != 4 {
40 t.Fatalf("should loop four times")
41 }
42 }},
43 {"int8", ValueOf(int8(4)), func(t *testing.T, s iter.Seq[Value]) {
44 i := int8(0)
45 for v := range s {
46 if v.Interface().(int8) != i {
47 t.Fatalf("got %d, want %d", v.Int(), i)
48 }
49 i++
50 }
51 if i != 4 {
52 t.Fatalf("should loop four times")
53 }
54 }},
55 {"uint", ValueOf(uint64(4)), func(t *testing.T, s iter.Seq[Value]) {
56 i := uint64(0)
57 for v := range s {
58 if v.Uint() != i {
59 t.Fatalf("got %d, want %d", v.Uint(), i)
60 }
61 i++
62 }
63 if i != 4 {
64 t.Fatalf("should loop four times")
65 }
66 }},
67 {"uint8", ValueOf(uint8(4)), func(t *testing.T, s iter.Seq[Value]) {
68 i := uint8(0)
69 for v := range s {
70 if v.Interface().(uint8) != i {
71 t.Fatalf("got %d, want %d", v.Int(), i)
72 }
73 i++
74 }
75 if i != 4 {
76 t.Fatalf("should loop four times")
77 }
78 }},
79 {"*[4]int", ValueOf(&[4]int{1, 2, 3, 4}), func(t *testing.T, s iter.Seq[Value]) {
80 i := int64(0)
81 for v := range s {
82 if v.Int() != i {
83 t.Fatalf("got %d, want %d", v.Int(), i)
84 }
85 i++
86 }
87 if i != 4 {
88 t.Fatalf("should loop four times")
89 }
90 }},
91 {"[4]int", ValueOf([4]int{1, 2, 3, 4}), func(t *testing.T, s iter.Seq[Value]) {
92 i := int64(0)
93 for v := range s {
94 if v.Int() != i {
95 t.Fatalf("got %d, want %d", v.Int(), i)
96 }
97 i++
98 }
99 if i != 4 {
100 t.Fatalf("should loop four times")
101 }
102 }},
103 {"[]int", ValueOf([]int{1, 2, 3, 4}), func(t *testing.T, s iter.Seq[Value]) {
104 i := int64(0)
105 for v := range s {
106 if v.Int() != i {
107 t.Fatalf("got %d, want %d", v.Int(), i)
108 }
109 i++
110 }
111 if i != 4 {
112 t.Fatalf("should loop four times")
113 }
114 }},
115 {"string", ValueOf("12语言"), func(t *testing.T, s iter.Seq[Value]) {
116 i := int64(0)
117 indexes := []int64{0, 1, 2, 5}
118 for v := range s {
119 if v.Int() != indexes[i] {
120 t.Fatalf("got %d, want %d", v.Int(), indexes[i])
121 }
122 i++
123 }
124 if i != 4 {
125 t.Fatalf("should loop four times")
126 }
127 }},
128 {"map[string]int", ValueOf(m), func(t *testing.T, s iter.Seq[Value]) {
129 copy := maps.Clone(m)
130 for v := range s {
131 if _, ok := copy[v.String()]; !ok {
132 t.Fatalf("unexpected %v", v.Interface())
133 }
134 delete(copy, v.String())
135 }
136 if len(copy) != 0 {
137 t.Fatalf("should loop four times")
138 }
139 }},
140 {"chan int", ValueOf(c), func(t *testing.T, s iter.Seq[Value]) {
141 i := 0
142 m := map[int64]bool{
143 0: false,
144 1: false,
145 2: false,
146 }
147 for v := range s {
148 if b, ok := m[v.Int()]; !ok || b {
149 t.Fatalf("unexpected %v", v.Interface())
150 }
151 m[v.Int()] = true
152 i++
153 }
154 if i != 3 {
155 t.Fatalf("should loop three times")
156 }
157 }},
158 {"func", ValueOf(func(yield func(int) bool) {
159 for i := range 4 {
160 if !yield(i) {
161 return
162 }
163 }
164 }), func(t *testing.T, s iter.Seq[Value]) {
165 i := int64(0)
166 for v := range s {
167 if v.Int() != i {
168 t.Fatalf("got %d, want %d", v.Int(), i)
169 }
170 i++
171 }
172 if i != 4 {
173 t.Fatalf("should loop four times")
174 }
175 }},
176 }
177 for _, tc := range tests {
178 seq := tc.val.Seq()
179 tc.check(t, seq)
180 }
181 }
182
183 func TestValueSeq2(t *testing.T) {
184 m := map[string]int{
185 "1": 1,
186 "2": 2,
187 "3": 3,
188 "4": 4,
189 }
190 tests := []struct {
191 name string
192 val Value
193 check func(*testing.T, iter.Seq2[Value, Value])
194 }{
195 {"*[4]int", ValueOf(&[4]int{1, 2, 3, 4}), func(t *testing.T, s iter.Seq2[Value, Value]) {
196 i := int64(0)
197 for v1, v2 := range s {
198 if v1.Int() != i {
199 t.Fatalf("got %d, want %d", v1.Int(), i)
200 }
201 i++
202 if v2.Int() != i {
203 t.Fatalf("got %d, want %d", v2.Int(), i)
204 }
205 }
206 if i != 4 {
207 t.Fatalf("should loop four times")
208 }
209 }},
210 {"[4]int", ValueOf([4]int{1, 2, 3, 4}), func(t *testing.T, s iter.Seq2[Value, Value]) {
211 i := int64(0)
212 for v1, v2 := range s {
213 if v1.Int() != i {
214 t.Fatalf("got %d, want %d", v1.Int(), i)
215 }
216 i++
217 if v2.Int() != i {
218 t.Fatalf("got %d, want %d", v2.Int(), i)
219 }
220 }
221 if i != 4 {
222 t.Fatalf("should loop four times")
223 }
224 }},
225 {"[]int", ValueOf([]int{1, 2, 3, 4}), func(t *testing.T, s iter.Seq2[Value, Value]) {
226 i := int64(0)
227 for v1, v2 := range s {
228 if v1.Int() != i {
229 t.Fatalf("got %d, want %d", v1.Int(), i)
230 }
231 i++
232 if v2.Int() != i {
233 t.Fatalf("got %d, want %d", v2.Int(), i)
234 }
235 }
236 if i != 4 {
237 t.Fatalf("should loop four times")
238 }
239 }},
240 {"string", ValueOf("12语言"), func(t *testing.T, s iter.Seq2[Value, Value]) {
241 next, stop := iter.Pull2(s)
242 defer stop()
243 i := int64(0)
244 for j, s := range "12语言" {
245 v1, v2, ok := next()
246 if !ok {
247 t.Fatalf("should loop four times")
248 }
249 if v1.Int() != int64(j) {
250 t.Fatalf("got %d, want %d", v1.Int(), j)
251 }
252 if v2.Interface() != s {
253 t.Fatalf("got %v, want %v", v2.Interface(), s)
254 }
255 i++
256 }
257 if i != 4 {
258 t.Fatalf("should loop four times")
259 }
260 }},
261 {"map[string]int", ValueOf(m), func(t *testing.T, s iter.Seq2[Value, Value]) {
262 copy := maps.Clone(m)
263 for v1, v2 := range s {
264 v, ok := copy[v1.String()]
265 if !ok {
266 t.Fatalf("unexpected %v", v1.String())
267 }
268 if v != v2.Interface() {
269 t.Fatalf("got %v, want %d", v2.Interface(), v)
270 }
271 delete(copy, v1.String())
272 }
273 if len(copy) != 0 {
274 t.Fatalf("should loop four times")
275 }
276 }},
277 {"func", ValueOf(func(f func(int, int) bool) {
278 for i := range 4 {
279 f(i, i+1)
280 }
281 }), func(t *testing.T, s iter.Seq2[Value, Value]) {
282 i := int64(0)
283 for v1, v2 := range s {
284 if v1.Int() != i {
285 t.Fatalf("got %d, want %d", v1.Int(), i)
286 }
287 i++
288 if v2.Int() != i {
289 t.Fatalf("got %d, want %d", v2.Int(), i)
290 }
291 }
292 if i != 4 {
293 t.Fatalf("should loop four times")
294 }
295 }},
296 }
297 for _, tc := range tests {
298 seq := tc.val.Seq2()
299 tc.check(t, seq)
300 }
301 }
302
View as plain text