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