OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 | 7 |
8 main() { | 8 main() { |
9 defaultFunctionValuesTest(); | 9 defaultFunctionValuesTest(); |
10 defaultKeyFunctionTest(); | 10 defaultKeyFunctionTest(); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 Expect.equals(2, map.length); | 103 Expect.equals(2, map.length); |
104 Expect.equals(2, map.keys.length); | 104 Expect.equals(2, map.keys.length); |
105 Expect.equals(2, map.values.length); | 105 Expect.equals(2, map.values.length); |
106 | 106 |
107 Expect.equals(1, map[2]); | 107 Expect.equals(1, map[2]); |
108 Expect.equals(2, map[3]); | 108 Expect.equals(2, map[3]); |
109 } | 109 } |
110 | 110 |
111 void genericTypeTest() { | 111 void genericTypeTest() { |
112 var map = new SplayTreeMap<int, String>.fromIterable([1, 2, 3], value: (x) =>
'$x'); | 112 var map = |
| 113 new SplayTreeMap<int, String>.fromIterable([1, 2, 3], value: (x) => '$x'); |
113 Expect.isTrue(map is Map<int, String>); | 114 Expect.isTrue(map is Map<int, String>); |
114 Expect.isTrue(map is SplayTreeMap<int, String>); | 115 Expect.isTrue(map is SplayTreeMap<int, String>); |
115 | 116 |
116 // Make sure it is not just SplayTreeMap<dynamic, dynamic>. | 117 // Make sure it is not just SplayTreeMap<dynamic, dynamic>. |
117 Expect.isFalse(map is SplayTreeMap<String, dynamic>); | 118 Expect.isFalse(map is SplayTreeMap<String, dynamic>); |
118 Expect.isFalse(map is SplayTreeMap<dynamic, int>); | 119 Expect.isFalse(map is SplayTreeMap<dynamic, int>); |
119 } | 120 } |
120 | 121 |
121 // Test in checked mode with explicitly given types. | 122 // Test in checked mode with explicitly given types. |
122 void typedTest() { | 123 void typedTest() { |
123 bool isCheckedMode = false; | 124 bool isCheckedMode = false; |
124 assert((isCheckedMode = true)); | 125 assert((isCheckedMode = true)); |
125 if (!isCheckedMode) return; | 126 if (!isCheckedMode) return; |
126 | 127 |
127 // Assign functions to untyped function variables. | 128 // Assign functions to untyped function variables. |
128 Function key = (int v) => "$v"; | 129 Function key = (int v) => "$v"; |
129 Function value = (int v) => v.isOdd; | 130 Function value = (int v) => v.isOdd; |
130 Function id = (int i) => i; | 131 Function id = (int i) => i; |
131 | 132 |
132 Expect.throws(() { | 133 Expect.throws(() { |
133 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 134 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], key: key |
134 key: key | 135 // No "value" map, defaults to identity, which returns int, not bool. |
135 // No "value" map, defaults to identity, which returns int, not bool. | 136 ); |
136 ); | |
137 }); | 137 }); |
138 | 138 |
139 Expect.throws(() { | 139 Expect.throws(() { |
140 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 140 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], |
141 // No "key" map, defaults to identity, which returns int, not String. | 141 // No "key" map, defaults to identity, which returns int, not String. |
142 value: value | 142 value: value); |
143 ); | |
144 }); | 143 }); |
145 | 144 |
146 Expect.throws(() { | 145 Expect.throws(() { |
147 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 146 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], |
148 key: id, // wrong type. | 147 key: id, // wrong type. |
149 value: value | 148 value: value); |
150 ); | |
151 }); | 149 }); |
152 | 150 |
153 Expect.throws(() { | 151 Expect.throws(() { |
154 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 152 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], |
155 key: key, | 153 key: key, value: id // wrong type. |
156 value: id // wrong type. | 154 ); |
157 ); | |
158 }); | 155 }); |
159 | 156 |
160 // But it works with explicit types when used correctly. | 157 // But it works with explicit types when used correctly. |
161 SplayTreeMap<String, bool> map = | 158 SplayTreeMap<String, bool> map = new SplayTreeMap<String, bool>.fromIterable( |
162 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], | 159 <int>[1, 2, 3], |
163 key: key, | 160 key: key, value: value); |
164 value: value); | |
165 Iterable<String> keys = map.keys; | 161 Iterable<String> keys = map.keys; |
166 Iterable<bool> values = map.values; | 162 Iterable<bool> values = map.values; |
167 List<String> keyList = keys.toList(); | 163 List<String> keyList = keys.toList(); |
168 List<bool> valueList = values.toList(); | 164 List<bool> valueList = values.toList(); |
169 Expect.equals(3, keyList.length); | 165 Expect.equals(3, keyList.length); |
170 Expect.equals(3, valueList.length); | 166 Expect.equals(3, valueList.length); |
171 Expect.equals(keys.first, map.firstKey()); | 167 Expect.equals(keys.first, map.firstKey()); |
172 Expect.equals(keys.last, map.lastKey()); | 168 Expect.equals(keys.last, map.lastKey()); |
173 } | 169 } |
OLD | NEW |