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 min(Iterable source, int compare(var a, var b)) { |
| 17 Iterator iterator = source.iterator; |
| 18 if (!iterator.moveNext()) throw new StateError("no values"); |
| 19 var min = iterator.current; |
| 20 while (iterator.moveNext()) { |
| 21 if (compare(min, iterator.current) > 0) min = iterator.current; |
| 22 } |
| 23 return iterator.current; |
| 24 } |
| 25 |
| 26 max(Iterable source, int compare(var a, var b)) { |
| 27 Iterator iterator = source.iterator; |
| 28 if (!iterator.moveNext()) throw new StateError("no values"); |
| 29 var max = iterator.current; |
| 30 while (iterator.moveNext()) { |
| 31 if (compare(min, iterator.current) < 0) max = iterator.current; |
| 32 } |
| 33 return iterator.current; |
| 34 } |
| 35 |
16 const inf = double.INFINITY; | 36 const inf = double.INFINITY; |
17 | 37 |
18 var intList = const [0, 1, -1, -5, 5, -1000, 1000, -7, 7]; | 38 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]; | 39 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"]; | 40 var stringList = const ["bbb", "bba", "bab", "abb", "bbc", "bcb", "cbb", "bb"]; |
21 var cList = const [const C(5), const C(3), const C(8), | 41 var cList = const [const C(5), const C(3), const C(8), |
22 const C(0), const C(10), const C(6)]; | 42 const C(0), const C(10), const C(6)]; |
23 int compareC(C a, C b) => a.x.compareTo(b.x); | 43 int compareC(C a, C b) => a.x.compareTo(b.x); |
24 | 44 |
25 | 45 |
26 testMinMax(iterable, min, max) { | 46 testMinMax(iterable, min, max) { |
27 Expect.equals(min, iterable.min()); | 47 Expect.equals(min, iterable.min()); |
28 Expect.equals(min, iterable.min(Comparable.compare)); | 48 Expect.equals(min, iterable.min(Comparable.compare)); |
29 Expect.equals(min, IterableMixinWorkaround.min(iterable)); | 49 Expect.equals(min, min(iterable)); |
30 Expect.equals(min, IterableMixinWorkaround.min(iterable, Comparable.compare)); | 50 Expect.equals(min, min(iterable, Comparable.compare)); |
31 Expect.equals(max, iterable.min((a, b) => Comparable.compare(b, a))); | 51 Expect.equals(max, iterable.min((a, b) => Comparable.compare(b, a))); |
32 | 52 |
33 Expect.equals(max, iterable.max()); | 53 Expect.equals(max, iterable.max()); |
34 Expect.equals(max, iterable.max(Comparable.compare)); | 54 Expect.equals(max, iterable.max(Comparable.compare)); |
35 Expect.equals(max, IterableMixinWorkaround.max(iterable)); | 55 Expect.equals(max, max(iterable)); |
36 Expect.equals(max, IterableMixinWorkaround.max(iterable, Comparable.compare)); | 56 Expect.equals(max, max(iterable, Comparable.compare)); |
37 Expect.equals(min, iterable.max((a, b) => Comparable.compare(b, a))); | 57 Expect.equals(min, iterable.max((a, b) => Comparable.compare(b, a))); |
38 } | 58 } |
39 | 59 |
40 | 60 |
41 main() { | 61 main() { |
42 testMinMax(const [], null, null); | 62 testMinMax(const [], null, null); |
43 testMinMax([], null, null); | 63 testMinMax([], null, null); |
44 testMinMax(new Set(), null, null); | 64 testMinMax(new Set(), null, null); |
45 | 65 |
46 testMinMax(intList, -1000, 1000); // Const list. | 66 testMinMax(intList, -1000, 1000); // Const list. |
47 testMinMax(new List.from(intList), -1000, 1000); // Non-const list. | 67 testMinMax(new List.from(intList), -1000, 1000); // Non-const list. |
48 testMinMax(new Set.from(intList), -1000, 1000); // Set. | 68 testMinMax(new Set.from(intList), -1000, 1000); // Set. |
49 | 69 |
50 testMinMax(doubleList, -inf, inf); | 70 testMinMax(doubleList, -inf, inf); |
51 testMinMax(new List.from(doubleList), -inf, inf); | 71 testMinMax(new List.from(doubleList), -inf, inf); |
52 testMinMax(new Set.from(doubleList), -inf, inf); | 72 testMinMax(new Set.from(doubleList), -inf, inf); |
53 | 73 |
54 testMinMax(stringList, "abb", "cbb"); | 74 testMinMax(stringList, "abb", "cbb"); |
55 testMinMax(new List.from(stringList), "abb", "cbb"); | 75 testMinMax(new List.from(stringList), "abb", "cbb"); |
56 testMinMax(new Set.from(stringList), "abb", "cbb"); | 76 testMinMax(new Set.from(stringList), "abb", "cbb"); |
57 | 77 |
58 // Objects that are not Comparable. | 78 // Objects that are not Comparable. |
59 Expect.equals(const C(0), cList.min(compareC)); | 79 Expect.equals(const C(0), cList.min(compareC)); |
60 Expect.equals(const C(0), IterableMixinWorkaround.min(cList, compareC)); | 80 Expect.equals(const C(0), min(cList, compareC)); |
61 Expect.equals(const C(0), new List.from(cList).min(compareC)); | 81 Expect.equals(const C(0), new List.from(cList).min(compareC)); |
62 Expect.equals(const C(0), IterableMixinWorkaround.min(new List.from(cList), co
mpareC)); | 82 Expect.equals(const C(0), min(new List.from(cList), compareC)); |
63 Expect.equals(const C(0), new Set.from(cList).min(compareC)); | 83 Expect.equals(const C(0), new Set.from(cList).min(compareC)); |
64 Expect.equals(const C(0), IterableMixinWorkaround.min(new Set.from(cList), com
pareC)); | 84 Expect.equals(const C(0), min(new Set.from(cList), compareC)); |
65 | 85 |
66 Expect.equals(const C(10), cList.max(compareC)); | 86 Expect.equals(const C(10), cList.max(compareC)); |
67 Expect.equals(const C(10), IterableMixinWorkaround.max(cList, compareC)); | 87 Expect.equals(const C(10), max(cList, compareC)); |
68 Expect.equals(const C(10), new List.from(cList).max(compareC)); | 88 Expect.equals(const C(10), new List.from(cList).max(compareC)); |
69 Expect.equals(const C(10), IterableMixinWorkaround.max(new List.from(cList), c
ompareC)); | 89 Expect.equals(const C(10), max(new List.from(cList), compareC)); |
70 Expect.equals(const C(10), new Set.from(cList).max(compareC)); | 90 Expect.equals(const C(10), new Set.from(cList).max(compareC)); |
71 Expect.equals(const C(10), IterableMixinWorkaround.max(new Set.from(cList), co
mpareC)); | 91 Expect.equals(const C(10), max(new Set.from(cList), compareC)); |
72 | 92 |
73 bool checkedMode = false; | 93 bool checkedMode = false; |
74 assert(checkedMode = true); | 94 assert(checkedMode = true); |
75 Expect.throws(cList.min, (e) => checkedMode ? e is TypeError | 95 Expect.throws(cList.min, (e) => checkedMode ? e is TypeError |
76 : e is NoSuchMethodError); | 96 : e is NoSuchMethodError); |
77 Expect.throws(cList.max, (e) => checkedMode ? e is TypeError | 97 Expect.throws(cList.max, (e) => checkedMode ? e is TypeError |
78 : e is NoSuchMethodError); | 98 : e is NoSuchMethodError); |
79 } | 99 } |
80 | |
OLD | NEW |