Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1204)

Side by Side Diff: tests/corelib/iterable_min_max_test.dart

Issue 11727007: Add min and max to Iterable and Stream. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 var 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.compare(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
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), Collections.min(cList, compareC));
58 Expect.equals(const C(0), new List.from(cList).min(compareC));
59 Expect.equals(const C(0), Collections.min(new List.from(cList), compareC));
60 Expect.equals(const C(0), new Set.from(cList).min(compareC));
61 Expect.equals(const C(0), Collections.min(new Set.from(cList), compareC));
62
63 Expect.equals(const C(10), cList.max(compareC));
64 Expect.equals(const C(10), Collections.max(cList, compareC));
65 Expect.equals(const C(10), new List.from(cList).max(compareC));
66 Expect.equals(const C(10), Collections.max(new List.from(cList), compareC));
67 Expect.equals(const C(10), new Set.from(cList).max(compareC));
68 Expect.equals(const C(10), Collections.max(new Set.from(cList), compareC));
69
70 Expect.throws(cList.min, (e) => e is NoSuchMethodError);
71 Expect.throws(cList.max, (e) => e is NoSuchMethodError);
72 }
73
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698