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