| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, 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 import "dart:typed_data"; | |
| 8 | |
| 9 main() { | |
| 10 var intTest = new Test<int>(); | |
| 11 intTest.run("ConstList", createConstList); | |
| 12 intTest.run("FixedList", createFixedList); | |
| 13 intTest.run("GrowableList", createGrowableList); | |
| 14 intTest.run("ConstMapKeys", createConstMapKeys); | |
| 15 intTest.run("ConstMapValues", createConstMapValues); | |
| 16 intTest.run("MapKeys", createMapKeys); | |
| 17 intTest.run("MapValues", createMapValues); | |
| 18 intTest.run("SplayMapKeys", createSplayMapKeys); | |
| 19 intTest.run("SplayMapValues", createSplayMapValues); | |
| 20 intTest.run("Set", createSet); | |
| 21 intTest.run("SplaySet", createSplaySet); | |
| 22 intTest.run("Queue", createQueue); | |
| 23 intTest.run("ListMapKeys", createListMapKeys); | |
| 24 intTest.run("ListMapValues", createListMapValues); | |
| 25 intTest.run("CodeUnits", createCodeUnits); | |
| 26 intTest.run("TypedList", createTypedList); | |
| 27 | |
| 28 new Test<String>().test("strings", ["a", "b", "c"]); | |
| 29 | |
| 30 new Test<num>().test("superclass", <int>[1, 2, 3]); | |
| 31 new Test<int>().test("subclass", <num>[1, 2, 3]); | |
| 32 } | |
| 33 | |
| 34 class Test<E> { | |
| 35 run(name, Iterable create(int size)) { | |
| 36 test(name, create(0)); | |
| 37 test(name, create(1)); | |
| 38 test(name, create(3)); | |
| 39 } | |
| 40 | |
| 41 test(name, iterable) { | |
| 42 testSingle(name, iterable); | |
| 43 testSingle("$name-where", iterable.where((x) => true)); | |
| 44 testSingle("$name-map", iterable.map((x) => x)); | |
| 45 testSingle("$name-expand", iterable.expand((x) => [x, x])); | |
| 46 testSingle("$name-skip", iterable.skip(1)); | |
| 47 testSingle("$name-take", iterable.take(2)); | |
| 48 testSingle("$name-skipWhile", iterable.skipWhile((x) => false)); | |
| 49 testSingle("$name-takeWhile", iterable.takeWhile((x) => true)); | |
| 50 } | |
| 51 | |
| 52 testSingle(name, iterable) { | |
| 53 var elements = iterable.toList(); | |
| 54 int length = elements.length; | |
| 55 | |
| 56 var list = new List<E>.unmodifiable(iterable); | |
| 57 | |
| 58 Expect.isTrue(list is List<E>, "$name-type-$E"); | |
| 59 Expect.isTrue(list is! List<Test>, "$name-!type-!$E"); | |
| 60 | |
| 61 checkElements() { | |
| 62 Expect.equals(length, list.length); | |
| 63 for (int i = 0; i < length; i++) { | |
| 64 Expect.identical(elements[i], list[i], "$name-identical-$i"); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 checkElements(); | |
| 69 | |
| 70 throws(funcName, func) { | |
| 71 try { | |
| 72 func(); | |
| 73 } catch (e, s) { | |
| 74 Expect.isTrue(e is UnsupportedError, "$name: $funcName threw $e"); | |
| 75 return; | |
| 76 } | |
| 77 checkElements(); | |
| 78 Expect.fail("$name: $funcName didn't throw"); | |
| 79 } | |
| 80 | |
| 81 throws("[]=", () { | |
| 82 list[0] = null; | |
| 83 }); | |
| 84 throws("length=", () { | |
| 85 list.length = length + 1; | |
| 86 }); | |
| 87 throws("length=", () { | |
| 88 list.length = length - 1; | |
| 89 }); | |
| 90 throws("setAll", () { | |
| 91 list.setAll(0, []); | |
| 92 }); | |
| 93 throws("add", () { | |
| 94 list.add(null); | |
| 95 }); | |
| 96 throws("insert", () { | |
| 97 list.insert(0, null); | |
| 98 }); | |
| 99 throws("insertAll", () { | |
| 100 list.insertAll(0, []); | |
| 101 }); | |
| 102 throws("addAll", () { | |
| 103 list.addAll([]); | |
| 104 }); | |
| 105 throws("remove", () { | |
| 106 list.remove(null); | |
| 107 }); | |
| 108 throws("removeWhere", () { | |
| 109 list.removeWhere((x) => true); | |
| 110 }); | |
| 111 throws("retainWhere", () { | |
| 112 list.retainWhere((x) => false); | |
| 113 }); | |
| 114 throws("sort", () { | |
| 115 list.sort(); | |
| 116 }); | |
| 117 throws("shuffle", () { | |
| 118 list.shuffle(); | |
| 119 }); | |
| 120 throws("clear", () { | |
| 121 list.clear(); | |
| 122 }); | |
| 123 throws("removeAt", () { | |
| 124 list.removeAt(0); | |
| 125 }); | |
| 126 throws("removeLast", () { | |
| 127 list.removeLast(); | |
| 128 }); | |
| 129 throws("setRange", () { | |
| 130 list.setRange(0, 1, []); | |
| 131 }); | |
| 132 throws("removeRange", () { | |
| 133 list.removeRange(0, 1); | |
| 134 }); | |
| 135 throws("replaceRange", () { | |
| 136 list.replaceRange(0, 1, []); | |
| 137 }); | |
| 138 throws("fillRange", () { | |
| 139 list.fillRange(0, 1, null); | |
| 140 }); | |
| 141 | |
| 142 success(opName, op(list)) { | |
| 143 var expect; | |
| 144 try { | |
| 145 expect = op(elements); | |
| 146 } catch (e) { | |
| 147 try { | |
| 148 op(list); | |
| 149 } catch (e2) { | |
| 150 Expect.equals( | |
| 151 e.runtimeType, | |
| 152 e2.runtimeType, | |
| 153 "$name :: $opName threw different exceptions: " | |
| 154 "${e.runtimeType} vs ${e2.runtimeType}"); | |
| 155 return; | |
| 156 } | |
| 157 Expect.fail("$name-$opName didn't throw, expected: $e"); | |
| 158 } | |
| 159 var actual = op(list); | |
| 160 checkElements(); | |
| 161 if (expect is List) { | |
| 162 Expect.listEquals(expect, actual, "$name-$opName"); | |
| 163 } else if (expect is Iterable) { | |
| 164 Expect.isTrue(actual is Iterable); | |
| 165 Expect.listEquals(expect.toList(), actual.toList(), "$name-$opName"); | |
| 166 } else { | |
| 167 Expect.equals(expect, actual, "$name-$opName"); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 success("indexOf", (l) => l.indexOf(null)); | |
| 172 success("lastIndexOf", (l) => l.lastIndexOf(null)); | |
| 173 success("contains", (l) => l.contains(2)); | |
| 174 success("elementAt", (l) => l.elementAt[1]); | |
| 175 success("reversed", (l) => l.reversed); | |
| 176 success("sublist0-1", (l) => l.sublist(0, 1)); | |
| 177 success("getRange0-1", (l) => l.getRange(0, 1)); | |
| 178 success("asMap-keys", (l) => l.asMap().keys); | |
| 179 success("asMap-values", (l) => l.asMap().values); | |
| 180 success("where", (l) => l.where((x) => true)); | |
| 181 success("map", (l) => l.map((x) => x)); | |
| 182 success("expand", (l) => l.expand((x) => [x, x])); | |
| 183 success("skip", (l) => l.skip(1)); | |
| 184 success("take", (l) => l.take(1)); | |
| 185 success("skipWhile", (l) => l.skipWhile((x) => false)); | |
| 186 success("takeWhile", (l) => l.takeWhile((x) => true)); | |
| 187 success("first", (l) => l.first); | |
| 188 success("last", (l) => l.last); | |
| 189 success("single", (l) => l.single); | |
| 190 success("firstWhere", (l) => l.firstWhere((x) => true)); | |
| 191 success("lastWhere", (l) => l.lastWhere((x) => true)); | |
| 192 success("singleWhere", (l) => l.singleWhere((x) => true)); | |
| 193 success("isEmpty", (l) => l.isEmpty); | |
| 194 success("isNotEmpty", (l) => l.isNotEmpty); | |
| 195 success("join", (l) => l.join("/")); | |
| 196 success("fold", (l) => l.fold("--", (a, b) => "$a/$b")); | |
| 197 success("reduce", (l) => l.reduce((a, b) => a + b)); | |
| 198 success("every", (l) => l.every((x) => x == 0)); | |
| 199 success("any", (l) => l.any((x) => x == 2)); | |
| 200 success("toList", (l) => l.toList()); | |
| 201 success("toSet", (l) => l.toSet()); | |
| 202 success("toString", (l) => l.toString()); | |
| 203 | |
| 204 var it = elements.iterator; | |
| 205 list.forEach((v) { | |
| 206 Expect.isTrue(it.moveNext()); | |
| 207 Expect.equals(it.current, v); | |
| 208 }); | |
| 209 Expect.isFalse(it.moveNext()); | |
| 210 | |
| 211 if (elements is List<int> && list is List<int>) { | |
| 212 success("String.fromCharCodes", (l) => new String.fromCharCodes(l)); | |
| 213 } | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 createConstList(n) { | |
| 218 if (n == 0) return const <int>[]; | |
| 219 return const <int>[1, 2, 3]; | |
| 220 } | |
| 221 | |
| 222 createFixedList(n) { | |
| 223 var result = new List<int>(n); | |
| 224 for (int i = 0; i < n; i++) result[i] = n; | |
| 225 return result; | |
| 226 } | |
| 227 | |
| 228 createGrowableList(n) { | |
| 229 var result = new List<int>()..length = n; | |
| 230 for (int i = 0; i < n; i++) result[i] = n; | |
| 231 return result; | |
| 232 } | |
| 233 | |
| 234 createIterable(n) => new Iterable.generate(n); | |
| 235 createConstMapKeys(n) { | |
| 236 if (n == 0) return const <int, int>{}.keys; | |
| 237 return const <int, int>{0: 0, 1: 1, 2: 2}.keys; | |
| 238 } | |
| 239 | |
| 240 createConstMapValues(n) { | |
| 241 if (n == 0) return const <int, int>{}.values; | |
| 242 return const <int, int>{0: 0, 1: 1, 2: 2}.values; | |
| 243 } | |
| 244 | |
| 245 createMapKeys(n) { | |
| 246 var map = <int, int>{}; | |
| 247 for (int i = 0; i < n; i++) map[i] = i; | |
| 248 return map.keys; | |
| 249 } | |
| 250 | |
| 251 createMapValues(n) { | |
| 252 var map = <int, int>{}; | |
| 253 for (int i = 0; i < n; i++) map[i] = i; | |
| 254 return map.values; | |
| 255 } | |
| 256 | |
| 257 createSplayMapKeys(n) { | |
| 258 var map = new SplayTreeMap<int, int>(); | |
| 259 for (int i = 0; i < n; i++) map[i] = i; | |
| 260 return map.keys; | |
| 261 } | |
| 262 | |
| 263 createSplayMapValues(n) { | |
| 264 var map = new SplayTreeMap<int, int>(); | |
| 265 for (int i = 0; i < n; i++) map[i] = i; | |
| 266 return map.values; | |
| 267 } | |
| 268 | |
| 269 createSet(n) { | |
| 270 var set = new Set<int>(); | |
| 271 for (int i = 0; i < n; i++) set.add(i); | |
| 272 return set; | |
| 273 } | |
| 274 | |
| 275 createSplaySet(n) { | |
| 276 var set = new SplayTreeSet<int>(); | |
| 277 for (int i = 0; i < n; i++) set.add(i); | |
| 278 return set; | |
| 279 } | |
| 280 | |
| 281 createQueue(n) { | |
| 282 var queue = new Queue<int>(); | |
| 283 for (int i = 0; i < n; i++) queue.add(i); | |
| 284 return queue; | |
| 285 } | |
| 286 | |
| 287 createListMapKeys(n) { | |
| 288 return createGrowableList(n).asMap().keys; | |
| 289 } | |
| 290 | |
| 291 createListMapValues(n) { | |
| 292 return createGrowableList(n).asMap().values; | |
| 293 } | |
| 294 | |
| 295 createCodeUnits(n) { | |
| 296 var string = new String.fromCharCodes(new Iterable.generate(n)); | |
| 297 return string.codeUnits; | |
| 298 } | |
| 299 | |
| 300 createTypedList(n) { | |
| 301 var tl = new Uint8List(n); | |
| 302 for (int i = 0; i < n; i++) tl[i] = i; | |
| 303 return tl; | |
| 304 } | |
| OLD | NEW |