forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_taskgroup_is_asyncsequence.swift
99 lines (78 loc) · 2.09 KB
/
async_taskgroup_is_asyncsequence.swift
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
// RUN: %target-run-simple-swift( -target %target-swift-5.1-abi-triple -parse-as-library) | %FileCheck %s --dump-input=always
// REQUIRES: executable_test
// REQUIRES: concurrency
// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
@available(SwiftStdlib 5.1,*)
func test_taskGroup_is_asyncSequence()async{
print(#function)
letsum=awaitwithTaskGroup(of:Int.self, returning:Int.self){ group in
fornin1...10{
group.spawn{
print("add \(n)")
return n
}
}
varsum=0
forawaitrin group { // here
print("next: \(r)")
sum += r
}
return sum
}
print("result: \(sum)")
}
@available(SwiftStdlib 5.1,*)
func test_throwingTaskGroup_is_asyncSequence()asyncthrows{
print(#function)
letsum=tryawaitwithThrowingTaskGroup(of:Int.self, returning:Int.self){ group in
fornin1...10{
group.spawn{
print("add \(n)")
return n
}
}
varsum=0
fortryawaitrin group { // here
print("next: \(r)")
sum += r
}
return sum
}
print("result: \(sum)")
}
typealiasLabelledTuple=(x:Int, y:Int)
@available(SwiftStdlib 5.1,*)
func test_asyncSequence_labelledTuples()async{
print(#function)
letsum=awaitwithTaskGroup(of:LabelledTuple.self, returning:Int.self){ group in
fornin1...10{
group.spawn{
print("add (x: \(n), y: 1)")
return(x: n, y:1)
}
}
varsum=0
forawait(x, y)in group { // here
print("next: (x:\(x), y:\(y))")
sum += x + y
}
return sum
}
print("result: \(sum)")
}
@available(SwiftStdlib 5.1,*)
@mainstructMain{
staticfunc main()async{
awaittest_taskGroup_is_asyncSequence()
// CHECK: test_taskGroup_is_asyncSequence()
// CHECK: result: 55
try!awaittest_throwingTaskGroup_is_asyncSequence()
// CHECK: test_throwingTaskGroup_is_asyncSequence()
// CHECK: result: 55
awaittest_asyncSequence_labelledTuples()
// CHECK: test_asyncSequence_labelledTuples()
// CHECK: result: 65
}
}