LightWeightResizableGSet extends LightWeightGSet<K, E> { @Override public E put(final E element) { E existing = super.put(element); expandIfNecessary(); return existing; } LightWeightResizableGSet(int initCapacity, float loadFactor);  LightWeightResizableGSet();  LightWeightResizableGSet(int initCapacity); @Override E put(final E element);  }
@Test(timeout = 60000) public void testBasicOperations() { TestElement[] elements = generateElements(1 << 16); final LightWeightResizableGSet<TestKey, TestElement> set = new LightWeightResizableGSet<TestKey, TestElement>(); assertEquals(set.size(), 0); for (int i = 0; i < elements.length; i++) { TestElement element = set.put(elements[i]); assertTrue(element == null); } assertEquals(set.size(), elements.length); for (int i = 0; i < elements.length; i++) { assertTrue(set.contains(elements[i])); TestElement element = set.get(elements[i]); assertEquals(elements[i].getData(), element.getData()); } TestKey[] keys = getKeys(elements); TestElement[] newElements = generateElements(keys); for (int i = 0; i < newElements.length; i++) { TestElement element = set.put(newElements[i]); assertTrue(element != null); } assertEquals(set.size(), elements.length); for (int i = 0; i < keys.length; i++) { assertTrue(set.contains(keys[i])); TestElement element = set.get(keys[i]); assertEquals(newElements[i].getData(), element.getData()); } Collection<TestElement> cElements = set.values(); assertEquals(cElements.size(), elements.length); for (TestElement element : cElements) { assertTrue(set.contains(element)); } for (int i = 0; i < keys.length; i++) { TestElement element = set.remove(keys[i]); assertTrue(element != null); assertFalse(set.contains(keys[i])); } assertEquals(set.size(), 0); }
@Test(timeout = 60000) public void testRemoveAll() { TestElement[] elements = generateElements(1 << 16); final LightWeightResizableGSet<TestKey, TestElement> set = new LightWeightResizableGSet<TestKey, TestElement>(); assertEquals(set.size(), 0); for (int i = 0; i < elements.length; i++) { TestElement element = set.put(elements[i]); assertTrue(element == null); } assertEquals(set.size(), elements.length); { set.clear(); assertEquals(set.size(), 0); for (int i = 0; i < elements.length; i++) { assertFalse(set.contains(elements[i])); } assertFalse(set.iterator().hasNext()); } for (int i = 0; i < elements.length; i++) { TestElement element = set.put(elements[i]); assertTrue(element == null); } { for (Iterator<TestElement> iter = set.iterator(); iter.hasNext(); ) { TestElement element = iter.next(); assertTrue(set.contains(element)); iter.remove(); assertFalse(set.contains(element)); } for (int i = 0; i < elements.length; i++) { assertFalse(set.contains(elements[i])); } assertFalse(set.iterator().hasNext()); assertEquals(set.size(), 0); } }