mirror of
https://github.com/dart-lang/sdk
synced 2024-09-19 21:01:50 +00:00
b101a7d002
Change-Id: Ib33169c3e0ffc870915c189404074a1dea472546 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196548 Reviewed-by: Bob Nystrom <rnystrom@google.com> Commit-Queue: Leaf Petersen <leafp@google.com>
93 lines
2 KiB
Dart
93 lines
2 KiB
Dart
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
// @dart = 2.9
|
|
|
|
// Simple test program for sync* generator functions.
|
|
|
|
// VMOptions=--optimization_counter_threshold=10
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
sum10() sync* {
|
|
var s = 0;
|
|
for (var k = 1; k <= 10; k++) {
|
|
s += k;
|
|
yield s;
|
|
}
|
|
}
|
|
|
|
class Range {
|
|
int start, end;
|
|
Range(this.start, this.end);
|
|
elements() sync* {
|
|
var e = start;
|
|
while (e <= end) yield e++;
|
|
}
|
|
|
|
get yield sync* {
|
|
// yield is a legal member name here.
|
|
var e = start;
|
|
while (e <= end) yield e++;
|
|
}
|
|
}
|
|
|
|
get sync sync* {
|
|
// sync is a legal identifier.
|
|
yield "sync";
|
|
}
|
|
|
|
einsZwei() sync* {
|
|
yield 1;
|
|
yield* [2, 3];
|
|
yield* [];
|
|
yield 5;
|
|
yield [6];
|
|
}
|
|
|
|
dreiVier() sync* {
|
|
// Throws type error: yielded object is not an iterable.
|
|
yield* 3;
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.YIELD_OF_INVALID_TYPE
|
|
// [cfe] A value of type 'int' can't be assigned to a variable of type 'Iterable<dynamic>'.
|
|
}
|
|
|
|
main() {
|
|
for (int i = 0; i < 10; i++) {
|
|
var sums = sum10();
|
|
print(sums);
|
|
Expect.isTrue(sums is Iterable);
|
|
Expect.equals(10, sums.length);
|
|
Expect.equals(1, sums.first);
|
|
Expect.equals(55, sums.last);
|
|
var q = "";
|
|
for (var n in sums.take(3)) {
|
|
q += "$n ";
|
|
}
|
|
Expect.equals("1 3 6 ", q);
|
|
|
|
var r = new Range(10, 12);
|
|
var elems1 = r.elements();
|
|
print(elems1);
|
|
var elems2 = r.yield;
|
|
print(elems2);
|
|
// Walk the elements of each iterable and compare them.
|
|
var i = elems1.iterator;
|
|
Expect.isTrue(i is Iterator);
|
|
elems2.forEach((e) {
|
|
Expect.isTrue(i.moveNext());
|
|
Expect.equals(e, i.current);
|
|
});
|
|
|
|
print(sync);
|
|
Expect.equals("sync", sync.single);
|
|
|
|
print(einsZwei());
|
|
Expect.equals("(1, 2, 3, 5, [6])", einsZwei().toString());
|
|
|
|
Expect.throws(() => dreiVier().toString());
|
|
}
|
|
}
|