Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, 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 void testListMapCorrespondence(List list, Map map) { | |
| 6 Expect.equals(list.length, map.length); | |
| 7 for (int i = 0; i < list.length; i++) { | |
| 8 Expect.equals(list[i], map[i]); | |
| 9 } | |
| 10 Expect.isNull(map[list.length]); | |
| 11 Expect.isNull(map[-1]); | |
| 12 | |
| 13 Iterable keys = map.keys; | |
| 14 Iterable values = map.values; | |
| 15 Expect.isFalse(keys is List); | |
| 16 Expect.isFalse(values is List); | |
| 17 Expect.equals(list.length, keys.length); | |
| 18 Expect.equals(list.length, values.length); | |
| 19 for (int i = 0; i < list.length; i++) { | |
| 20 Expect.equals(i, keys.elementAt(i)); | |
| 21 Expect.equals(list[i], values.elementAt(i)); | |
| 22 } | |
| 23 | |
| 24 int forEachCount = 0; | |
| 25 map.forEach((key, value) { | |
| 26 Expect.equals(forEachCount, key); | |
| 27 Expect.equals(list[key], value); | |
| 28 forEachCount++; | |
| 29 }); | |
| 30 | |
| 31 for (int i = 0; i < list.length; i++) { | |
| 32 Expect.isTrue(map.containsKey(i)); | |
| 33 Expect.isTrue(map.containsValue(list[i])); | |
| 34 } | |
| 35 Expect.isFalse(map.containsKey(-1)); | |
| 36 Expect.isFalse(map.containsKey(list.length)); | |
| 37 | |
| 38 Expect.equals(list.length, forEachCount); | |
| 39 | |
| 40 Expect.equals(list.isEmpty, map.isEmpty); | |
| 41 } | |
| 42 | |
| 43 void testAsMap(List list, bool canBeModified, bool canGrow) { | |
| 44 Map<int, dynamic> map = list.asMap(); | |
| 45 | |
| 46 testListMapCorrespondence(list, map); | |
| 47 | |
| 48 Expect.throws(() => map[0] = 499, | |
| 49 (e) => e is UnsupportedError); | |
| 50 Expect.throws(() => map.putIfAbsent(0, () => 499), | |
| 51 (e) => e is UnsupportedError); | |
| 52 Expect.throws(() => map.clear(), | |
| 53 (e) => e is UnsupportedError); | |
| 54 | |
| 55 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.
| |
| 56 | |
| 57 if (!list.isEmpty) { | |
| 58 list[0] = 499; | |
| 59 // Check again to make sure the map is backed by the list. | |
| 60 testListMapCorrespondence(list, map); | |
| 61 } | |
| 62 | |
| 63 if (!canGrow) return; | |
| 64 | |
| 65 Iterable keys = map.keys; | |
| 66 Iterable values = map.values; | |
| 67 | |
| 68 list.add(42); | |
| 69 // Check again to make sure the map is backed by the list and that the | |
| 70 // length is not cached. | |
| 71 testListMapCorrespondence(list, map); | |
| 72 // Also check that the keys and values iterable from the map are backed by | |
| 73 // the list. | |
| 74 Expect.equals(list.length, keys.length); | |
| 75 Expect.equals(values.length, values.length); | |
| 76 } | |
| 77 | |
| 78 main() { | |
| 79 testAsMap(const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], false, false); | |
| 80 testAsMap([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], true, true); | |
| 81 List list = new List(10); | |
| 82 for (int i = 0; i < 10; i++) list[i] = i + 1; | |
| 83 testAsMap(list, true, false); | |
| 84 | |
| 85 testAsMap(const [], false, false); | |
| 86 testAsMap([], true, true); | |
| 87 testAsMap(new List(0), true, false); | |
| 88 } | |
| OLD | NEW |