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