| 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 import "package:expect/expect.dart"; | |
| 6 | |
| 7 void testListMapCorrespondence(List list, Map map) { | |
| 8 Expect.equals(list.length, map.length); | |
| 9 for (int i = 0; i < list.length; i++) { | |
| 10 Expect.equals(list[i], map[i]); | |
| 11 } | |
| 12 Expect.isNull(map[list.length]); | |
| 13 Expect.isNull(map[-1]); | |
| 14 | |
| 15 Iterable keys = map.keys; | |
| 16 Iterable values = map.values; | |
| 17 Expect.isFalse(keys is List); | |
| 18 Expect.isFalse(values is List); | |
| 19 Expect.equals(list.length, keys.length); | |
| 20 Expect.equals(list.length, values.length); | |
| 21 for (int i = 0; i < list.length; i++) { | |
| 22 Expect.equals(i, keys.elementAt(i)); | |
| 23 Expect.equals(list[i], values.elementAt(i)); | |
| 24 } | |
| 25 | |
| 26 int forEachCount = 0; | |
| 27 map.forEach((key, value) { | |
| 28 Expect.equals(forEachCount, key); | |
| 29 Expect.equals(list[key], value); | |
| 30 forEachCount++; | |
| 31 }); | |
| 32 | |
| 33 for (int i = 0; i < list.length; i++) { | |
| 34 Expect.isTrue(map.containsKey(i)); | |
| 35 Expect.isTrue(map.containsValue(list[i])); | |
| 36 } | |
| 37 Expect.isFalse(map.containsKey(-1)); | |
| 38 Expect.isFalse(map.containsKey(list.length)); | |
| 39 | |
| 40 Expect.equals(list.length, forEachCount); | |
| 41 | |
| 42 Expect.equals(list.isEmpty, map.isEmpty); | |
| 43 } | |
| 44 | |
| 45 void testConstAsMap(List list) { | |
| 46 Map<int, dynamic> map = list.asMap(); | |
| 47 | |
| 48 testListMapCorrespondence(list, map); | |
| 49 | |
| 50 Expect.throws(() => map[0] = 499, (e) => e is UnsupportedError); | |
| 51 Expect.throws( | |
| 52 () => map.putIfAbsent(0, () => 499), (e) => e is UnsupportedError); | |
| 53 Expect.throws(() => map.clear(), (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 |