| 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 testConstAsMap(List list) { |
| 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 |
| 56 void testFixedAsMap(List list) { |
| 57 testConstAsMap(list); |
| 58 |
| 59 Map<int, dynamic> map = list.asMap(); |
| 60 |
| 61 if (!list.isEmpty) { |
| 62 list[0] = 499; |
| 63 // Check again to make sure the map is backed by the list. |
| 64 testListMapCorrespondence(list, map); |
| 65 } |
| 66 } |
| 67 |
| 68 void testAsMap(List list) { |
| 69 testFixedAsMap(list); |
| 70 |
| 71 Map<int, dynamic> map = list.asMap(); |
| 72 |
| 73 Iterable keys = map.keys; |
| 74 Iterable values = map.values; |
| 75 |
| 76 list.add(42); |
| 77 // Check again to make sure the map is backed by the list and that the |
| 78 // length is not cached. |
| 79 testListMapCorrespondence(list, map); |
| 80 // Also check that the keys and values iterable from the map are backed by |
| 81 // the list. |
| 82 Expect.equals(list.length, keys.length); |
| 83 Expect.equals(values.length, values.length); |
| 84 } |
| 85 |
| 86 main() { |
| 87 testConstAsMap(const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 88 testAsMap([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 89 List list = new List(10); |
| 90 for (int i = 0; i < 10; i++) list[i] = i + 1; |
| 91 testFixedAsMap(list); |
| 92 |
| 93 testConstAsMap(const []); |
| 94 testAsMap([]); |
| 95 testFixedAsMap(new List(0)); |
| 96 } |
| OLD | NEW |