Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Unified Diff: tests/corelib/growable_list_test.dart

Issue 485043002: Optimize List.toList/.sublist and List.from on lists. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/corelib/corelib.status ('k') | tests/corelib/iterable_return_type_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/corelib/growable_list_test.dart
diff --git a/tests/corelib/growable_list_test.dart b/tests/corelib/growable_list_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6f1b9a0e86a25c7c6c9aaedefd42a1a9ca0ecabf
--- /dev/null
+++ b/tests/corelib/growable_list_test.dart
@@ -0,0 +1,132 @@
+// 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.
+
+// Sanity check on the growing behavior of a growable list.
+
+import "package:expect/expect.dart";
+import "dart:collection" show IterableBase;
+
+// Iterable generating numbers in range [0..count).
+// May perform callback at some point underways.
+class TestIterableBase<E> extends IterableBase<E> {
+ final int length;
+ final int count;
+ // call [callback] if generating callbackIndex.
+ final int callbackIndex;
+ final Function callback;
+ TestIterableBase(this.length, this.count,
+ this.callbackIndex, this.callback);
+ Iterator<E> get iterator => new CallbackIterator(this);
+}
+
+class TestIterable<E> extends TestIterableBase<E> {
+ TestIterable(count, [callbackIndex = -1, callback])
+ : super(-1, count, callbackIndex, callback);
+ int get length => throw "SHOULD NOT BE CALLED";
+}
+
+// Implement Set for private EfficientLength interface.
+@proxy // Avoid warnings because we don't actually implement Set.
+class EfficientTestIterable<E> extends TestIterableBase<E>
+ implements Set<E> {
+ EfficientTestIterable(length, count, [callbackIndex = -1, callback])
+ : super(length, count, callbackIndex, callback);
+}
+
+class CallbackIterator<E> implements Iterator<E> {
+ TestIterableBase _iterable;
+ int _current = null;
+ int _nextIndex = 0;
+ CallbackIterator(this._iterable);
+ bool moveNext() {
+ if (_nextIndex >= _iterable.count) {
+ _current = null;
+ return false;
+ }
+ _current = _nextIndex;
+ _nextIndex++;
+ if (_current == _iterable.callbackIndex) {
+ _iterable.callback();
+ }
+ return true;
+ }
+ int get current => _current;
+}
+
+
+void main() {
+ // Without EfficientLength interface
+ {
+ // Change length of list after 20 additions.
+ var l = [];
+ var ci = new TestIterable<E>(257, 200, () {
+ l.add("X");
+ });
+ Expect.throws(() {
+ l.addAll(ci);
+ }, (e) => e is ConcurrentModificationError);
+ }
+
+ {
+ // Change length of list after 20 additions.
+ var l = [];
+ var ci = new TestIterable<E>(257, 200, () {
+ l.length = 0;
+ });
+ Expect.throws(() {
+ l.addAll(ci);
+ }, (e) => e is ConcurrentModificationError);
+ }
+
+ // With EfficientLength interface (uses length).
+ {
+ // Change length of list after 20 additions.
+ var l = [];
+ var ci = new EfficientTestIterable<E>(257, 257, 20, () {
+ l.add("X");
+ });
+ Expect.throws(() {
+ l.addAll(ci);
+ }, (e) => e is ConcurrentModificationError);
+ }
+
+ {
+ var l = [];
+ var ci = new EfficientTestIterable<E>(257, 257, 20, () {
+ l.length = 0;
+ });
+ Expect.throws(() {
+ l.addAll(ci);
+ }, (e) => e is ConcurrentModificationError);
+ }
+
+ {
+ // Length 50, only 25 elements.
+ var l = [];
+ var ci = new EfficientTestIterable<E>(500, 250);
+ l.addAll(ci);
+ Expect.listEquals(new List.generate(250, (x)=>x), l);
+ }
+
+ {
+ // Length 25, but 50 elements.
+ var l = [];
+ var ci = new EfficientTestIterable<E>(250, 500);
+ l.addAll(ci);
+ Expect.listEquals(new List.generate(500, (x)=>x), l);
+ }
+
+ {
+ // Adding to yourself.
+ var l = [1];
+ Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError);
+ }
+
+ {
+ // Adding to yourself.
+ var l = [1, 2, 3];
+ Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError);
+ }
+}
+
« no previous file with comments | « tests/corelib/corelib.status ('k') | tests/corelib/iterable_return_type_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698