13
13
import AsyncAlgorithms
14
14
15
15
final class TestZip2: XCTestCase {
16
- func test_zip ( ) async {
16
+ func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_all_sequences_have_same_size ( ) async {
17
17
let a = [ 1 , 2 , 3 ]
18
18
let b = [ " a " , " b " , " c " ]
19
+
19
20
let expected = Array ( zip ( a, b) )
20
21
let actual = await Array ( zip ( a. async , b. async ) )
21
22
XCTAssertEqual ( expected, actual)
22
23
}
23
24
24
- func test_zip_first_longer ( ) async {
25
+ func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_first_is_longer ( ) async {
25
26
let a = [ 1 , 2 , 3 , 4 , 5 ]
26
27
let b = [ " a " , " b " , " c " ]
28
+
27
29
let expected = Array ( zip ( a, b) )
28
30
let actual = await Array ( zip ( a. async , b. async ) )
29
31
XCTAssertEqual ( expected, actual)
30
32
}
31
33
32
- func test_zip_second_longer ( ) async {
34
+ func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_second_is_longer ( ) async {
33
35
let a = [ 1 , 2 , 3 ]
34
36
let b = [ " a " , " b " , " c " , " d " , " e " ]
37
+
35
38
let expected = Array ( zip ( a, b) )
36
39
let actual = await Array ( zip ( a. async , b. async ) )
37
40
XCTAssertEqual ( expected, actual)
38
41
}
39
42
40
- func test_iterate_past_end ( ) async {
43
+ func test_zip_produces_nil_next_element_when_iteration_is_finished_and_all_sequences_have_same_size ( ) async {
41
44
let a = [ 1 , 2 , 3 ]
42
45
let b = [ " a " , " b " , " c " ]
43
46
let sequence = zip ( a. async , b. async )
44
47
var iterator = sequence. makeAsyncIterator ( )
48
+
49
+ let expected = Array ( zip ( a, b) )
45
50
var collected = [ ( Int, String) ] ( )
46
51
while let item = await iterator. next ( ) {
47
52
collected. append ( item)
48
53
}
49
- XCTAssertEqual ( [ ( 1 , " a " ) , ( 2 , " b " ) , ( 3 , " c " ) ] , collected)
54
+ XCTAssertEqual ( expected, collected)
55
+
50
56
let pastEnd = await iterator. next ( )
51
57
XCTAssertNil ( pastEnd)
52
58
}
53
59
54
- func test_iterate_past_end_first_longer ( ) async {
60
+ func test_zip_produces_nil_next_element_when_iteration_is_finished_and_first_is_longer ( ) async {
55
61
let a = [ 1 , 2 , 3 , 4 , 5 ]
56
62
let b = [ " a " , " b " , " c " ]
57
63
let sequence = zip ( a. async , b. async )
58
64
var iterator = sequence. makeAsyncIterator ( )
65
+
66
+ let expected = Array ( zip ( a, b) )
59
67
var collected = [ ( Int, String) ] ( )
60
68
while let item = await iterator. next ( ) {
61
69
collected. append ( item)
62
70
}
63
- XCTAssertEqual ( [ ( 1 , " a " ) , ( 2 , " b " ) , ( 3 , " c " ) ] , collected)
71
+ XCTAssertEqual ( expected, collected)
72
+
64
73
let pastEnd = await iterator. next ( )
65
74
XCTAssertNil ( pastEnd)
66
75
}
67
76
68
- func test_iterate_past_end_second_longer ( ) async {
77
+ func test_zip_produces_nil_next_element_when_iteration_is_finished_and_second_is_longer ( ) async {
69
78
let a = [ 1 , 2 , 3 ]
70
79
let b = [ " a " , " b " , " c " , " d " , " e " ]
71
80
let sequence = zip ( a. async , b. async )
72
81
var iterator = sequence. makeAsyncIterator ( )
82
+
83
+ let expected = Array ( zip ( a, b) )
73
84
var collected = [ ( Int, String) ] ( )
74
85
while let item = await iterator. next ( ) {
75
86
collected. append ( item)
76
87
}
77
- XCTAssertEqual ( [ ( 1 , " a " ) , ( 2 , " b " ) , ( 3 , " c " ) ] , collected)
88
+ XCTAssertEqual ( expected, collected)
89
+
78
90
let pastEnd = await iterator. next ( )
79
91
XCTAssertNil ( pastEnd)
80
92
}
81
93
82
- func test_first_throwing ( ) async throws {
94
+ func test_zip_produces_one_element_and_throws_when_first_produces_one_element_and_throws ( ) async throws {
83
95
let a = [ 1 , 2 , 3 ]
84
96
let b = [ " a " , " b " , " c " ]
85
97
let sequence = zip ( a. async . map { try throwOn ( 2 , $0) } , b. async )
86
98
var iterator = sequence. makeAsyncIterator ( )
99
+
100
+ let expected = [ ( 1 , " a " ) ]
87
101
var collected = [ ( Int, String) ] ( )
88
102
do {
89
103
while let item = try await iterator. next ( ) {
90
104
collected. append ( item)
91
105
}
92
- XCTFail( )
106
+ XCTFail( " Zipped sequence should throw after one collected element " )
93
107
} catch {
94
108
XCTAssertEqual ( Failure ( ) , error as? Failure )
95
109
}
96
- XCTAssertEqual ( [ ( 1 , " a " ) ] , collected)
110
+ XCTAssertEqual ( expected, collected)
111
+
97
112
let pastEnd = try await iterator. next ( )
98
113
XCTAssertNil ( pastEnd)
99
114
}
100
115
101
- func test_second_throwing ( ) async throws {
116
+ func test_zip_produces_one_element_and_throws_when_second_produces_one_element_and_throws ( ) async throws {
102
117
let a = [ 1 , 2 , 3 ]
103
118
let b = [ " a " , " b " , " c " ]
104
119
let sequence = zip ( a. async , b. async . map { try throwOn ( " b " , $0) } )
105
120
var iterator = sequence. makeAsyncIterator ( )
121
+
122
+ let expected = [ ( 1 , " a " ) ]
106
123
var collected = [ ( Int, String) ] ( )
107
124
do {
108
125
while let item = try await iterator. next ( ) {
109
126
collected. append ( item)
110
127
}
111
- XCTFail( )
128
+ XCTFail( " Zipped sequence should throw after one collected element " )
112
129
} catch {
113
130
XCTAssertEqual ( Failure ( ) , error as? Failure )
114
131
}
115
- XCTAssertEqual ( [ ( 1 , " a " ) ] , collected)
132
+ XCTAssertEqual ( expected, collected)
133
+
116
134
let pastEnd = try await iterator. next ( )
117
135
XCTAssertNil ( pastEnd)
118
136
}
119
137
120
- func test_cancellation ( ) async {
138
+ func test_zip_finishes_when_iteration_task_is_cancelled ( ) async {
121
139
let source1 = Indefinite ( value: " test1 " )
122
140
let source2 = Indefinite ( value: " test2 " )
123
141
let sequence = zip ( source1. async , source2. async )
@@ -143,159 +161,188 @@ final class TestZip2: XCTestCase {
143
161
}
144
162
145
163
final class TestZip3: XCTestCase {
146
- func test_zip ( ) async {
164
+ func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_all_sequences_have_same_size ( ) async {
147
165
let a = [ 1 , 2 , 3 ]
148
166
let b = [ " a " , " b " , " c " ]
149
167
let c = [ 1 , 2 , 3 ]
168
+
169
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
150
170
let actual = await Array ( zip ( a. async , b. async , c. async ) )
151
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , actual)
171
+ XCTAssertEqual ( expected , actual)
152
172
}
153
173
154
- func test_zip_first_longer ( ) async {
174
+ func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_first_is_longer ( ) async {
155
175
let a = [ 1 , 2 , 3 , 4 , 5 ]
156
176
let b = [ " a " , " b " , " c " ]
157
177
let c = [ 1 , 2 , 3 ]
178
+
179
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
158
180
let actual = await Array ( zip ( a. async , b. async , c. async ) )
159
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , actual)
181
+ XCTAssertEqual ( expected , actual)
160
182
}
161
183
162
- func test_zip_second_longer ( ) async {
184
+ func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_second_is_longer ( ) async {
163
185
let a = [ 1 , 2 , 3 ]
164
186
let b = [ " a " , " b " , " c " , " d " , " e " ]
165
187
let c = [ 1 , 2 , 3 ]
188
+
189
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
166
190
let actual = await Array ( zip ( a. async , b. async , c. async ) )
167
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , actual)
191
+ XCTAssertEqual ( expected , actual)
168
192
}
169
193
170
- func test_zip_third_longer ( ) async {
194
+ func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_third_is_longer ( ) async {
171
195
let a = [ 1 , 2 , 3 ]
172
196
let b = [ " a " , " b " , " c " ]
173
197
let c = [ 1 , 2 , 3 , 4 , 5 ]
198
+
199
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
174
200
let actual = await Array ( zip ( a. async , b. async , c. async ) )
175
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , actual)
201
+ XCTAssertEqual ( expected , actual)
176
202
}
177
203
178
- func test_iterate_past_end ( ) async {
204
+ func test_zip_produces_nil_next_element_when_iteration_is_finished_and_all_sequences_have_same_size ( ) async {
179
205
let a = [ 1 , 2 , 3 ]
180
206
let b = [ " a " , " b " , " c " ]
181
207
let c = [ 1 , 2 , 3 ]
182
208
let sequence = zip ( a. async , b. async , c. async )
183
209
var iterator = sequence. makeAsyncIterator ( )
210
+
211
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
184
212
var collected = [ ( Int, String, Int) ] ( )
185
213
while let item = await iterator. next ( ) {
186
214
collected. append ( item)
187
215
}
188
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , collected)
216
+ XCTAssertEqual ( expected, collected)
217
+
189
218
let pastEnd = await iterator. next ( )
190
219
XCTAssertNil ( pastEnd)
191
220
}
192
221
193
- func test_iterate_past_end_first_longer ( ) async {
222
+ func test_zip_produces_nil_next_element_when_iteration_is_finished_and_first_is_longer ( ) async {
194
223
let a = [ 1 , 2 , 3 , 4 , 5 ]
195
224
let b = [ " a " , " b " , " c " ]
196
225
let c = [ 1 , 2 , 3 ]
197
226
let sequence = zip ( a. async , b. async , c. async )
198
227
var iterator = sequence. makeAsyncIterator ( )
228
+
229
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
199
230
var collected = [ ( Int, String, Int) ] ( )
200
231
while let item = await iterator. next ( ) {
201
232
collected. append ( item)
202
233
}
203
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , collected)
234
+ XCTAssertEqual ( expected, collected)
235
+
204
236
let pastEnd = await iterator. next ( )
205
237
XCTAssertNil ( pastEnd)
206
238
}
207
239
208
- func test_iterate_past_end_second_longer ( ) async {
240
+ func test_zip_produces_nil_next_element_when_iteration_is_finished_and_second_is_longer ( ) async {
209
241
let a = [ 1 , 2 , 3 ]
210
242
let b = [ " a " , " b " , " c " , " d " , " e " ]
211
243
let c = [ 1 , 2 , 3 ]
212
244
let sequence = zip ( a. async , b. async , c. async )
213
245
var iterator = sequence. makeAsyncIterator ( )
246
+
247
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
214
248
var collected = [ ( Int, String, Int) ] ( )
215
249
while let item = await iterator. next ( ) {
216
250
collected. append ( item)
217
251
}
218
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , collected)
252
+ XCTAssertEqual ( expected, collected)
253
+
219
254
let pastEnd = await iterator. next ( )
220
255
XCTAssertNil ( pastEnd)
221
256
}
222
257
223
- func test_iterate_past_end_third_longer ( ) async {
258
+ func test_zip_produces_nil_next_element_when_iteration_is_finished_and_third_is_longer ( ) async {
224
259
let a = [ 1 , 2 , 3 ]
225
260
let b = [ " a " , " b " , " c " ]
226
261
let c = [ 1 , 2 , 3 , 4 , 5 ]
227
262
let sequence = zip ( a. async , b. async , c. async )
228
263
var iterator = sequence. makeAsyncIterator ( )
264
+
265
+ let expected = [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ]
229
266
var collected = [ ( Int, String, Int) ] ( )
230
267
while let item = await iterator. next ( ) {
231
268
collected. append ( item)
232
269
}
233
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) , ( 2 , " b " , 2 ) , ( 3 , " c " , 3 ) ] , collected)
270
+ XCTAssertEqual ( expected, collected)
271
+
234
272
let pastEnd = await iterator. next ( )
235
273
XCTAssertNil ( pastEnd)
236
274
}
237
275
238
- func test_first_throwing ( ) async throws {
276
+ func test_zip_produces_one_element_and_throws_when_first_produces_one_element_and_throws ( ) async throws {
239
277
let a = [ 1 , 2 , 3 ]
240
278
let b = [ " a " , " b " , " c " ]
241
279
let c = [ 1 , 2 , 3 ]
242
280
let sequence = zip ( a. async . map { try throwOn ( 2 , $0) } , b. async , c. async )
243
281
var iterator = sequence. makeAsyncIterator ( )
282
+
283
+ let expected = [ ( 1 , " a " , 1 ) ]
244
284
var collected = [ ( Int, String, Int) ] ( )
245
285
do {
246
286
while let item = try await iterator. next ( ) {
247
287
collected. append ( item)
248
288
}
249
- XCTFail( )
289
+ XCTFail( " Zipped sequence should throw after one collected element " )
250
290
} catch {
251
291
XCTAssertEqual ( Failure ( ) , error as? Failure )
252
292
}
253
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) ] , collected)
293
+ XCTAssertEqual ( expected, collected)
294
+
254
295
let pastEnd = try await iterator. next ( )
255
296
XCTAssertNil ( pastEnd)
256
297
}
257
298
258
- func test_second_throwing ( ) async throws {
299
+ func test_zip_produces_one_element_and_throws_when_second_produces_one_element_and_throws ( ) async throws {
259
300
let a = [ 1 , 2 , 3 ]
260
301
let b = [ " a " , " b " , " c " ]
261
302
let c = [ 1 , 2 , 3 ]
262
303
let sequence = zip ( a. async , b. async . map { try throwOn ( " b " , $0) } , c. async )
263
304
var iterator = sequence. makeAsyncIterator ( )
305
+
306
+ let expected = [ ( 1 , " a " , 1 ) ]
264
307
var collected = [ ( Int, String, Int) ] ( )
265
308
do {
266
309
while let item = try await iterator. next ( ) {
267
310
collected. append ( item)
268
311
}
269
- XCTFail( )
312
+ XCTFail( " Zipped sequence should throw after one collected element " )
270
313
} catch {
271
314
XCTAssertEqual ( Failure ( ) , error as? Failure )
272
315
}
273
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) ] , collected)
316
+ XCTAssertEqual ( expected, collected)
317
+
274
318
let pastEnd = try await iterator. next ( )
275
319
XCTAssertNil ( pastEnd)
276
320
}
277
321
278
- func test_third_throwing ( ) async throws {
322
+ func test_zip_produces_one_element_and_throws_when_third_produces_one_element_and_throws ( ) async throws {
279
323
let a = [ 1 , 2 , 3 ]
280
324
let b = [ " a " , " b " , " c " ]
281
325
let c = [ 1 , 2 , 3 ]
282
326
let sequence = zip ( a. async , b. async , c. async . map { try throwOn ( 2 , $0) } )
283
327
var iterator = sequence. makeAsyncIterator ( )
328
+
329
+ let expected = [ ( 1 , " a " , 1 ) ]
284
330
var collected = [ ( Int, String, Int) ] ( )
285
331
do {
286
332
while let item = try await iterator. next ( ) {
287
333
collected. append ( item)
288
334
}
289
- XCTFail( )
335
+ XCTFail( " Zipped sequence should throw after one collected element " )
290
336
} catch {
291
337
XCTAssertEqual ( Failure ( ) , error as? Failure )
292
338
}
293
- XCTAssertEqual ( [ ( 1 , " a " , 1 ) ] , collected)
339
+ XCTAssertEqual ( expected, collected)
340
+
294
341
let pastEnd = try await iterator. next ( )
295
342
XCTAssertNil ( pastEnd)
296
343
}
297
344
298
- func test_cancellation ( ) async {
345
+ func test_zip_finishes_when_iteration_task_is_cancelled ( ) async {
299
346
let source1 = Indefinite ( value: " test1 " )
300
347
let source2 = Indefinite ( value: " test2 " )
301
348
let source3 = Indefinite ( value: " test3 " )
0 commit comments