forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_properties_actor.swift
202 lines (160 loc) · 4.7 KB
/
async_properties_actor.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// RUN: %target-run-simple-swift(-parse-as-library -Xfrontend -enable-copy-propagation -Xfrontend -enable-lexical-lifetimes=false -target %target-swift-5.1-abi-triple) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
structContainer{
@MainActorstaticvarcounter:Int=10
@MainActorstaticvarthis:Container?
varnoniso:Int=20
staticfunc getCount()async->Int{
returnawait counter
}
staticfunc getValue()async->Int?{
returnawait this?.noniso
}
}
@propertyWrapper
structSuccessTracker{
privatevar_stored:Bool
privatevarnumReads:Int=0 // an unchecked statistic to exercise mutating get
init(initially:Bool){
self._stored = initially
}
varwrappedValue:Bool{
mutating get{
numReads +=1
return _stored
}
set{ _stored = newValue }
}
}
func writeToBool(_ b :inoutBool, _ val :Bool){
b = val
}
actorList<T :Sendable>{
varhead:T
vartail:List<T>?
lazy varhasTail:Bool=(tail !=nil)
varcomputedTail:List<T>?{
get{ tail }
}
subscript(_ offset :Int)->T?{
// since we don't have async subscripts, we just return nil for non-zero inputs! :)
if offset !=0{
returnnil
}
return head
}
/// this silly property is here just for testing wrapped-property access
@SuccessTracker(initially:true)varsuccess:Bool
func setSuccess(_ b :Bool){writeToBool(&success, b)}
init(_ value :T){
self.head = value
self.tail =nil
}
init(_ value :T, _ tail :List<T>){
self.head = value
self.tail = tail
}
func last()async->T{
switch tail {
case.none:
return head
case .some(let tl):
if await tl.hasTail{
returnawait tl.last()
}
return await tl.head
}
}
staticfunc tabulate(_ n :Int, _ f :(Int)->T)->List<T>{
if n ==0{
returnList<T>(f(n))
}
returnList<T>(f(n),tabulate(n-1, f))
}
staticfunc foldr<R>(_ f :(T,R)->R, _ start :R, _ lst :List<T>)async->R{
switch await lst.tail{
case .none:
returnf(await lst.head, start)
case .some(let tl):
returnf(await lst.head,awaitfoldr(f, start, tl))
}
}
}
actorTester{
/// returns true iff success
func doListTest()async->Bool{
letn=4 // if you change this, you'll have to update other stuff too
letints= List<Int>.tabulate(n,{ $0 })
letlast1=await ints.tail!.computedTail!.tail!.tail![0]
letlast2=await ints.last()
guard last1 == last2 else{
print("fail 1")
returnfalse
}
letexpected=(n *(n +1))/2
letsum=await List<Int>.foldr({ $0 + $1 },0, ints)
guard sum == expected else{
print("fail 2")
returnfalse
}
// CHECK: done list test
// CHECK-NOT: fail
print("done list test")
returntrue
}
func doPropertyWrapperTest()async->Bool{
letactor=List<String>("blah")
awaitactor.setSuccess(false)
guardawaitactor.success ==falseelse{
print("fail 3")
returnfalse
}
awaitactor.setSuccess(true)
guardawaitactor.success ==trueelse{
print("fail 4")
returnfalse
}
// CHECK: done property wrapper test
// CHECK-NOT: fail
print("done property wrapper test")
returntrue
}
func doContainerTest()async->Bool{
vartotal:Int=0
total +=awaitContainer.getCount()
total +=awaitContainer.getValue()??0
return total ==10
}
}
@globalActor
structSillyActor{
actor_Impl{}
staticletshared=_Impl()
}
@SillyActor
vartest:Tester=Tester()
@SillyActor
varexpectedResult:Bool{
get{true}
set{}
}
@mainstructRunIt{
staticfunc main()async{
letsuccess=await expectedResult
guardawait test.doListTest()== success else{
fatalError("fail list test")
}
guardawait test.doPropertyWrapperTest()== success else{
fatalError("fail property wrapper test")
}
guardawait test.doContainerTest()== success else{
fatalError("fail container test")
}
// CHECK: done all testing
print("done all testing")
}
}