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

Side by Side Diff: tests/compiler/dart2js/enumset_test.dart

Issue 1568213003: Add EnumSet. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Updated cf. comments. Created 4 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
« no previous file with comments | « tests/compiler/dart2js/class_set_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 enumset.test;
6
7 import 'package:compiler/src/util/enumset.dart';
8 import 'package:expect/expect.dart';
9
10 enum Enum { A, B, C, D, E, F, }
11
12 main() {
13 testAddRemoveContains();
14 testConstructorsIntersects();
15 }
16
17
18 void checkEnumSet(EnumSet<Enum> enumSet,
19 int expectedValue,
20 List<Enum> expectedValues,
21 String expectedToString) {
22 Expect.equals(expectedValue, enumSet.value,
23 "Unexpected EnumSet.value for ${enumSet.iterable(Enum.values)}");
24 Expect.listEquals(expectedValues, enumSet.iterable(Enum.values).toList(),
25 "Unexpected values: ${enumSet.iterable(Enum.values)}");
26 Expect.equals(expectedValues.isEmpty, enumSet.isEmpty,
27 "Unexpected EnumSet.isEmpty for ${enumSet.iterable(Enum.values)}");
28 Expect.equals(expectedToString, enumSet.toString(),
29 "Unexpected EnumSet.toString for ${enumSet.iterable(Enum.values)}");
30 for (Enum value in Enum.values) {
31 Expect.equals(expectedValues.contains(value), enumSet.contains(value),
32 "Unexpected EnumSet.contains for $value in "
33 "${enumSet.iterable(Enum.values)}");
34 }
35 }
36
37 void testAddRemoveContains() {
38 EnumSet<Enum> enumSet = new EnumSet<Enum>();
39
40 void check(int expectedValue,
41 List<Enum> expectedValues,
42 String expectedToString) {
43 checkEnumSet(enumSet, expectedValue, expectedValues, expectedToString);
44 }
45
46 check(0, [], '0');
47
48 enumSet.add(Enum.B);
49 check(2, [Enum.B], '10');
50
51 enumSet.add(Enum.F);
52 check(34, [Enum.F, Enum.B], '100010');
53
54 enumSet.add(Enum.A);
55 check(35, [Enum.F, Enum.B, Enum.A], '100011');
56
57 enumSet.add(Enum.A);
58 check(35, [Enum.F, Enum.B, Enum.A], '100011');
59
60 enumSet.remove(Enum.C);
61 check(35, [Enum.F, Enum.B, Enum.A], '100011');
62
63 enumSet.remove(Enum.B);
64 check(33, [Enum.F, Enum.A], '100001');
65
66 enumSet.remove(Enum.A);
67 check(32, [Enum.F], '100000');
68
69 enumSet.clear();
70 check(0, [], '0');
71
72 enumSet.add(Enum.A);
73 enumSet.add(Enum.B);
74 enumSet.add(Enum.C);
75 enumSet.add(Enum.D);
76 enumSet.add(Enum.E);
77 enumSet.add(Enum.F);
78 check(63, [Enum.F, Enum.E, Enum.D, Enum.C, Enum.B, Enum.A], '111111');
79 }
80
81 void testConstructorsIntersects() {
82 EnumSet<Enum> emptyA = new EnumSet<Enum>();
83 EnumSet<Enum> emptyB = new EnumSet<Enum>.fromValue(0);
84 EnumSet<Enum> emptyC = const EnumSet<Enum>.fixed(0);
85 EnumSet<Enum> emptyD = new EnumSet<Enum>.fixed(0);
86
87
88 void checkIntersects(EnumSet<Enum> a, EnumSet<Enum> b, bool expectedValue) {
89 Expect.equals(expectedValue, a.intersects(b),
90 "Unexpected intersects of $a and $b");
91 Expect.equals(a.intersects(b), b.intersects(a),
92 "Unsymmetric intersects of $a and $b");
93 }
94
95 void check(EnumSet<Enum> a, EnumSet<Enum> b) {
96 Expect.equals(a.value, b.value,
97 "Unexpected values of $a and $b");
98 Expect.equals(a.hashCode, b.hashCode,
99 "Unexpected hash codes of $a and $b");
100 Expect.equals(a, b,
101 "Unexpected equality of $a and $b");
102 checkIntersects(a, b, !a.isEmpty);
103 }
104
105 check(emptyA, emptyA);
106 check(emptyA, emptyB);
107 check(emptyA, emptyC);
108 check(emptyA, emptyD);
109
110 EnumSet<Enum> singleA = new EnumSet<Enum>()..add(Enum.C);
111 EnumSet<Enum> singleB = new EnumSet<Enum>.fromValue(4);
112 EnumSet<Enum> singleC = const EnumSet<Enum>.fixed(4);
113 EnumSet<Enum> singleD = new EnumSet<Enum>.fixed(4);
114 EnumSet<Enum> singleE = new EnumSet<Enum>.fromValues([Enum.C]);
115 EnumSet<Enum> singleF = new EnumSet<Enum>.fromValues([Enum.C], fixed: true);
116
117 check(singleA, singleA);
118 check(singleA, singleB);
119 check(singleA, singleC);
120 check(singleA, singleD);
121 check(singleA, singleE);
122 check(singleA, singleF);
123
124 EnumSet<Enum> multiA = new EnumSet<Enum>()
125 ..add(Enum.A)..add(Enum.D)..add(Enum.F);
126 EnumSet<Enum> multiB = new EnumSet<Enum>.fromValue(41);
127 EnumSet<Enum> multiC = const EnumSet<Enum>.fixed(41);
128 EnumSet<Enum> multiD = new EnumSet<Enum>.fixed(41);
129 EnumSet<Enum> multiE = new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D]);
130 EnumSet<Enum> multiF =
131 new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D], fixed: true);
132
133 check(multiA, multiA);
134 check(multiA, multiB);
135 check(multiA, multiC);
136 check(multiA, multiD);
137 check(multiA, multiE);
138 check(multiA, multiF);
139
140 EnumSet<Enum> multi2 = new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.C]);
141
142 checkIntersects(emptyA, singleA, false);
143 checkIntersects(emptyA, multiA, false);
144 checkIntersects(emptyA, multi2, false);
145
146 checkIntersects(singleA, multiA, false);
147 checkIntersects(singleA, multi2, true);
148
149 checkIntersects(multiA, multi2, true);
150 }
OLDNEW
« no previous file with comments | « tests/compiler/dart2js/class_set_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698