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>(); assertThat(set.size()).isZero(); for (int i = 0; i < elements.length; i++) { TestElement element = set.put(elements[i]); assertThat(element).isNull(); } assertThat(set.size()).isEqualTo(elements.length); for (int i = 0; i < elements.length; i++) { assertThat(set.contains(elements[i])).isTrue(); TestElement element = set.get(elements[i]); assertThat(elements[i].getData()).isEqualTo(element.getData()); } TestKey[] keys = getKeys(elements); TestElement[] newElements = generateElements(keys); for (int i = 0; i < newElements.length; i++) { TestElement element = set.put(newElements[i]); assertThat(element).isNotNull(); } assertThat(set.size()).isEqualTo(elements.length); for (int i = 0; i < keys.length; i++) { assertThat(set.contains(keys[i])).isTrue(); TestElement element = set.get(keys[i]); assertThat(newElements[i].getData()).isEqualTo(element.getData()); } Collection<TestElement> cElements = set.values(); assertThat(cElements.size()).isEqualTo(elements.length); for (TestElement element : cElements) { assertThat(set.contains(element)).isTrue(); } for (int i = 0; i < keys.length; i++) { TestElement element = set.remove(keys[i]); assertThat(element).isNotNull(); assertThat(set.contains(keys[i])).isFalse(); } assertThat(set.size()).isZero(); }
@Test(timeout = 60000) public void testRemoveAll() { TestElement[] elements = generateElements(1 << 16); final LightWeightResizableGSet<TestKey, TestElement> set = new LightWeightResizableGSet<TestKey, TestElement>(); assertThat(set.size()).isZero(); for (int i = 0; i < elements.length; i++) { TestElement element = set.put(elements[i]); assertThat(element).isNull(); } assertThat(set.size()).isEqualTo(elements.length); { set.clear(); assertThat(set.size()).isZero(); for (int i = 0; i < elements.length; i++) { assertThat(set.contains(elements[i])).isFalse(); } assertThat(set.iterator().hasNext()).isFalse(); } for (int i = 0; i < elements.length; i++) { TestElement element = set.put(elements[i]); assertThat(element).isNull(); } { for (Iterator<TestElement> iter = set.iterator(); iter.hasNext(); ) { TestElement element = iter.next(); assertThat(set.contains(element)).isTrue(); iter.remove(); assertThat(set.contains(element)).isFalse(); } for (int i = 0; i < elements.length; i++) { assertThat(set.contains(elements[i])).isFalse(); } assertThat(set.iterator().hasNext()).isFalse(); assertThat(set.size()).isZero(); } }