| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 enumset.test; | 5 library enumset.test; |
| 6 | 6 |
| 7 import 'package:compiler/src/util/enumset.dart'; | 7 import 'package:compiler/src/util/enumset.dart'; |
| 8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
| 9 | 9 |
| 10 enum Enum { A, B, C, D, E, F, } | 10 enum Enum { A, B, C, D, E, F, } |
| 11 | 11 |
| 12 main() { | 12 main() { |
| 13 testAddRemoveContains(); | 13 testAddRemoveContains(); |
| 14 testConstructorsIntersects(); | 14 testConstructorsIntersects(); |
| 15 } | 15 } |
| 16 | 16 |
| 17 | 17 void checkEnumSet(EnumSet<Enum> enumSet, int expectedValue, |
| 18 void checkEnumSet(EnumSet<Enum> enumSet, | 18 List<Enum> expectedValues, String expectedToString) { |
| 19 int expectedValue, | |
| 20 List<Enum> expectedValues, | |
| 21 String expectedToString) { | |
| 22 Expect.equals(expectedValue, enumSet.value, | 19 Expect.equals(expectedValue, enumSet.value, |
| 23 "Unexpected EnumSet.value for ${enumSet.iterable(Enum.values)}"); | 20 "Unexpected EnumSet.value for ${enumSet.iterable(Enum.values)}"); |
| 24 Expect.listEquals(expectedValues, enumSet.iterable(Enum.values).toList(), | 21 Expect.listEquals(expectedValues, enumSet.iterable(Enum.values).toList(), |
| 25 "Unexpected values: ${enumSet.iterable(Enum.values)}"); | 22 "Unexpected values: ${enumSet.iterable(Enum.values)}"); |
| 26 Expect.equals(expectedValues.isEmpty, enumSet.isEmpty, | 23 Expect.equals(expectedValues.isEmpty, enumSet.isEmpty, |
| 27 "Unexpected EnumSet.isEmpty for ${enumSet.iterable(Enum.values)}"); | 24 "Unexpected EnumSet.isEmpty for ${enumSet.iterable(Enum.values)}"); |
| 28 Expect.equals(expectedToString, enumSet.toString(), | 25 Expect.equals(expectedToString, enumSet.toString(), |
| 29 "Unexpected EnumSet.toString for ${enumSet.iterable(Enum.values)}"); | 26 "Unexpected EnumSet.toString for ${enumSet.iterable(Enum.values)}"); |
| 30 for (Enum value in Enum.values) { | 27 for (Enum value in Enum.values) { |
| 31 Expect.equals(expectedValues.contains(value), enumSet.contains(value), | 28 Expect.equals( |
| 29 expectedValues.contains(value), |
| 30 enumSet.contains(value), |
| 32 "Unexpected EnumSet.contains for $value in " | 31 "Unexpected EnumSet.contains for $value in " |
| 33 "${enumSet.iterable(Enum.values)}"); | 32 "${enumSet.iterable(Enum.values)}"); |
| 34 } | 33 } |
| 35 } | 34 } |
| 36 | 35 |
| 37 void testAddRemoveContains() { | 36 void testAddRemoveContains() { |
| 38 EnumSet<Enum> enumSet = new EnumSet<Enum>(); | 37 EnumSet<Enum> enumSet = new EnumSet<Enum>(); |
| 39 | 38 |
| 40 void check(int expectedValue, | 39 void check( |
| 41 List<Enum> expectedValues, | 40 int expectedValue, List<Enum> expectedValues, String expectedToString) { |
| 42 String expectedToString) { | |
| 43 checkEnumSet(enumSet, expectedValue, expectedValues, expectedToString); | 41 checkEnumSet(enumSet, expectedValue, expectedValues, expectedToString); |
| 44 } | 42 } |
| 45 | 43 |
| 46 check(0, [], '0'); | 44 check(0, [], '0'); |
| 47 | 45 |
| 48 enumSet.add(Enum.B); | 46 enumSet.add(Enum.B); |
| 49 check(2, [Enum.B], '10'); | 47 check(2, [Enum.B], '10'); |
| 50 | 48 |
| 51 enumSet.add(Enum.F); | 49 enumSet.add(Enum.F); |
| 52 check(34, [Enum.F, Enum.B], '100010'); | 50 check(34, [Enum.F, Enum.B], '100010'); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 77 enumSet.add(Enum.F); | 75 enumSet.add(Enum.F); |
| 78 check(63, [Enum.F, Enum.E, Enum.D, Enum.C, Enum.B, Enum.A], '111111'); | 76 check(63, [Enum.F, Enum.E, Enum.D, Enum.C, Enum.B, Enum.A], '111111'); |
| 79 } | 77 } |
| 80 | 78 |
| 81 void testConstructorsIntersects() { | 79 void testConstructorsIntersects() { |
| 82 EnumSet<Enum> emptyA = new EnumSet<Enum>(); | 80 EnumSet<Enum> emptyA = new EnumSet<Enum>(); |
| 83 EnumSet<Enum> emptyB = new EnumSet<Enum>.fromValue(0); | 81 EnumSet<Enum> emptyB = new EnumSet<Enum>.fromValue(0); |
| 84 EnumSet<Enum> emptyC = const EnumSet<Enum>.fixed(0); | 82 EnumSet<Enum> emptyC = const EnumSet<Enum>.fixed(0); |
| 85 EnumSet<Enum> emptyD = new EnumSet<Enum>.fixed(0); | 83 EnumSet<Enum> emptyD = new EnumSet<Enum>.fixed(0); |
| 86 | 84 |
| 87 | |
| 88 void checkIntersects(EnumSet<Enum> a, EnumSet<Enum> b, bool expectedValue) { | 85 void checkIntersects(EnumSet<Enum> a, EnumSet<Enum> b, bool expectedValue) { |
| 89 Expect.equals(expectedValue, a.intersects(b), | 86 Expect.equals( |
| 90 "Unexpected intersects of $a and $b"); | 87 expectedValue, a.intersects(b), "Unexpected intersects of $a and $b"); |
| 91 Expect.equals(a.intersects(b), b.intersects(a), | 88 Expect.equals(a.intersects(b), b.intersects(a), |
| 92 "Unsymmetric intersects of $a and $b"); | 89 "Unsymmetric intersects of $a and $b"); |
| 93 } | 90 } |
| 94 | 91 |
| 95 void check(EnumSet<Enum> a, EnumSet<Enum> b) { | 92 void check(EnumSet<Enum> a, EnumSet<Enum> b) { |
| 96 Expect.equals(a.value, b.value, | 93 Expect.equals(a.value, b.value, "Unexpected values of $a and $b"); |
| 97 "Unexpected values of $a and $b"); | 94 Expect.equals(a.hashCode, b.hashCode, "Unexpected hash codes of $a and $b"); |
| 98 Expect.equals(a.hashCode, b.hashCode, | 95 Expect.equals(a, b, "Unexpected equality of $a and $b"); |
| 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); | 96 checkIntersects(a, b, !a.isEmpty); |
| 103 } | 97 } |
| 104 | 98 |
| 105 check(emptyA, emptyA); | 99 check(emptyA, emptyA); |
| 106 check(emptyA, emptyB); | 100 check(emptyA, emptyB); |
| 107 check(emptyA, emptyC); | 101 check(emptyA, emptyC); |
| 108 check(emptyA, emptyD); | 102 check(emptyA, emptyD); |
| 109 | 103 |
| 110 EnumSet<Enum> singleA = new EnumSet<Enum>()..add(Enum.C); | 104 EnumSet<Enum> singleA = new EnumSet<Enum>()..add(Enum.C); |
| 111 EnumSet<Enum> singleB = new EnumSet<Enum>.fromValue(4); | 105 EnumSet<Enum> singleB = new EnumSet<Enum>.fromValue(4); |
| 112 EnumSet<Enum> singleC = const EnumSet<Enum>.fixed(4); | 106 EnumSet<Enum> singleC = const EnumSet<Enum>.fixed(4); |
| 113 EnumSet<Enum> singleD = new EnumSet<Enum>.fixed(4); | 107 EnumSet<Enum> singleD = new EnumSet<Enum>.fixed(4); |
| 114 EnumSet<Enum> singleE = new EnumSet<Enum>.fromValues([Enum.C]); | 108 EnumSet<Enum> singleE = new EnumSet<Enum>.fromValues([Enum.C]); |
| 115 EnumSet<Enum> singleF = new EnumSet<Enum>.fromValues([Enum.C], fixed: true); | 109 EnumSet<Enum> singleF = new EnumSet<Enum>.fromValues([Enum.C], fixed: true); |
| 116 | 110 |
| 117 check(singleA, singleA); | 111 check(singleA, singleA); |
| 118 check(singleA, singleB); | 112 check(singleA, singleB); |
| 119 check(singleA, singleC); | 113 check(singleA, singleC); |
| 120 check(singleA, singleD); | 114 check(singleA, singleD); |
| 121 check(singleA, singleE); | 115 check(singleA, singleE); |
| 122 check(singleA, singleF); | 116 check(singleA, singleF); |
| 123 | 117 |
| 124 EnumSet<Enum> multiA = new EnumSet<Enum>() | 118 EnumSet<Enum> multiA = new EnumSet<Enum>() |
| 125 ..add(Enum.A)..add(Enum.D)..add(Enum.F); | 119 ..add(Enum.A) |
| 120 ..add(Enum.D) |
| 121 ..add(Enum.F); |
| 126 EnumSet<Enum> multiB = new EnumSet<Enum>.fromValue(41); | 122 EnumSet<Enum> multiB = new EnumSet<Enum>.fromValue(41); |
| 127 EnumSet<Enum> multiC = const EnumSet<Enum>.fixed(41); | 123 EnumSet<Enum> multiC = const EnumSet<Enum>.fixed(41); |
| 128 EnumSet<Enum> multiD = new EnumSet<Enum>.fixed(41); | 124 EnumSet<Enum> multiD = new EnumSet<Enum>.fixed(41); |
| 129 EnumSet<Enum> multiE = new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D]); | 125 EnumSet<Enum> multiE = new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D]); |
| 130 EnumSet<Enum> multiF = | 126 EnumSet<Enum> multiF = |
| 131 new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D], fixed: true); | 127 new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.D], fixed: true); |
| 132 | 128 |
| 133 check(multiA, multiA); | 129 check(multiA, multiA); |
| 134 check(multiA, multiB); | 130 check(multiA, multiB); |
| 135 check(multiA, multiC); | 131 check(multiA, multiC); |
| 136 check(multiA, multiD); | 132 check(multiA, multiD); |
| 137 check(multiA, multiE); | 133 check(multiA, multiE); |
| 138 check(multiA, multiF); | 134 check(multiA, multiF); |
| 139 | 135 |
| 140 EnumSet<Enum> multi2 = new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.C]); | 136 EnumSet<Enum> multi2 = new EnumSet<Enum>.fromValues([Enum.F, Enum.A, Enum.C]); |
| 141 | 137 |
| 142 checkIntersects(emptyA, singleA, false); | 138 checkIntersects(emptyA, singleA, false); |
| 143 checkIntersects(emptyA, multiA, false); | 139 checkIntersects(emptyA, multiA, false); |
| 144 checkIntersects(emptyA, multi2, false); | 140 checkIntersects(emptyA, multi2, false); |
| 145 | 141 |
| 146 checkIntersects(singleA, multiA, false); | 142 checkIntersects(singleA, multiA, false); |
| 147 checkIntersects(singleA, multi2, true); | 143 checkIntersects(singleA, multi2, true); |
| 148 | 144 |
| 149 checkIntersects(multiA, multi2, true); | 145 checkIntersects(multiA, multi2, true); |
| 150 } | 146 } |
| OLD | NEW |