HashSetTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.util;
19
20import dalvik.annotation.TestTarget;
21import dalvik.annotation.TestInfo;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.util.Arrays;
26import java.util.HashSet;
27import java.util.Iterator;
28import java.util.Set;
29import java.io.ObjectOutputStream;
30import java.io.ByteArrayOutputStream;
31import java.io.IOException;
32import java.io.Serializable;
33import java.lang.reflect.Method;
34import java.lang.reflect.InvocationTargetException;
35
36import org.apache.harmony.testframework.serialization.SerializationTest;
37
38@TestTargetClass(HashSet.class)
39public class HashSetTest extends junit.framework.TestCase {
40
41    HashSet hs;
42
43    static Object[] objArray;
44    {
45        objArray = new Object[1000];
46        for (int i = 0; i < objArray.length; i++)
47            objArray[i] = new Integer(i);
48    }
49
50    /**
51     * @tests java.util.HashSet#HashSet()
52     */
53    @TestInfo(
54      level = TestLevel.COMPLETE,
55      purpose = "",
56      targets = {
57        @TestTarget(
58          methodName = "HashSet",
59          methodArgs = {}
60        )
61    })
62    public void test_Constructor() {
63        // Test for method java.util.HashSet()
64        HashSet hs2 = new HashSet();
65        assertEquals("Created incorrect HashSet", 0, hs2.size());
66    }
67
68    /**
69     * @tests java.util.HashSet#HashSet(int)
70     */
71    @TestInfo(
72      level = TestLevel.COMPLETE,
73      purpose = "",
74      targets = {
75        @TestTarget(
76          methodName = "HashSet",
77          methodArgs = {int.class}
78        )
79    })
80    public void test_ConstructorI() {
81        // Test for method java.util.HashSet(int)
82        HashSet hs2 = new HashSet(5);
83        assertEquals("Created incorrect HashSet", 0, hs2.size());
84        try {
85            new HashSet(-1);
86        } catch (IllegalArgumentException e) {
87            return;
88        }
89        fail(
90                "Failed to throw IllegalArgumentException for capacity < 0");
91    }
92
93    /**
94     * @tests java.util.HashSet#HashSet(int, float)
95     */
96    @TestInfo(
97      level = TestLevel.COMPLETE,
98      purpose = "",
99      targets = {
100        @TestTarget(
101          methodName = "HashSet",
102          methodArgs = {int.class, float.class}
103        )
104    })
105    public void test_ConstructorIF() {
106        // Test for method java.util.HashSet(int, float)
107        HashSet hs2 = new HashSet(5, (float) 0.5);
108        assertEquals("Created incorrect HashSet", 0, hs2.size());
109        try {
110            new HashSet(0, 0);
111        } catch (IllegalArgumentException e) {
112            return;
113        }
114        fail(
115                "Failed to throw IllegalArgumentException for initial load factor <= 0");
116    }
117
118    /**
119     * @tests java.util.HashSet#HashSet(java.util.Collection)
120     */
121    @TestInfo(
122      level = TestLevel.PARTIAL,
123      purpose = "Doesn't verify NullPointerException.",
124      targets = {
125        @TestTarget(
126          methodName = "HashSet",
127          methodArgs = {java.util.Collection.class}
128        )
129    })
130    public void test_ConstructorLjava_util_Collection() {
131        // Test for method java.util.HashSet(java.util.Collection)
132        HashSet hs2 = new HashSet(Arrays.asList(objArray));
133        for (int counter = 0; counter < objArray.length; counter++)
134            assertTrue("HashSet does not contain correct elements", hs
135                    .contains(objArray[counter]));
136        assertTrue("HashSet created from collection incorrect size",
137                hs2.size() == objArray.length);
138    }
139
140    /**
141     * @tests java.util.HashSet#add(java.lang.Object)
142     */
143    @TestInfo(
144      level = TestLevel.COMPLETE,
145      purpose = "",
146      targets = {
147        @TestTarget(
148          methodName = "add",
149          methodArgs = {Object.class}
150        )
151    })
152    public void test_addLjava_lang_Object() {
153        // Test for method boolean java.util.HashSet.add(java.lang.Object)
154        int size = hs.size();
155        hs.add(new Integer(8));
156        assertTrue("Added element already contained by set", hs.size() == size);
157        hs.add(new Integer(-9));
158        assertTrue("Failed to increment set size after add",
159                hs.size() == size + 1);
160        assertTrue("Failed to add element to set", hs.contains(new Integer(-9)));
161    }
162
163    /**
164     * @tests java.util.HashSet#clear()
165     */
166    @TestInfo(
167      level = TestLevel.COMPLETE,
168      purpose = "",
169      targets = {
170        @TestTarget(
171          methodName = "clear",
172          methodArgs = {}
173        )
174    })
175    public void test_clear() {
176        // Test for method void java.util.HashSet.clear()
177        Set orgSet = (Set) hs.clone();
178        hs.clear();
179        Iterator i = orgSet.iterator();
180        assertEquals("Returned non-zero size after clear", 0, hs.size());
181        while (i.hasNext())
182            assertTrue("Failed to clear set", !hs.contains(i.next()));
183    }
184
185    /**
186     * @tests java.util.HashSet#clone()
187     */
188    @TestInfo(
189      level = TestLevel.COMPLETE,
190      purpose = "",
191      targets = {
192        @TestTarget(
193          methodName = "clone",
194          methodArgs = {}
195        )
196    })
197    public void test_clone() {
198        // Test for method java.lang.Object java.util.HashSet.clone()
199        HashSet hs2 = (HashSet) hs.clone();
200        assertTrue("clone returned an equivalent HashSet", hs != hs2);
201        assertTrue("clone did not return an equal HashSet", hs.equals(hs2));
202    }
203
204    /**
205     * @tests java.util.HashSet#contains(java.lang.Object)
206     */
207    @TestInfo(
208      level = TestLevel.COMPLETE,
209      purpose = "",
210      targets = {
211        @TestTarget(
212          methodName = "contains",
213          methodArgs = {java.lang.Object.class}
214        )
215    })
216    public void test_containsLjava_lang_Object() {
217        // Test for method boolean java.util.HashSet.contains(java.lang.Object)
218        assertTrue("Returned false for valid object", hs.contains(objArray[90]));
219        assertTrue("Returned true for invalid Object", !hs
220                .contains(new Object()));
221
222        HashSet s = new HashSet();
223        s.add(null);
224        assertTrue("Cannot handle null", s.contains(null));
225    }
226
227    /**
228     * @tests java.util.HashSet#isEmpty()
229     */
230    @TestInfo(
231      level = TestLevel.COMPLETE,
232      purpose = "",
233      targets = {
234        @TestTarget(
235          methodName = "isEmpty",
236          methodArgs = {}
237        )
238    })
239    public void test_isEmpty() {
240        // Test for method boolean java.util.HashSet.isEmpty()
241        assertTrue("Empty set returned false", new HashSet().isEmpty());
242        assertTrue("Non-empty set returned true", !hs.isEmpty());
243    }
244
245    /**
246     * @tests java.util.HashSet#iterator()
247     */
248    @TestInfo(
249      level = TestLevel.COMPLETE,
250      purpose = "",
251      targets = {
252        @TestTarget(
253          methodName = "iterator",
254          methodArgs = {}
255        )
256    })
257    public void test_iterator() {
258        // Test for method java.util.Iterator java.util.HashSet.iterator()
259        Iterator i = hs.iterator();
260        int x = 0;
261        while (i.hasNext()) {
262            assertTrue("Failed to iterate over all elements", hs.contains(i
263                    .next()));
264            ++x;
265        }
266        assertTrue("Returned iteration of incorrect size", hs.size() == x);
267
268        HashSet s = new HashSet();
269        s.add(null);
270        assertNull("Cannot handle null", s.iterator().next());
271    }
272
273    /**
274     * @tests java.util.HashSet#remove(java.lang.Object)
275     */
276    @TestInfo(
277      level = TestLevel.PARTIAL,
278      purpose = "Doesn't verify remove method for not present element in " +
279            "HashSet.",
280      targets = {
281        @TestTarget(
282          methodName = "remove",
283          methodArgs = {java.lang.Object.class}
284        )
285    })
286    public void test_removeLjava_lang_Object() {
287        // Test for method boolean java.util.HashSet.remove(java.lang.Object)
288        int size = hs.size();
289        hs.remove(new Integer(98));
290        assertTrue("Failed to remove element", !hs.contains(new Integer(98)));
291        assertTrue("Failed to decrement set size", hs.size() == size - 1);
292
293        HashSet s = new HashSet();
294        s.add(null);
295        assertTrue("Cannot handle null", s.remove(null));
296    }
297
298    /**
299     * @tests java.util.HashSet#size()
300     */
301    @TestInfo(
302      level = TestLevel.COMPLETE,
303      purpose = "",
304      targets = {
305        @TestTarget(
306          methodName = "size",
307          methodArgs = {}
308        )
309    })
310    public void test_size() {
311        // Test for method int java.util.HashSet.size()
312        assertTrue("Returned incorrect size", hs.size() == (objArray.length + 1));
313        hs.clear();
314        assertEquals("Cleared set returned non-zero size", 0, hs.size());
315    }
316
317    /**
318     * @tests java.util.HashSet#SerializationTest
319     */
320    @TestInfo(
321      level = TestLevel.COMPLETE,
322      purpose = "Verifies serialization/deserialization compatibility.",
323      targets = {
324        @TestTarget(
325          methodName = "!SerializationSelf",
326          methodArgs = {}
327        ),
328        @TestTarget(
329          methodName = "!SerializationGolden",
330          methodArgs = {}
331        )
332    })
333    public void test_Serialization() throws Exception{
334        HashSet<String> hs = new HashSet<String>();
335        hs.add("hello");
336        hs.add("world");
337        SerializationTest.verifySelf(hs, comparator);
338        SerializationTest.verifyGolden(this, hs, comparator);
339    }
340
341    /**
342     * Sets up the fixture, for example, open a network connection. This method
343     * is called before a test is executed.
344     */
345    protected void setUp() {
346        hs = new HashSet();
347        for (int i = 0; i < objArray.length; i++)
348            hs.add(objArray[i]);
349        hs.add(null);
350    }
351
352    /**
353     * Tears down the fixture, for example, close a network connection. This
354     * method is called after a test is executed.
355     */
356    protected void tearDown() {
357    }
358
359    private static final SerializationTest.SerializableAssert comparator = new
360                                       SerializationTest.SerializableAssert() {
361        public void assertDeserialized(Serializable initial, Serializable deserialized) {
362            HashSet<String> initialHs = (HashSet<String>) initial;
363            HashSet<String> deseriaHs = (HashSet<String>) deserialized;
364            assertEquals("should be equal", initialHs.size(), deseriaHs.size());
365            assertEquals("should be equal", initialHs, deseriaHs);
366        }
367
368    };
369}
370