001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.util;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: import java.util.HashSet;
021: import java.util.Arrays;
022: import java.util.List;
023: import java.util.ArrayList;
024: import java.util.EnumSet;
025:
026: /**
027: * Tests EnumSet. TODO(tobyr) Consider using Apache collections tests.
028: */
029: public class EnumSetTest extends GWTTestCase {
030:
031: enum FalseEnum {
032:
033: Zero, One,
034: }
035:
036: // Falls into SimpleEnumSet
037: enum Number {
038:
039: Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen, Twenty, TwentyOne, TwentyTwo, TwentyThree, TwentyFour, TwentyFive, TwentySix, TwentySeven, TwentyEight, TwentyNine, Thirty,
040: }
041:
042: // Requires LargeEnumSet
043: enum Number2 {
044:
045: Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen, Twenty, TwentyOne, TwentyTwo, TwentyThree, TwentyFour, TwentyFive, TwentySix, TwentySeven, TwentyEight, TwentyNine, Thirty, ThirtyOne,
046: }
047:
048: public String getModuleName() {
049: return "com.google.gwt.emultest.EmulSuite";
050: }
051:
052: public void testFastEnums() {
053: enumTest(Number.class);
054: }
055:
056: public void testLargeEnums() {
057: enumTest(Number2.class);
058: }
059:
060: private <E extends Enum<E>> void enumTest(Class<E> e) {
061: E[] enums = e.getEnumConstants();
062:
063: EnumSet<E> numbers = EnumSet.of(enums[1], enums[2]);
064: HashSet<E> numberSet = new HashSet<E>();
065: numberSet.add(enums[1]);
066: numberSet.add(enums[2]);
067: assertEquals(numberSet, numbers);
068:
069: numbers.add(enums[23]);
070: numberSet.add(enums[23]);
071: assertEquals(numberSet, numbers);
072:
073: numbers.remove(enums[1]);
074: numberSet.remove(enums[1]);
075: assertEquals(numberSet, numbers);
076:
077: // Attempt an add at the beginning
078: numbers.add(enums[0]);
079: numberSet.add(enums[0]);
080: assertEquals(numberSet, numbers);
081:
082: // Attempt an add at the end
083: numbers.add(enums[enums.length - 1]);
084: numberSet.add(enums[enums.length - 1]);
085: assertEquals(numberSet, numbers);
086:
087: // Attempt to remove something bogus
088: numbers.remove(enums[15]);
089: numberSet.remove(enums[15]);
090: assertEquals(numberSet, numbers);
091:
092: // Attempt to add a duplicate value
093: numbers.add(enums[23]);
094: numberSet.add(enums[23]);
095: assertEquals(numberSet, numbers);
096:
097: numbers = EnumSet.allOf(e);
098: numberSet.clear();
099: numberSet.addAll(Arrays.asList(enums));
100: assertEquals(numberSet, numbers);
101:
102: numbers = EnumSet.noneOf(e);
103: numberSet.clear();
104: assertEquals(numberSet, numbers);
105:
106: numbers = EnumSet.complementOf(EnumSet.allOf(e));
107: numberSet.clear();
108: assertEquals(numberSet, numbers);
109:
110: numbers = EnumSet.complementOf(EnumSet.noneOf(e));
111: numberSet.clear();
112: numberSet.addAll(Arrays.asList(enums));
113: assertEquals(numberSet, numbers);
114:
115: numbers = EnumSet.allOf(e);
116: numbers.remove(enums[20]);
117: numbers.remove(enums[17]);
118: numbers = EnumSet.complementOf(numbers);
119: numberSet.clear();
120: numberSet.add(enums[20]);
121: numberSet.add(enums[17]);
122: assertEquals(numberSet, numbers);
123:
124: numbers = EnumSet.range(enums[0], enums[enums.length - 1]);
125: numberSet.clear();
126: numberSet.addAll(Arrays.asList(enums));
127: assertEquals(numberSet, numbers);
128:
129: numbers = EnumSet.range(enums[5], enums[5]);
130: numberSet.clear();
131: numberSet.add(enums[5]);
132: assertEquals(numberSet, numbers);
133:
134: numbers = EnumSet.range(enums[5], enums[10]);
135: numberSet.clear();
136: for (int i = 5, end = 10; i <= end; ++i) {
137: numberSet.add(enums[i]);
138: }
139: assertEquals(numberSet, numbers);
140:
141: // Test duplicates
142: List<E> numberList = new ArrayList<E>();
143: for (int i = 5, end = 10; i <= end; ++i) {
144: numberList.add(enums[i]);
145: }
146: for (int i = 5, end = 10; i <= end; ++i) {
147: numberList.add(enums[i]);
148: }
149: numbers = EnumSet.copyOf(numberList);
150: numberSet = new HashSet<E>(numberList);
151: assertEquals(numberSet, numbers);
152:
153: try {
154: numbers.add(null);
155: fail("EnumSet shouldn't allow null.");
156: } catch (NullPointerException ex) {
157: }
158:
159: // Try testing for null
160: assertFalse("EnumSet should allow testing for null", numbers
161: .contains(null));
162:
163: // Try testing for an Enum that looks the same, but has different type
164: numbers.clear();
165: numbers.add(enums[1]);
166: assertFalse("EnumSet should test for correct type.", numbers
167: .contains(FalseEnum.One));
168:
169: // Same type testing, but on remove.
170: numbers.remove(FalseEnum.One);
171: assertTrue("EnumSet should test for correct type on remove",
172: numbers.contains(enums[1]));
173: }
174: }
|