| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 import 'dart:collection'; | |
| 7 | |
| 8 main() { | |
| 9 defaultFunctionValuesTest(); | |
| 10 defaultKeyFunctionTest(); | |
| 11 defaultValueFunctionTest(); | |
| 12 noDefaultValuesTest(); | |
| 13 emptyIterableTest(); | |
| 14 equalElementsTest(); | |
| 15 genericTypeTest(); | |
| 16 typedTest(); | |
| 17 } | |
| 18 | |
| 19 void defaultFunctionValuesTest() { | |
| 20 var map = new SplayTreeMap.fromIterable([1, 2, 3]); | |
| 21 | |
| 22 Expect.isTrue(map is Map); | |
| 23 Expect.isTrue(map is SplayTreeMap); | |
| 24 Expect.isFalse(map is HashMap); | |
| 25 | |
| 26 Expect.equals(3, map.length); | |
| 27 Expect.equals(3, map.keys.length); | |
| 28 Expect.equals(3, map.values.length); | |
| 29 | |
| 30 Expect.equals(1, map[1]); | |
| 31 Expect.equals(2, map[2]); | |
| 32 Expect.equals(3, map[3]); | |
| 33 } | |
| 34 | |
| 35 void defaultKeyFunctionTest() { | |
| 36 var map = new SplayTreeMap.fromIterable([1, 2, 3], value: (x) => x + 1); | |
| 37 | |
| 38 Expect.isTrue(map is Map); | |
| 39 Expect.isTrue(map is SplayTreeMap); | |
| 40 Expect.isFalse(map is HashMap); | |
| 41 | |
| 42 Expect.equals(3, map.length); | |
| 43 Expect.equals(3, map.keys.length); | |
| 44 Expect.equals(3, map.values.length); | |
| 45 | |
| 46 Expect.equals(2, map[1]); | |
| 47 Expect.equals(3, map[2]); | |
| 48 Expect.equals(4, map[3]); | |
| 49 } | |
| 50 | |
| 51 void defaultValueFunctionTest() { | |
| 52 var map = new SplayTreeMap.fromIterable([1, 2, 3], key: (x) => x + 1); | |
| 53 | |
| 54 Expect.isTrue(map is Map); | |
| 55 Expect.isTrue(map is SplayTreeMap); | |
| 56 Expect.isFalse(map is HashMap); | |
| 57 | |
| 58 Expect.equals(3, map.length); | |
| 59 Expect.equals(3, map.keys.length); | |
| 60 Expect.equals(3, map.values.length); | |
| 61 | |
| 62 Expect.equals(1, map[2]); | |
| 63 Expect.equals(2, map[3]); | |
| 64 Expect.equals(3, map[4]); | |
| 65 } | |
| 66 | |
| 67 void noDefaultValuesTest() { | |
| 68 var map = new SplayTreeMap.fromIterable([1, 2, 3], | |
| 69 key: (x) => x + 1, value: (x) => x - 1); | |
| 70 | |
| 71 Expect.isTrue(map is Map); | |
| 72 Expect.isTrue(map is SplayTreeMap); | |
| 73 Expect.isFalse(map is HashMap); | |
| 74 | |
| 75 Expect.equals(3, map.length); | |
| 76 Expect.equals(3, map.keys.length); | |
| 77 Expect.equals(3, map.values.length); | |
| 78 | |
| 79 Expect.equals(0, map[2]); | |
| 80 Expect.equals(1, map[3]); | |
| 81 Expect.equals(2, map[4]); | |
| 82 } | |
| 83 | |
| 84 void emptyIterableTest() { | |
| 85 var map = new SplayTreeMap.fromIterable([]); | |
| 86 | |
| 87 Expect.isTrue(map is Map); | |
| 88 Expect.isTrue(map is SplayTreeMap); | |
| 89 Expect.isFalse(map is HashMap); | |
| 90 | |
| 91 Expect.equals(0, map.length); | |
| 92 Expect.equals(0, map.keys.length); | |
| 93 Expect.equals(0, map.values.length); | |
| 94 } | |
| 95 | |
| 96 void equalElementsTest() { | |
| 97 var map = new SplayTreeMap.fromIterable([1, 2, 2], key: (x) => x + 1); | |
| 98 | |
| 99 Expect.isTrue(map is Map); | |
| 100 Expect.isTrue(map is SplayTreeMap); | |
| 101 Expect.isFalse(map is HashMap); | |
| 102 | |
| 103 Expect.equals(2, map.length); | |
| 104 Expect.equals(2, map.keys.length); | |
| 105 Expect.equals(2, map.values.length); | |
| 106 | |
| 107 Expect.equals(1, map[2]); | |
| 108 Expect.equals(2, map[3]); | |
| 109 } | |
| 110 | |
| 111 void genericTypeTest() { | |
| 112 var map = | |
| 113 new SplayTreeMap<int, String>.fromIterable([1, 2, 3], value: (x) => '$x'); | |
| 114 Expect.isTrue(map is Map<int, String>); | |
| 115 Expect.isTrue(map is SplayTreeMap<int, String>); | |
| 116 | |
| 117 // Make sure it is not just SplayTreeMap<dynamic, dynamic>. | |
| 118 Expect.isFalse(map is SplayTreeMap<String, dynamic>); | |
| 119 Expect.isFalse(map is SplayTreeMap<dynamic, int>); | |
| 120 } | |
| 121 | |
| 122 typedef String intToString(int v); | |
| 123 typedef bool intToBool(int v); | |
| 124 | |
| 125 // Test in checked mode with explicitly given types. | |
| 126 void typedTest() { | |
| 127 bool isCheckedMode = false; | |
| 128 assert((isCheckedMode = true)); | |
| 129 if (!isCheckedMode) return; | |
| 130 | |
| 131 // Assign functions to typed function variables. | |
| 132 intToString key = (int v) => "$v"; | |
| 133 intToBool value = (int v) => v.isOdd; | |
| 134 Function id = (int i) => i; | |
| 135 | |
| 136 Expect.throws(() { | |
| 137 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], key: key | |
| 138 // No "value" map, defaults to identity, which returns int, not bool. | |
| 139 ); | |
| 140 }); | |
| 141 | |
| 142 Expect.throws(() { | |
| 143 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], | |
| 144 // No "key" map, defaults to identity, which returns int, not String. | |
| 145 value: value); | |
| 146 }); | |
| 147 | |
| 148 Expect.throws(() { | |
| 149 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], | |
| 150 key: id as dynamic, // wrong type. | |
| 151 value: value); | |
| 152 }); | |
| 153 | |
| 154 Expect.throws(() { | |
| 155 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], | |
| 156 key: key, value: id as dynamic // wrong type. | |
| 157 ); | |
| 158 }); | |
| 159 | |
| 160 // But it works with explicit types when used correctly. | |
| 161 SplayTreeMap<String, bool> map = new SplayTreeMap<String, bool>.fromIterable( | |
| 162 <int>[1, 2, 3], | |
| 163 key: key, value: value); | |
| 164 Iterable<String> keys = map.keys; | |
| 165 Iterable<bool> values = map.values; | |
| 166 List<String> keyList = keys.toList(); | |
| 167 List<bool> valueList = values.toList(); | |
| 168 Expect.equals(3, keyList.length); | |
| 169 Expect.equals(3, valueList.length); | |
| 170 Expect.equals(keys.first, map.firstKey()); | |
| 171 Expect.equals(keys.last, map.lastKey()); | |
| 172 } | |
| OLD | NEW |