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