| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library iterable_min_max_test; | 5 library iterable_min_max_test; |
| 6 | 6 |
| 7 import "dart:collection"; | 7 import "dart:collection"; |
| 8 | 8 |
| 9 class C { | 9 class C { |
| 10 final x; | 10 final x; |
| 11 const C(this.x); | 11 const C(this.x); |
| 12 int get hashCode => x.hashCode; | 12 int get hashCode => x.hashCode; |
| 13 bool operator==(var other) => other is C && x == other.x; | 13 bool operator==(var other) => other is C && x == other.x; |
| 14 } | 14 } |
| 15 | 15 |
| 16 const inf = double.INFINITY; | 16 const inf = double.INFINITY; |
| 17 | 17 |
| 18 var intList = const [0, 1, -1, -5, 5, -1000, 1000, -7, 7]; | 18 var intList = const [0, 1, -1, -5, 5, -1000, 1000, -7, 7]; |
| 19 var doubleList = const [-0.0, 0.0, -1.0, 1.0, -1000.0, 1000.0, -inf, inf]; | 19 var doubleList = const [-0.0, 0.0, -1.0, 1.0, -1000.0, 1000.0, -inf, inf]; |
| 20 var stringList = const ["bbb", "bba", "bab", "abb", "bbc", "bcb", "cbb", "bb"]; | 20 var stringList = const ["bbb", "bba", "bab", "abb", "bbc", "bcb", "cbb", "bb"]; |
| 21 var cList = const [const C(5), const C(3), const C(8), | 21 var cList = const [const C(5), const C(3), const C(8), |
| 22 const C(0), const C(10), const C(6)]; | 22 const C(0), const C(10), const C(6)]; |
| 23 int compareC(C a, C b) => a.x.compareTo(b.x); | 23 int compareC(C a, C b) => a.x.compareTo(b.x); |
| 24 | 24 |
| 25 | 25 |
| 26 testMinMax(iterable, min, max) { | 26 testMinMax(iterable, min, max) { |
| 27 Expect.equals(min, iterable.min()); | 27 Expect.equals(min, iterable.min()); |
| 28 Expect.equals(min, iterable.min(Comparable.compare)); | 28 Expect.equals(min, iterable.min(Comparable.compare)); |
| 29 Expect.equals(min, Collections.min(iterable)); | 29 Expect.equals(min, IterableMixinWorkaround.min(iterable)); |
| 30 Expect.equals(min, Collections.min(iterable, Comparable.compare)); | 30 Expect.equals(min, IterableMixinWorkaround.min(iterable, Comparable.compare)); |
| 31 Expect.equals(max, iterable.min((a, b) => Comparable.compare(b, a))); | 31 Expect.equals(max, iterable.min((a, b) => Comparable.compare(b, a))); |
| 32 | 32 |
| 33 Expect.equals(max, iterable.max()); | 33 Expect.equals(max, iterable.max()); |
| 34 Expect.equals(max, iterable.max(Comparable.compare)); | 34 Expect.equals(max, iterable.max(Comparable.compare)); |
| 35 Expect.equals(max, Collections.max(iterable)); | 35 Expect.equals(max, IterableMixinWorkaround.max(iterable)); |
| 36 Expect.equals(max, Collections.max(iterable, Comparable.compare)); | 36 Expect.equals(max, IterableMixinWorkaround.max(iterable, Comparable.compare)); |
| 37 Expect.equals(min, iterable.max((a, b) => Comparable.compare(b, a))); | 37 Expect.equals(min, iterable.max((a, b) => Comparable.compare(b, a))); |
| 38 } | 38 } |
| 39 | 39 |
| 40 | 40 |
| 41 main() { | 41 main() { |
| 42 testMinMax(const [], null, null); | 42 testMinMax(const [], null, null); |
| 43 testMinMax([], null, null); | 43 testMinMax([], null, null); |
| 44 testMinMax(new Set(), null, null); | 44 testMinMax(new Set(), null, null); |
| 45 | 45 |
| 46 testMinMax(intList, -1000, 1000); // Const list. | 46 testMinMax(intList, -1000, 1000); // Const list. |
| 47 testMinMax(new List.from(intList), -1000, 1000); // Non-const list. | 47 testMinMax(new List.from(intList), -1000, 1000); // Non-const list. |
| 48 testMinMax(new Set.from(intList), -1000, 1000); // Set. | 48 testMinMax(new Set.from(intList), -1000, 1000); // Set. |
| 49 | 49 |
| 50 testMinMax(doubleList, -inf, inf); | 50 testMinMax(doubleList, -inf, inf); |
| 51 testMinMax(new List.from(doubleList), -inf, inf); | 51 testMinMax(new List.from(doubleList), -inf, inf); |
| 52 testMinMax(new Set.from(doubleList), -inf, inf); | 52 testMinMax(new Set.from(doubleList), -inf, inf); |
| 53 | 53 |
| 54 testMinMax(stringList, "abb", "cbb"); | 54 testMinMax(stringList, "abb", "cbb"); |
| 55 testMinMax(new List.from(stringList), "abb", "cbb"); | 55 testMinMax(new List.from(stringList), "abb", "cbb"); |
| 56 testMinMax(new Set.from(stringList), "abb", "cbb"); | 56 testMinMax(new Set.from(stringList), "abb", "cbb"); |
| 57 | 57 |
| 58 // Objects that are not Comparable. | 58 // Objects that are not Comparable. |
| 59 Expect.equals(const C(0), cList.min(compareC)); | 59 Expect.equals(const C(0), cList.min(compareC)); |
| 60 Expect.equals(const C(0), Collections.min(cList, compareC)); | 60 Expect.equals(const C(0), IterableMixinWorkaround.min(cList, compareC)); |
| 61 Expect.equals(const C(0), new List.from(cList).min(compareC)); | 61 Expect.equals(const C(0), new List.from(cList).min(compareC)); |
| 62 Expect.equals(const C(0), Collections.min(new List.from(cList), compareC)); | 62 Expect.equals(const C(0), IterableMixinWorkaround.min(new List.from(cList), co
mpareC)); |
| 63 Expect.equals(const C(0), new Set.from(cList).min(compareC)); | 63 Expect.equals(const C(0), new Set.from(cList).min(compareC)); |
| 64 Expect.equals(const C(0), Collections.min(new Set.from(cList), compareC)); | 64 Expect.equals(const C(0), IterableMixinWorkaround.min(new Set.from(cList), com
pareC)); |
| 65 | 65 |
| 66 Expect.equals(const C(10), cList.max(compareC)); | 66 Expect.equals(const C(10), cList.max(compareC)); |
| 67 Expect.equals(const C(10), Collections.max(cList, compareC)); | 67 Expect.equals(const C(10), IterableMixinWorkaround.max(cList, compareC)); |
| 68 Expect.equals(const C(10), new List.from(cList).max(compareC)); | 68 Expect.equals(const C(10), new List.from(cList).max(compareC)); |
| 69 Expect.equals(const C(10), Collections.max(new List.from(cList), compareC)); | 69 Expect.equals(const C(10), IterableMixinWorkaround.max(new List.from(cList), c
ompareC)); |
| 70 Expect.equals(const C(10), new Set.from(cList).max(compareC)); | 70 Expect.equals(const C(10), new Set.from(cList).max(compareC)); |
| 71 Expect.equals(const C(10), Collections.max(new Set.from(cList), compareC)); | 71 Expect.equals(const C(10), IterableMixinWorkaround.max(new Set.from(cList), co
mpareC)); |
| 72 | 72 |
| 73 bool checkedMode = false; | 73 bool checkedMode = false; |
| 74 assert(checkedMode = true); | 74 assert(checkedMode = true); |
| 75 Expect.throws(cList.min, (e) => checkedMode ? e is TypeError | 75 Expect.throws(cList.min, (e) => checkedMode ? e is TypeError |
| 76 : e is NoSuchMethodError); | 76 : e is NoSuchMethodError); |
| 77 Expect.throws(cList.max, (e) => checkedMode ? e is TypeError | 77 Expect.throws(cList.max, (e) => checkedMode ? e is TypeError |
| 78 : e is NoSuchMethodError); | 78 : e is NoSuchMethodError); |
| 79 } | 79 } |
| 80 | 80 |
| OLD | NEW |