OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // VMOptions=--optimization-counter-threshold=5 | 5 // VMOptions=--optimization-counter-threshold=5 |
6 | 6 |
7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
9 | 9 |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 | 11 |
12 // It does not matter where a future is generated. | 12 // It does not matter where a future is generated. |
13 bar(p) async => p; | 13 bar(p) async => p; |
14 baz(p) => new Future(() => p); | 14 baz(p) => new Future(() => p); |
15 | 15 |
16 foo() async { | 16 foo() async { |
17 var b = 0; | 17 var b = 0; |
18 for(int i = 0; i < 10; i++) { | 18 for (int i = 0; i < 10; i++) { |
19 b += (await bar(1)) + (await baz(2)); | 19 b += (await bar(1)) + (await baz(2)); |
20 } | 20 } |
21 return b; | 21 return b; |
22 } | 22 } |
23 | 23 |
24 faa() async => (await bar('faa')).length; | 24 faa() async => (await bar('faa')).length; |
25 | 25 |
26 quaz(p) async { | 26 quaz(p) async { |
27 var x = 0; | 27 var x = 0; |
28 try { | 28 try { |
(...skipping 23 matching lines...) Expand all Loading... |
52 } | 52 } |
53 | 53 |
54 nesting() async { | 54 nesting() async { |
55 try { | 55 try { |
56 try { | 56 try { |
57 var x = 1; | 57 var x = 1; |
58 var y = () async { | 58 var y = () async { |
59 try { | 59 try { |
60 var z = (await bar(3)) + x; | 60 var z = (await bar(3)) + x; |
61 throw z; | 61 throw z; |
62 } catch (e1) { | 62 } catch (e1) { |
63 return e1; | 63 return e1; |
64 } | 64 } |
65 }; | 65 }; |
66 var a = await y(); | 66 var a = await y(); |
67 throw a; | 67 throw a; |
68 } catch (e2) { | 68 } catch (e2) { |
69 throw e2 + 1; | 69 throw e2 + 1; |
70 } | 70 } |
71 } catch (e3) { | 71 } catch (e3) { |
72 return e3; | 72 return e3; |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 awaitAsUnary(a,b) async { | 76 awaitAsUnary(a, b) async { |
77 return await a + await b; | 77 return await a + await b; |
78 } | 78 } |
79 | 79 |
80 awaitIf(p) async { | 80 awaitIf(p) async { |
81 if (p < (await bar(5))) { | 81 if (p < (await bar(5))) { |
82 return "p<5"; | 82 return "p<5"; |
83 } else { | 83 } else { |
84 return "p>=5"; | 84 return "p>=5"; |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 awaitNestedIf(p,q) async { | 88 awaitNestedIf(p, q) async { |
89 if (p == (await bar(5))) { | 89 if (p == (await bar(5))) { |
90 if (q < (await bar(7))) { | 90 if (q < (await bar(7))) { |
91 return "q<7"; | 91 return "q<7"; |
92 } else { | 92 } else { |
93 return "q>=7"; | 93 return "q>=7"; |
94 } | 94 } |
95 } else { | 95 } else { |
96 return "p!=5"; | 96 return "p!=5"; |
97 } | 97 } |
98 return "!"; | 98 return "!"; |
99 } | 99 } |
100 | 100 |
101 awaitElseIf(p) async { | 101 awaitElseIf(p) async { |
102 if (p > (await bar(5))) { | 102 if (p > (await bar(5))) { |
103 return "p>5"; | 103 return "p>5"; |
104 } else if (p < (await bar(5))) { | 104 } else if (p < (await bar(5))) { |
105 return "p<5"; | 105 return "p<5"; |
106 } else { | 106 } else { |
107 return "p==5"; | 107 return "p==5"; |
108 } | 108 } |
109 return "!"; | 109 return "!"; |
110 } | 110 } |
111 | 111 |
112 awaitReturn() async { | 112 awaitReturn() async { |
113 return await bar(17); | 113 return await bar(17); |
114 } | 114 } |
115 | 115 |
116 awaitSwitch() async { | 116 awaitSwitch() async { |
117 switch(await bar(3)) { | 117 switch (await bar(3)) { |
118 case 1: | 118 case 1: |
119 return 1; | 119 return 1; |
120 break; | 120 break; |
121 case 3: | 121 case 3: |
122 return 3; | 122 return 3; |
123 break; | 123 break; |
124 default: | 124 default: |
125 return -1; | 125 return -1; |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 awaitNestedWhile(int i, int j) async { | 129 awaitNestedWhile(int i, int j) async { |
130 int savedJ = j; | 130 int savedJ = j; |
131 var decI = () async { | 131 var decI = () async { |
132 return i--; | 132 return i--; |
133 }; | 133 }; |
134 var decJ = () async { | 134 var decJ = () async { |
135 return j--; | 135 return j--; |
136 }; | 136 }; |
137 var k = 0; | 137 var k = 0; |
138 while ((await decI()) > 0) { | 138 while ((await decI()) > 0) { |
139 j = savedJ; | 139 j = savedJ; |
140 while(0 < (await decJ())) { | 140 while (0 < (await decJ())) { |
141 k++; | 141 k++; |
142 } | 142 } |
143 } | 143 } |
144 return k; | 144 return k; |
145 } | 145 } |
146 | 146 |
147 awaitNestedDoWhile(int i, int j) async { | 147 awaitNestedDoWhile(int i, int j) async { |
148 int savedJ = j; | 148 int savedJ = j; |
149 var decI = () async { | 149 var decI = () async { |
150 return i--; | 150 return i--; |
151 }; | 151 }; |
152 var decJ = () async { | 152 var decJ = () async { |
153 return j--; | 153 return j--; |
154 }; | 154 }; |
155 var k = 0; | 155 var k = 0; |
156 do { | 156 do { |
157 do { | 157 do { |
158 k++; | 158 k++; |
159 } while (0 < (await decI())); | 159 } while (0 < (await decI())); |
160 } while((await decJ()) > 0); | 160 } while ((await decJ()) > 0); |
161 return k; | 161 return k; |
162 } | 162 } |
163 | 163 |
164 awaitFor() async { | 164 awaitFor() async { |
165 var asyncInc = (p) async => p+1; | 165 var asyncInc = (p) async => p + 1; |
166 var k = 0; | 166 var k = 0; |
167 for (int j = (await bar(0)), i = (await bar(1)); | 167 for (int j = (await bar(0)), i = (await bar(1)); |
168 j < (await bar(5)); | 168 j < (await bar(5)); |
169 j = (await asyncInc(j)), i = (await asyncInc(i))) { | 169 j = (await asyncInc(j)), i = (await asyncInc(i))) { |
170 k += i; | 170 k += i; |
171 k += j; | 171 k += j; |
172 } | 172 } |
173 return k; | 173 return k; |
174 } | 174 } |
175 | 175 |
176 awaitForIn() async { | 176 awaitForIn() async { |
177 var list = ['a', 'b', 'c']; | 177 var list = ['a', 'b', 'c']; |
178 var k = ''; | 178 var k = ''; |
179 for (var c in (await bar(list))) { | 179 for (var c in (await bar(list))) { |
(...skipping 12 matching lines...) Expand all Loading... |
192 result = await quaz(17); | 192 result = await quaz(17); |
193 Expect.equals(17, result); | 193 Expect.equals(17, result); |
194 result = await quazz(); | 194 result = await quazz(); |
195 Expect.equals(2, result); | 195 Expect.equals(2, result); |
196 result = await nesting(); | 196 result = await nesting(); |
197 Expect.equals(5, result); | 197 Expect.equals(5, result); |
198 result = await awaitIf(3); | 198 result = await awaitIf(3); |
199 Expect.equals("p<5", result); | 199 Expect.equals("p<5", result); |
200 result = await awaitIf(5); | 200 result = await awaitIf(5); |
201 Expect.equals("p>=5", result); | 201 Expect.equals("p>=5", result); |
202 result = await awaitNestedIf(5,3); | 202 result = await awaitNestedIf(5, 3); |
203 Expect.equals("q<7", result); | 203 Expect.equals("q<7", result); |
204 result = await awaitNestedIf(5,8); | 204 result = await awaitNestedIf(5, 8); |
205 Expect.equals("q>=7", result); | 205 Expect.equals("q>=7", result); |
206 result = await awaitNestedIf(3,8); | 206 result = await awaitNestedIf(3, 8); |
207 Expect.equals("p!=5", result); | 207 Expect.equals("p!=5", result); |
208 result = await awaitReturn(); | 208 result = await awaitReturn(); |
209 Expect.equals(17, result); | 209 Expect.equals(17, result); |
210 result = await awaitSwitch(); | 210 result = await awaitSwitch(); |
211 Expect.equals(3, result); | 211 Expect.equals(3, result); |
212 result = await awaitElseIf(6); | 212 result = await awaitElseIf(6); |
213 Expect.equals("p>5", result); | 213 Expect.equals("p>5", result); |
214 result = await awaitElseIf(4); | 214 result = await awaitElseIf(4); |
215 Expect.equals("p<5", result); | 215 Expect.equals("p<5", result); |
216 result = await awaitElseIf(5); | 216 result = await awaitElseIf(5); |
217 Expect.equals("p==5", result); | 217 Expect.equals("p==5", result); |
218 result = await awaitNestedWhile(5,3); | 218 result = await awaitNestedWhile(5, 3); |
219 Expect.equals(15, result); | 219 Expect.equals(15, result); |
220 result = await awaitNestedWhile(4,6); | 220 result = await awaitNestedWhile(4, 6); |
221 Expect.equals(24, result); | 221 Expect.equals(24, result); |
222 result = await awaitAsUnary(bar(1), bar(2)); | 222 result = await awaitAsUnary(bar(1), bar(2)); |
223 Expect.equals(3, result); | 223 Expect.equals(3, result); |
224 result = await awaitFor(); | 224 result = await awaitFor(); |
225 Expect.equals(25, result); | 225 Expect.equals(25, result); |
226 result = await awaitForIn(); | 226 result = await awaitForIn(); |
227 Expect.equals('abc', result); | 227 Expect.equals('abc', result); |
228 } | 228 } |
229 } | 229 } |
230 | 230 |
231 main() { | 231 main() { |
232 asyncStart(); | 232 asyncStart(); |
233 test().then((_) { | 233 test().then((_) { |
234 asyncEnd(); | 234 asyncEnd(); |
235 }); | 235 }); |
236 } | 236 } |
OLD | NEW |