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

Side by Side Diff: packages/collection/test/equality_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
OLDNEW
(Empty)
1 // Copyright (c) 2013, 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 // Tests equality utilities.
6
7 import "dart:collection";
8 import "package:collection/collection.dart";
9 import "package:test/test.dart";
10
11 main() {
12 o(id) => new Element(id);
13
14 // Lists that are point-wise equal, but not identical.
15 var list1 = [o(1), o(2), o(3), o(4), o(5)];
16 var list2 = [o(1), o(2), o(3), o(4), o(5)];
17 // Similar length list with equal elements in different order.
18 var list3 = [o(1), o(3), o(5), o(4), o(2)];
19
20 test("IterableEquality - List", () {
21 expect(const IterableEquality().equals(list1, list2), isTrue);
22 Equality iterId = const IterableEquality(const IdentityEquality());
23 expect(iterId.equals(list1, list2), isFalse);
24 });
25
26 test("IterableEquality - LinkedSet", () {
27 var l1 = new LinkedHashSet.from(list1);
28 var l2 = new LinkedHashSet.from(list2);
29 expect(const IterableEquality().equals(l1, l2), isTrue);
30 Equality iterId = const IterableEquality(const IdentityEquality());
31 expect(iterId.equals(l1, l2), isFalse);
32 });
33
34 test("ListEquality", () {
35 expect(const ListEquality().equals(list1, list2),
36 isTrue);
37 Equality listId = const ListEquality(const IdentityEquality());
38 expect(listId.equals(list1, list2), isFalse);
39 });
40
41 test("ListInequality length", () {
42 var list4 = [o(1), o(2), o(3), o(4), o(5), o(6)];
43 expect(const ListEquality().equals(list1, list4),
44 isFalse);
45 expect(const ListEquality(const IdentityEquality()).equals(list1, list4),
46 isFalse);
47 });
48
49 test("ListInequality value", () {
50 var list5 = [o(1), o(2), o(3), o(4), o(6)];
51 expect(const ListEquality().equals(list1, list5),
52 isFalse);
53 expect(const ListEquality(const IdentityEquality()).equals(list1, list5),
54 isFalse);
55 });
56
57 test("UnorderedIterableEquality", () {
58 expect(const UnorderedIterableEquality().equals(list1, list3),
59 isTrue);
60 Equality uniterId =
61 const UnorderedIterableEquality(const IdentityEquality());
62 expect(uniterId.equals(list1, list3), isFalse);
63 });
64
65 test("UnorderedIterableInequality length", () {
66 var list6 = [o(1), o(3), o(5), o(4), o(2), o(1)];
67 expect(const UnorderedIterableEquality().equals(list1, list6),
68 isFalse);
69 expect(const UnorderedIterableEquality(const IdentityEquality())
70 .equals(list1, list6),
71 isFalse);
72 });
73
74 test("UnorderedIterableInequality values", () {
75 var list7 = [o(1), o(3), o(5), o(4), o(6)];
76 expect(const UnorderedIterableEquality().equals(list1, list7),
77 isFalse);
78 expect(const UnorderedIterableEquality(const IdentityEquality())
79 .equals(list1, list7),
80 isFalse);
81 });
82
83 test("SetEquality", () {
84 var set1 = new HashSet.from(list1);
85 var set2 = new LinkedHashSet.from(list3);
86 expect(const SetEquality().equals(set1, set2), isTrue);
87 Equality setId = const SetEquality(const IdentityEquality());
88 expect(setId.equals(set1, set2), isFalse);
89 });
90
91 test("SetInequality length", () {
92 var list8 = [o(1), o(3), o(5), o(4), o(2), o(6)];
93 var set1 = new HashSet.from(list1);
94 var set2 = new LinkedHashSet.from(list8);
95 expect(const SetEquality().equals(set1, set2),
96 isFalse);
97 expect(const SetEquality(const IdentityEquality()).equals(set1, set2),
98 isFalse);
99 });
100
101 test("SetInequality value", () {
102 var list7 = [o(1), o(3), o(5), o(4), o(6)];
103 var set1 = new HashSet.from(list1);
104 var set2 = new LinkedHashSet.from(list7);
105 expect(const SetEquality().equals(set1, set2),
106 isFalse);
107 expect(const SetEquality(const IdentityEquality()).equals(set1, set2),
108 isFalse);
109 });
110
111 var map1a = {"x": [o(1), o(2), o(3)], "y": [true, false, null]};
112 var map1b = {"x": [o(4), o(5), o(6)], "y": [false, true, null]};
113 var map2a = {"x": [o(3), o(2), o(1)], "y": [false, true, null]};
114 var map2b = {"x": [o(6), o(5), o(4)], "y": [null, false, true]};
115 var l1 = [map1a, map1b];
116 var l2 = [map2a, map2b];
117 var s1 = new Set.from(l1);
118 var s2 = new Set.from([map2b, map2a]);
119
120 test("RecursiveEquality", () {
121 const unordered = const UnorderedIterableEquality();
122 expect(unordered.equals(map1a["x"], map2a["x"]),
123 isTrue);
124 expect(unordered.equals(map1a["y"], map2a["y"]),
125 isTrue);
126 expect(unordered.equals(map1b["x"], map2b["x"]),
127 isTrue);
128 expect(unordered.equals(map1b["y"], map2b["y"]),
129 isTrue);
130 const mapval = const MapEquality(values: unordered);
131 expect(
132 mapval.equals(map1a, map2a),
133 isTrue);
134 expect(mapval.equals(map1b, map2b),
135 isTrue);
136 const listmapval = const ListEquality(mapval);
137 expect(listmapval.equals(l1, l2),
138 isTrue);
139 const setmapval = const SetEquality(mapval);
140 expect(setmapval.equals(s1, s2),
141 isTrue);
142 });
143
144 test("DeepEquality", () {
145 var colleq = const DeepCollectionEquality.unordered();
146 expect(colleq.equals(map1a["x"], map2a["x"]),
147 isTrue);
148 expect(colleq.equals(map1a["y"], map2a["y"]),
149 isTrue);
150 expect(colleq.equals(map1b["x"], map2b["x"]),
151 isTrue);
152 expect(colleq.equals(map1b["y"], map2b["y"]),
153 isTrue);
154 expect(colleq.equals(map1a, map2a),
155 isTrue);
156 expect(colleq.equals(map1b, map2b),
157 isTrue);
158 expect(colleq.equals(l1, l2),
159 isTrue);
160 expect(colleq.equals(s1, s2),
161 isTrue);
162 });
163 }
164
165 /// Wrapper objects for an `id` value.
166 ///
167 /// Compares the `id` value by equality and for comparison.
168 /// Allows creating simple objects that are equal without being identical.
169 class Element implements Comparable<Element> {
170 final Comparable id;
171 const Element(this.id);
172 int get hashCode => id.hashCode;
173 bool operator==(Object other) => other is Element && id == other.id;
174 int compareTo(other) => id.compareTo(other.id);
175 }
OLDNEW
« no previous file with comments | « packages/collection/test/canonicalized_map_test.dart ('k') | packages/collection/test/iterable_zip_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698