OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 // Simple test program for sync* generator functions. |
| 6 |
| 7 // VMOptions=--optimization_counter_threshold=10 |
| 8 |
| 9 import "package:expect/expect.dart"; |
| 10 |
| 11 sum10() sync* { |
| 12 var s = 0; |
| 13 for (var k = 1; k <= 10; k++) { |
| 14 s += k; |
| 15 yield s; |
| 16 } |
| 17 } |
| 18 |
| 19 class Range { |
| 20 int start, end; |
| 21 Range(this.start, this.end); |
| 22 elements() sync* { |
| 23 var e = start; |
| 24 while (e <= end) yield e++; |
| 25 } |
| 26 |
| 27 get yield sync* { // yield is a legal member name here. |
| 28 var e = start; |
| 29 while (e <= end) yield e++; |
| 30 } |
| 31 } |
| 32 |
| 33 get sync sync* { // sync is a legal identifier. |
| 34 yield "sync"; |
| 35 } |
| 36 |
| 37 einsZwei() sync* { |
| 38 yield* 1; |
| 39 yield* [2, 3]; |
| 40 yield* []; |
| 41 yield 5; |
| 42 yield [6]; |
| 43 } |
| 44 |
| 45 main() { |
| 46 for (int i = 0; i < 10; i++) { |
| 47 var sums = sum10(); |
| 48 print(sums); |
| 49 Expect.isTrue(sums is Iterable); |
| 50 Expect.equals(10, sums.length); |
| 51 Expect.equals(1, sums.first); |
| 52 Expect.equals(55, sums.last); |
| 53 var q = ""; |
| 54 for (var n in sums.take(3)) { |
| 55 q += "$n "; |
| 56 } |
| 57 Expect.equals("1 3 6 ", q); |
| 58 |
| 59 var r = new Range(10, 12); |
| 60 var elems1 = r.elements(); |
| 61 print(elems1); |
| 62 var elems2 = r.yield; |
| 63 print(elems2); |
| 64 // Walk the elements of each iterable and compare them. |
| 65 var i = elems1.iterator; |
| 66 Expect.isTrue(i is Iterator); |
| 67 elems2.forEach((e) { |
| 68 Expect.isTrue(i.moveNext()); |
| 69 Expect.equals(e, i.current); |
| 70 }); |
| 71 |
| 72 print(sync); |
| 73 Expect.equals("sync", sync.single); |
| 74 |
| 75 print(einsZwei()); |
| 76 Expect.equals("(1, 2, 3, 5, [6])", einsZwei().toString()); |
| 77 } |
| 78 } |
OLD | NEW |