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