1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.util;
18
19import java.util.Arrays;
20import java.util.Iterator;
21import java.util.LinkedHashSet;
22import java.util.Set;
23
24/**
25 * @tests java.util.LinkedHashSet
26 */
27public class LinkedHashSetTest extends junit.framework.TestCase {
28
29	LinkedHashSet hs;
30
31	static Object[] objArray;
32	{
33		objArray = new Object[1000];
34		for (int i = 0; i < objArray.length; i++)
35			objArray[i] = new Integer(i);
36	}
37
38	/**
39	 * @tests java.util.LinkedHashSet#LinkedHashSet()
40	 */
41	public void test_Constructor() {
42		// Test for method java.util.LinkedHashSet()
43		LinkedHashSet hs2 = new LinkedHashSet();
44		assertEquals("Created incorrect LinkedHashSet", 0, hs2.size());
45	}
46
47	/**
48	 * @tests java.util.LinkedHashSet#LinkedHashSet(int)
49	 */
50	public void test_ConstructorI() {
51		// Test for method java.util.LinkedHashSet(int)
52		LinkedHashSet hs2 = new LinkedHashSet(5);
53		assertEquals("Created incorrect LinkedHashSet", 0, hs2.size());
54		try {
55			new LinkedHashSet(-1);
56		} catch (IllegalArgumentException e) {
57			return;
58		}
59		fail(
60				"Failed to throw IllegalArgumentException for capacity < 0");
61	}
62
63	/**
64	 * @tests java.util.LinkedHashSet#LinkedHashSet(int, float)
65	 */
66	public void test_ConstructorIF() {
67		// Test for method java.util.LinkedHashSet(int, float)
68		LinkedHashSet hs2 = new LinkedHashSet(5, (float) 0.5);
69		assertEquals("Created incorrect LinkedHashSet", 0, hs2.size());
70		try {
71			new LinkedHashSet(0, 0);
72		} catch (IllegalArgumentException e) {
73			return;
74		}
75		fail(
76				"Failed to throw IllegalArgumentException for initial load factor <= 0");
77	}
78
79	/**
80	 * @tests java.util.LinkedHashSet#LinkedHashSet(java.util.Collection)
81	 */
82	public void test_ConstructorLjava_util_Collection() {
83		// Test for method java.util.LinkedHashSet(java.util.Collection)
84		LinkedHashSet hs2 = new LinkedHashSet(Arrays.asList(objArray));
85		for (int counter = 0; counter < objArray.length; counter++)
86			assertTrue("LinkedHashSet does not contain correct elements", hs
87					.contains(objArray[counter]));
88		assertTrue("LinkedHashSet created from collection incorrect size", hs2
89				.size() == objArray.length);
90	}
91
92	/**
93	 * @tests java.util.LinkedHashSet#add(java.lang.Object)
94	 */
95	public void test_addLjava_lang_Object() {
96		// Test for method boolean java.util.LinkedHashSet.add(java.lang.Object)
97		int size = hs.size();
98		hs.add(new Integer(8));
99		assertTrue("Added element already contained by set", hs.size() == size);
100		hs.add(new Integer(-9));
101		assertTrue("Failed to increment set size after add",
102				hs.size() == size + 1);
103		assertTrue("Failed to add element to set", hs.contains(new Integer(-9)));
104	}
105
106	/**
107	 * @tests java.util.LinkedHashSet#clear()
108	 */
109	public void test_clear() {
110		// Test for method void java.util.LinkedHashSet.clear()
111		Set orgSet = (Set) hs.clone();
112		hs.clear();
113		Iterator i = orgSet.iterator();
114		assertEquals("Returned non-zero size after clear", 0, hs.size());
115		while (i.hasNext())
116			assertTrue("Failed to clear set", !hs.contains(i.next()));
117	}
118
119	/**
120	 * @tests java.util.LinkedHashSet#clone()
121	 */
122	public void test_clone() {
123		// Test for method java.lang.Object java.util.LinkedHashSet.clone()
124		LinkedHashSet hs2 = (LinkedHashSet) hs.clone();
125		assertTrue("clone returned an equivalent LinkedHashSet", hs != hs2);
126		assertTrue("clone did not return an equal LinkedHashSet", hs
127				.equals(hs2));
128	}
129
130	/**
131	 * @tests java.util.LinkedHashSet#contains(java.lang.Object)
132	 */
133	public void test_containsLjava_lang_Object() {
134		// Test for method boolean
135		// java.util.LinkedHashSet.contains(java.lang.Object)
136		assertTrue("Returned false for valid object", hs.contains(objArray[90]));
137		assertTrue("Returned true for invalid Object", !hs
138				.contains(new Object()));
139
140		LinkedHashSet s = new LinkedHashSet();
141		s.add(null);
142		assertTrue("Cannot handle null", s.contains(null));
143	}
144
145	/**
146	 * @tests java.util.LinkedHashSet#isEmpty()
147	 */
148	public void test_isEmpty() {
149		// Test for method boolean java.util.LinkedHashSet.isEmpty()
150		assertTrue("Empty set returned false", new LinkedHashSet().isEmpty());
151		assertTrue("Non-empty set returned true", !hs.isEmpty());
152	}
153
154	/**
155	 * @tests java.util.LinkedHashSet#iterator()
156	 */
157	public void test_iterator() {
158		// Test for method java.util.Iterator java.util.LinkedHashSet.iterator()
159		Iterator i = hs.iterator();
160		int x = 0;
161		int j;
162		for (j = 0; i.hasNext(); j++) {
163			Object oo = i.next();
164			if (oo != null) {
165				Integer ii = (Integer) oo;
166				assertTrue("Incorrect element found", ii.intValue() == j);
167			} else {
168				assertTrue("Cannot find null", hs.contains(oo));
169			}
170			++x;
171		}
172		assertTrue("Returned iteration of incorrect size", hs.size() == x);
173
174		LinkedHashSet s = new LinkedHashSet();
175		s.add(null);
176		assertNull("Cannot handle null", s.iterator().next());
177	}
178
179	/**
180	 * @tests java.util.LinkedHashSet#remove(java.lang.Object)
181	 */
182	public void test_removeLjava_lang_Object() {
183		// Test for method boolean
184		// java.util.LinkedHashSet.remove(java.lang.Object)
185		int size = hs.size();
186		hs.remove(new Integer(98));
187		assertTrue("Failed to remove element", !hs.contains(new Integer(98)));
188		assertTrue("Failed to decrement set size", hs.size() == size - 1);
189
190		LinkedHashSet s = new LinkedHashSet();
191		s.add(null);
192		assertTrue("Cannot handle null", s.remove(null));
193	}
194
195	/**
196	 * @tests java.util.LinkedHashSet#size()
197	 */
198	public void test_size() {
199		// Test for method int java.util.LinkedHashSet.size()
200		assertTrue("Returned incorrect size", hs.size() == (objArray.length + 1));
201		hs.clear();
202		assertEquals("Cleared set returned non-zero size", 0, hs.size());
203	}
204
205	/**
206	 * Sets up the fixture, for example, open a network connection. This method
207	 * is called before a test is executed.
208	 */
209	protected void setUp() {
210		hs = new LinkedHashSet();
211		for (int i = 0; i < objArray.length; i++)
212			hs.add(objArray[i]);
213		hs.add(null);
214	}
215
216	/**
217	 * Tears down the fixture, for example, close a network connection. This
218	 * method is called after a test is executed.
219	 */
220	protected void tearDown() {
221	}
222}
223