Chromium Code Reviews| Index: tests/corelib/list_as_map_test.dart |
| diff --git a/tests/corelib/list_as_map_test.dart b/tests/corelib/list_as_map_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e230140478e91cad76a23ff2982666a9f26275ee |
| --- /dev/null |
| +++ b/tests/corelib/list_as_map_test.dart |
| @@ -0,0 +1,88 @@ |
| +// Copyright (c) 2013, 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. |
| + |
| +void testListMapCorrespondence(List list, Map map) { |
| + Expect.equals(list.length, map.length); |
| + for (int i = 0; i < list.length; i++) { |
| + Expect.equals(list[i], map[i]); |
| + } |
| + Expect.isNull(map[list.length]); |
| + Expect.isNull(map[-1]); |
| + |
| + Iterable keys = map.keys; |
| + Iterable values = map.values; |
| + Expect.isFalse(keys is List); |
| + Expect.isFalse(values is List); |
| + Expect.equals(list.length, keys.length); |
| + Expect.equals(list.length, values.length); |
| + for (int i = 0; i < list.length; i++) { |
| + Expect.equals(i, keys.elementAt(i)); |
| + Expect.equals(list[i], values.elementAt(i)); |
| + } |
| + |
| + int forEachCount = 0; |
| + map.forEach((key, value) { |
| + Expect.equals(forEachCount, key); |
| + Expect.equals(list[key], value); |
| + forEachCount++; |
| + }); |
| + |
| + for (int i = 0; i < list.length; i++) { |
| + Expect.isTrue(map.containsKey(i)); |
| + Expect.isTrue(map.containsValue(list[i])); |
| + } |
| + Expect.isFalse(map.containsKey(-1)); |
| + Expect.isFalse(map.containsKey(list.length)); |
| + |
| + Expect.equals(list.length, forEachCount); |
| + |
| + Expect.equals(list.isEmpty, map.isEmpty); |
| +} |
| + |
| +void testAsMap(List list, bool canBeModified, bool canGrow) { |
| + Map<int, dynamic> map = list.asMap(); |
| + |
| + testListMapCorrespondence(list, map); |
| + |
| + Expect.throws(() => map[0] = 499, |
| + (e) => e is UnsupportedError); |
| + Expect.throws(() => map.putIfAbsent(0, () => 499), |
| + (e) => e is UnsupportedError); |
| + Expect.throws(() => map.clear(), |
| + (e) => e is UnsupportedError); |
| + |
| + if (!canBeModified) return; |
|
Lasse Reichstein Nielsen
2013/03/04 08:28:37
Instead of passing anonymous booleans, you can hav
floitsch
2013/03/04 16:50:20
Done.
|
| + |
| + if (!list.isEmpty) { |
| + list[0] = 499; |
| + // Check again to make sure the map is backed by the list. |
| + testListMapCorrespondence(list, map); |
| + } |
| + |
| + if (!canGrow) return; |
| + |
| + Iterable keys = map.keys; |
| + Iterable values = map.values; |
| + |
| + list.add(42); |
| + // Check again to make sure the map is backed by the list and that the |
| + // length is not cached. |
| + testListMapCorrespondence(list, map); |
| + // Also check that the keys and values iterable from the map are backed by |
| + // the list. |
| + Expect.equals(list.length, keys.length); |
| + Expect.equals(values.length, values.length); |
| +} |
| + |
| +main() { |
| + testAsMap(const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], false, false); |
| + testAsMap([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], true, true); |
| + List list = new List(10); |
| + for (int i = 0; i < 10; i++) list[i] = i + 1; |
| + testAsMap(list, true, false); |
| + |
| + testAsMap(const [], false, false); |
| + testAsMap([], true, true); |
| + testAsMap(new List(0), true, false); |
| +} |