2011-10-05 06:22:36 +00:00
|
|
|
// Copyright (c) 2011, 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.
|
|
|
|
|
2021-04-26 17:58:57 +00:00
|
|
|
// @dart = 2.9
|
|
|
|
|
2013-04-05 19:43:16 +00:00
|
|
|
import "package:expect/expect.dart";
|
|
|
|
|
2011-10-05 06:22:36 +00:00
|
|
|
class ListIteratorsTest {
|
|
|
|
static void checkListIterator(List a) {
|
2013-01-07 11:23:16 +00:00
|
|
|
Iterator it = a.iterator;
|
2013-01-22 14:56:45 +00:00
|
|
|
Expect.isNull(it.current);
|
2011-10-05 06:22:36 +00:00
|
|
|
for (int i = 0; i < a.length; i++) {
|
2013-01-07 11:23:16 +00:00
|
|
|
Expect.isTrue(it.moveNext());
|
|
|
|
var elem = it.current;
|
|
|
|
Expect.equals(a[i], elem);
|
2011-10-05 06:22:36 +00:00
|
|
|
}
|
2013-01-07 11:23:16 +00:00
|
|
|
Expect.isFalse(it.moveNext());
|
|
|
|
Expect.isNull(it.current);
|
2011-10-05 06:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static testMain() {
|
|
|
|
checkListIterator([]);
|
|
|
|
checkListIterator([1, 2]);
|
2013-02-27 08:45:04 +00:00
|
|
|
checkListIterator(new List(0));
|
|
|
|
checkListIterator(new List(10));
|
2011-10-05 06:22:36 +00:00
|
|
|
checkListIterator(new List());
|
|
|
|
List g = new List();
|
2013-01-07 11:23:16 +00:00
|
|
|
g.addAll([1, 2, 3]);
|
2011-10-05 06:22:36 +00:00
|
|
|
checkListIterator(g);
|
|
|
|
|
2013-01-07 11:23:16 +00:00
|
|
|
// This is mostly undefined behavior.
|
|
|
|
Iterator it = g.iterator;
|
|
|
|
Expect.isTrue(it.moveNext());
|
|
|
|
Expect.equals(1, it.current);
|
|
|
|
Expect.isTrue(it.moveNext());
|
|
|
|
g[1] = 49;
|
|
|
|
// The iterator keeps the last value.
|
|
|
|
Expect.equals(2, it.current);
|
2013-01-22 14:56:45 +00:00
|
|
|
Expect.isTrue(it.moveNext());
|
2011-10-05 06:22:36 +00:00
|
|
|
g.removeLast();
|
2013-01-07 11:23:16 +00:00
|
|
|
// The iterator keeps the last value.
|
2013-01-22 14:56:45 +00:00
|
|
|
Expect.equals(3, it.current);
|
2013-01-24 07:07:25 +00:00
|
|
|
Expect.throws(it.moveNext, (e) => e is ConcurrentModificationError);
|
|
|
|
// No progress when throwing.
|
|
|
|
Expect.equals(3, it.current);
|
2011-10-05 06:22:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
ListIteratorsTest.testMain();
|
|
|
|
}
|