001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.Arrays;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Set;
024: import java.io.ObjectOutputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.Serializable;
028: import java.lang.reflect.Method;
029: import java.lang.reflect.InvocationTargetException;
030:
031: import org.apache.harmony.testframework.serialization.SerializationTest;
032:
033: public class HashSetTest extends junit.framework.TestCase {
034:
035: HashSet hs;
036:
037: static Object[] objArray;
038: {
039: objArray = new Object[1000];
040: for (int i = 0; i < objArray.length; i++)
041: objArray[i] = new Integer(i);
042: }
043:
044: /**
045: * @tests java.util.HashSet#HashSet()
046: */
047: public void test_Constructor() {
048: // Test for method java.util.HashSet()
049: HashSet hs2 = new HashSet();
050: assertEquals("Created incorrect HashSet", 0, hs2.size());
051: }
052:
053: /**
054: * @tests java.util.HashSet#HashSet(int)
055: */
056: public void test_ConstructorI() {
057: // Test for method java.util.HashSet(int)
058: HashSet hs2 = new HashSet(5);
059: assertEquals("Created incorrect HashSet", 0, hs2.size());
060: try {
061: new HashSet(-1);
062: } catch (IllegalArgumentException e) {
063: return;
064: }
065: fail("Failed to throw IllegalArgumentException for capacity < 0");
066: }
067:
068: /**
069: * @tests java.util.HashSet#HashSet(int, float)
070: */
071: public void test_ConstructorIF() {
072: // Test for method java.util.HashSet(int, float)
073: HashSet hs2 = new HashSet(5, (float) 0.5);
074: assertEquals("Created incorrect HashSet", 0, hs2.size());
075: try {
076: new HashSet(0, 0);
077: } catch (IllegalArgumentException e) {
078: return;
079: }
080: fail("Failed to throw IllegalArgumentException for initial load factor <= 0");
081: }
082:
083: /**
084: * @tests java.util.HashSet#HashSet(java.util.Collection)
085: */
086: public void test_ConstructorLjava_util_Collection() {
087: // Test for method java.util.HashSet(java.util.Collection)
088: HashSet hs2 = new HashSet(Arrays.asList(objArray));
089: for (int counter = 0; counter < objArray.length; counter++)
090: assertTrue("HashSet does not contain correct elements", hs
091: .contains(objArray[counter]));
092: assertTrue("HashSet created from collection incorrect size",
093: hs2.size() == objArray.length);
094: }
095:
096: /**
097: * @tests java.util.HashSet#add(java.lang.Object)
098: */
099: public void test_addLjava_lang_Object() {
100: // Test for method boolean java.util.HashSet.add(java.lang.Object)
101: int size = hs.size();
102: hs.add(new Integer(8));
103: assertTrue("Added element already contained by set",
104: hs.size() == size);
105: hs.add(new Integer(-9));
106: assertTrue("Failed to increment set size after add",
107: hs.size() == size + 1);
108: assertTrue("Failed to add element to set", hs
109: .contains(new Integer(-9)));
110: }
111:
112: /**
113: * @tests java.util.HashSet#clear()
114: */
115: public void test_clear() {
116: // Test for method void java.util.HashSet.clear()
117: Set orgSet = (Set) hs.clone();
118: hs.clear();
119: Iterator i = orgSet.iterator();
120: assertEquals("Returned non-zero size after clear", 0, hs.size());
121: while (i.hasNext())
122: assertTrue("Failed to clear set", !hs.contains(i.next()));
123: }
124:
125: /**
126: * @tests java.util.HashSet#clone()
127: */
128: public void test_clone() {
129: // Test for method java.lang.Object java.util.HashSet.clone()
130: HashSet hs2 = (HashSet) hs.clone();
131: assertTrue("clone returned an equivalent HashSet", hs != hs2);
132: assertTrue("clone did not return an equal HashSet", hs
133: .equals(hs2));
134: }
135:
136: /**
137: * @tests java.util.HashSet#contains(java.lang.Object)
138: */
139: public void test_containsLjava_lang_Object() {
140: // Test for method boolean java.util.HashSet.contains(java.lang.Object)
141: assertTrue("Returned false for valid object", hs
142: .contains(objArray[90]));
143: assertTrue("Returned true for invalid Object", !hs
144: .contains(new Object()));
145:
146: HashSet s = new HashSet();
147: s.add(null);
148: assertTrue("Cannot handle null", s.contains(null));
149: }
150:
151: /**
152: * @tests java.util.HashSet#isEmpty()
153: */
154: public void test_isEmpty() {
155: // Test for method boolean java.util.HashSet.isEmpty()
156: assertTrue("Empty set returned false", new HashSet().isEmpty());
157: assertTrue("Non-empty set returned true", !hs.isEmpty());
158: }
159:
160: /**
161: * @tests java.util.HashSet#iterator()
162: */
163: public void test_iterator() {
164: // Test for method java.util.Iterator java.util.HashSet.iterator()
165: Iterator i = hs.iterator();
166: int x = 0;
167: while (i.hasNext()) {
168: assertTrue("Failed to iterate over all elements", hs
169: .contains(i.next()));
170: ++x;
171: }
172: assertTrue("Returned iteration of incorrect size",
173: hs.size() == x);
174:
175: HashSet s = new HashSet();
176: s.add(null);
177: assertNull("Cannot handle null", s.iterator().next());
178: }
179:
180: /**
181: * @tests java.util.HashSet#remove(java.lang.Object)
182: */
183: public void test_removeLjava_lang_Object() {
184: // Test for method boolean java.util.HashSet.remove(java.lang.Object)
185: int size = hs.size();
186: hs.remove(new Integer(98));
187: assertTrue("Failed to remove element", !hs
188: .contains(new Integer(98)));
189: assertTrue("Failed to decrement set size",
190: hs.size() == size - 1);
191:
192: HashSet s = new HashSet();
193: s.add(null);
194: assertTrue("Cannot handle null", s.remove(null));
195: }
196:
197: /**
198: * @tests java.util.HashSet#size()
199: */
200: public void test_size() {
201: // Test for method int java.util.HashSet.size()
202: assertTrue("Returned incorrect size",
203: hs.size() == (objArray.length + 1));
204: hs.clear();
205: assertEquals("Cleared set returned non-zero size", 0, hs.size());
206: }
207:
208: /**
209: * @tests java.util.HashSet#SerializationTest
210: */
211: public void test_Serialization() throws Exception {
212: HashSet<String> hs = new HashSet<String>();
213: hs.add("hello");
214: hs.add("world");
215: SerializationTest.verifySelf(hs, comparator);
216: SerializationTest.verifyGolden(this , hs, comparator);
217: }
218:
219: /**
220: * Sets up the fixture, for example, open a network connection. This method
221: * is called before a test is executed.
222: */
223: protected void setUp() {
224: hs = new HashSet();
225: for (int i = 0; i < objArray.length; i++)
226: hs.add(objArray[i]);
227: hs.add(null);
228: }
229:
230: /**
231: * Tears down the fixture, for example, close a network connection. This
232: * method is called after a test is executed.
233: */
234: protected void tearDown() {
235: }
236:
237: private static final SerializationTest.SerializableAssert comparator = new SerializationTest.SerializableAssert() {
238: public void assertDeserialized(Serializable initial,
239: Serializable deserialized) {
240: HashSet<String> initialHs = (HashSet<String>) initial;
241: HashSet<String> deseriaHs = (HashSet<String>) deserialized;
242: assertEquals("should be equal", initialHs.size(), deseriaHs
243: .size());
244: assertEquals("should be equal", initialHs, deseriaHs);
245: }
246:
247: };
248: }
|