545ee13cd4
Change-Id: I193e19581d29a9c4b343ae0a681d1ff530cfce1d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281309 Commit-Queue: Nate Biggs <natebiggs@google.com> Reviewed-by: Mayank Patke <fishythefish@google.com>
154 lines
4.6 KiB
Dart
154 lines
4.6 KiB
Dart
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
library enumset.test;
|
|
|
|
import 'package:compiler/src/util/enumset.dart';
|
|
import 'package:expect/expect.dart';
|
|
|
|
enum Enum {
|
|
A,
|
|
B,
|
|
C,
|
|
D,
|
|
E,
|
|
F,
|
|
}
|
|
|
|
main() {
|
|
testAddRemoveContains();
|
|
testConstructorsIntersects();
|
|
}
|
|
|
|
void checkEnumSet(EnumSet<Enum> enumSet, int expectedValue,
|
|
List<Enum> expectedValues, String expectedToString) {
|
|
Expect.equals(expectedValue, enumSet.value,
|
|
"Unexpected EnumSet.value for ${enumSet.iterable(Enum.values)}");
|
|
Expect.listEquals(expectedValues, enumSet.iterable(Enum.values).toList(),
|
|
"Unexpected values: ${enumSet.iterable(Enum.values)}");
|
|
Expect.equals(expectedValues.isEmpty, enumSet.isEmpty,
|
|
"Unexpected EnumSet.isEmpty for ${enumSet.iterable(Enum.values)}");
|
|
Expect.equals(expectedToString, enumSet.toString(),
|
|
"Unexpected EnumSet.toString for ${enumSet.iterable(Enum.values)}");
|
|
for (Enum value in Enum.values) {
|
|
Expect.equals(
|
|
expectedValues.contains(value),
|
|
enumSet.contains(value),
|
|
"Unexpected EnumSet.contains for $value in "
|
|
"${enumSet.iterable(Enum.values)}");
|
|
}
|
|
}
|
|
|
|
void testAddRemoveContains() {
|
|
EnumSet<Enum> enumSet = EnumSet<Enum>();
|
|
|
|
void check(
|
|
int expectedValue, List<Enum> expectedValues, String expectedToString) {
|
|
checkEnumSet(enumSet, expectedValue, expectedValues, expectedToString);
|
|
}
|
|
|
|
check(0, [], '0');
|
|
|
|
enumSet.add(Enum.B);
|
|
check(2, [Enum.B], '10');
|
|
|
|
enumSet.add(Enum.F);
|
|
check(34, [Enum.F, Enum.B], '100010');
|
|
|
|
enumSet.add(Enum.A);
|
|
check(35, [Enum.F, Enum.B, Enum.A], '100011');
|
|
|
|
enumSet.add(Enum.A);
|
|
check(35, [Enum.F, Enum.B, Enum.A], '100011');
|
|
|
|
enumSet.remove(Enum.C);
|
|
check(35, [Enum.F, Enum.B, Enum.A], '100011');
|
|
|
|
enumSet.remove(Enum.B);
|
|
check(33, [Enum.F, Enum.A], '100001');
|
|
|
|
enumSet.remove(Enum.A);
|
|
check(32, [Enum.F], '100000');
|
|
|
|
enumSet.clear();
|
|
check(0, [], '0');
|
|
|
|
enumSet.add(Enum.A);
|
|
enumSet.add(Enum.B);
|
|
enumSet.add(Enum.C);
|
|
enumSet.add(Enum.D);
|
|
enumSet.add(Enum.E);
|
|
enumSet.add(Enum.F);
|
|
check(63, [Enum.F, Enum.E, Enum.D, Enum.C, Enum.B, Enum.A], '111111');
|
|
}
|
|
|
|
void testConstructorsIntersects() {
|
|
EnumSet<Enum> emptyA = EnumSet<Enum>();
|
|
EnumSet<Enum> emptyB = EnumSet<Enum>.fromValue(0);
|
|
EnumSet<Enum> emptyC = const EnumSet<Enum>.fixed(0);
|
|
EnumSet<Enum> emptyD = EnumSet<Enum>.fixed(0);
|
|
|
|
void checkIntersects(EnumSet<Enum> a, EnumSet<Enum> b, bool expectedValue) {
|
|
Expect.equals(
|
|
expectedValue, a.intersects(b), "Unexpected intersects of $a and $b");
|
|
Expect.equals(a.intersects(b), b.intersects(a),
|
|
"Unsymmetric intersects of $a and $b");
|
|
}
|
|
|
|
void check(EnumSet<Enum> a, EnumSet<Enum> b) {
|
|
Expect.equals(a.value, b.value, "Unexpected values of $a and $b");
|
|
Expect.equals(a.hashCode, b.hashCode, "Unexpected hash codes of $a and $b");
|
|
Expect.equals(a, b, "Unexpected equality of $a and $b");
|
|
checkIntersects(a, b, !a.isEmpty);
|
|
}
|
|
|
|
check(emptyA, emptyA);
|
|
check(emptyA, emptyB);
|
|
check(emptyA, emptyC);
|
|
check(emptyA, emptyD);
|
|
|
|
EnumSet<Enum> singleA = EnumSet<Enum>()..add(Enum.C);
|
|
EnumSet<Enum> singleB = EnumSet<Enum>.fromValue(4);
|
|
EnumSet<Enum> singleC = const EnumSet<Enum>.fixed(4);
|
|
EnumSet<Enum> singleD = EnumSet<Enum>.fixed(4);
|
|
EnumSet<Enum> singleE = EnumSet<Enum>.fromValues([Enum.C]);
|
|
EnumSet<Enum> singleF = EnumSet<Enum>.fromValues([Enum.C], fixed: true);
|
|
|
|
check(singleA, singleA);
|
|
check(singleA, singleB);
|
|
check(singleA, singleC);
|
|
check(singleA, singleD);
|
|
check(singleA, singleE);
|
|
check(singleA, singleF);
|
|
|
|
EnumSet<Enum> multiA = EnumSet<Enum>()
|
|
..add(Enum.A)
|
|
..add(Enum.D)
|
|
..add(Enum.F);
|
|
EnumSet<Enum> multiB = EnumSet<Enum>.fromValue(41);
|
|
EnumSet<Enum> multiC = const EnumSet<Enum>.fixed(41);
|
|
EnumSet<Enum> multiD = EnumSet<Enum>.fixed(41);
|
|
EnumSet<Enum> multiE = EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D]);
|
|
EnumSet<Enum> multiF =
|
|
EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D], fixed: true);
|
|
|
|
check(multiA, multiA);
|
|
check(multiA, multiB);
|
|
check(multiA, multiC);
|
|
check(multiA, multiD);
|
|
check(multiA, multiE);
|
|
check(multiA, multiF);
|
|
|
|
EnumSet<Enum> multi2 = EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.C]);
|
|
|
|
checkIntersects(emptyA, singleA, false);
|
|
checkIntersects(emptyA, multiA, false);
|
|
checkIntersects(emptyA, multi2, false);
|
|
|
|
checkIntersects(singleA, multiA, false);
|
|
checkIntersects(singleA, multi2, true);
|
|
|
|
checkIntersects(multiA, multi2, true);
|
|
}
|