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 } |
| 121 |
120 typedef String intToString(int v); | 122 typedef String intToString(int v); |
121 typedef bool intToBool(int v); | 123 typedef bool intToBool(int v); |
122 | 124 |
123 // Test in checked mode with explicitly given types. | 125 // Test in checked mode with explicitly given types. |
124 void typedTest() { | 126 void typedTest() { |
125 bool isCheckedMode = false; | 127 bool isCheckedMode = false; |
126 assert((isCheckedMode = true)); | 128 assert((isCheckedMode = true)); |
127 if (!isCheckedMode) return; | 129 if (!isCheckedMode) return; |
128 | 130 |
129 // Assign functions to typed function variables. | 131 // Assign functions to typed function variables. |
130 intToString key = (int v) => "$v"; | 132 intToString key = (int v) => "$v"; |
131 intToBool value = (int v) => v.isOdd; | 133 intToBool value = (int v) => v.isOdd; |
132 Function id = (int i) => i; | 134 Function id = (int i) => i; |
133 | 135 |
134 Expect.throws(() { | 136 Expect.throws(() { |
135 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 137 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], key: key |
136 key: key | 138 // No "value" map, defaults to identity, which returns int, not bool. |
137 // No "value" map, defaults to identity, which returns int, not bool. | 139 ); |
138 ); | |
139 }); | 140 }); |
140 | 141 |
141 Expect.throws(() { | 142 Expect.throws(() { |
142 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 143 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], |
143 // No "key" map, defaults to identity, which returns int, not String. | 144 // No "key" map, defaults to identity, which returns int, not String. |
144 value: value | 145 value: value); |
145 ); | |
146 }); | 146 }); |
147 | 147 |
148 Expect.throws(() { | 148 Expect.throws(() { |
149 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 149 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], |
150 key: id as dynamic, // wrong type. | 150 key: id as dynamic, // wrong type. |
151 value: value | 151 value: value); |
152 ); | |
153 }); | 152 }); |
154 | 153 |
155 Expect.throws(() { | 154 Expect.throws(() { |
156 new SplayTreeMap<String,bool>.fromIterable(<int>[1, 2, 3], | 155 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], |
157 key: key, | 156 key: key, value: id as dynamic // wrong type. |
158 value: id as dynamic // wrong type. | 157 ); |
159 ); | |
160 }); | 158 }); |
161 | 159 |
162 // But it works with explicit types when used correctly. | 160 // But it works with explicit types when used correctly. |
163 SplayTreeMap<String, bool> map = | 161 SplayTreeMap<String, bool> map = new SplayTreeMap<String, bool>.fromIterable( |
164 new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], | 162 <int>[1, 2, 3], |
165 key: key, | 163 key: key, value: value); |
166 value: value); | |
167 Iterable<String> keys = map.keys; | 164 Iterable<String> keys = map.keys; |
168 Iterable<bool> values = map.values; | 165 Iterable<bool> values = map.values; |
169 List<String> keyList = keys.toList(); | 166 List<String> keyList = keys.toList(); |
170 List<bool> valueList = values.toList(); | 167 List<bool> valueList = values.toList(); |
171 Expect.equals(3, keyList.length); | 168 Expect.equals(3, keyList.length); |
172 Expect.equals(3, valueList.length); | 169 Expect.equals(3, valueList.length); |
173 Expect.equals(keys.first, map.firstKey()); | 170 Expect.equals(keys.first, map.firstKey()); |
174 Expect.equals(keys.last, map.lastKey()); | 171 Expect.equals(keys.last, map.lastKey()); |
175 } | 172 } |
OLD | NEW |