Skip to content

Commit

Permalink
MINOR: Fix bugs in handling zero-length ImplicitLinkedHashCollections (
Browse files Browse the repository at this point in the history
…apache#7163)

Reviewers: Colin P. McCabe <cmccabe@apache.org>
  • Loading branch information
mimaison authored and cmccabe committed Aug 15, 2019
1 parent 4d0cc43 commit 9cb27f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Expand Up @@ -304,7 +304,7 @@ final int slot(Element[] curElements, Object e) {
* @return The match index, or INVALID_INDEX if no match was found.
*/
final private int findIndexOfEqualElement(Object key) {
if (key == null) {
if (key == null || size == 0) {
return INVALID_INDEX;
}
int slot = slot(elements, key);
Expand Down
Expand Up @@ -91,7 +91,7 @@ int addInternal(Element newElement, Element[] addElements) {
*/
@Override
int findElementToRemove(Object key) {
if (key == null) {
if (key == null || size() == 0) {
return INVALID_INDEX;
}
int slot = slot(elements, key);
Expand Down Expand Up @@ -120,7 +120,7 @@ int findElementToRemove(Object key) {
* @return All of the matching elements.
*/
final public List<E> findAll(E key) {
if (key == null) {
if (key == null || size() == 0) {
return Collections.<E>emptyList();
}
ArrayList<E> results = new ArrayList<>();
Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.util.Set;

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -271,7 +272,7 @@ public void testListViewModification() {
@Test
public void testEmptyListIterator() {
ImplicitLinkedHashCollection<TestElement> coll = new ImplicitLinkedHashCollection<>();
ListIterator iter = coll.valuesList().listIterator();
ListIterator<TestElement> iter = coll.valuesList().listIterator();
assertFalse(iter.hasNext());
assertFalse(iter.hasPrevious());
assertEquals(0, iter.nextIndex());
Expand Down Expand Up @@ -513,6 +514,14 @@ public void testEquals() {
assertNotEquals(coll2, coll3);
}

@Test
public void testFindContainsRemoveOnEmptyCollection() {
ImplicitLinkedHashCollection<TestElement> coll = new ImplicitLinkedHashCollection<>();
assertNull(coll.find(new TestElement(2)));
assertFalse(coll.contains(new TestElement(2)));
assertFalse(coll.remove(new TestElement(2)));
}

private void addRandomElement(Random random, LinkedHashSet<Integer> existing,
ImplicitLinkedHashCollection<TestElement> set) {
int next;
Expand All @@ -523,6 +532,7 @@ private void addRandomElement(Random random, LinkedHashSet<Integer> existing,
set.add(new TestElement(next));
}

@SuppressWarnings("unlikely-arg-type")
private void removeRandomElement(Random random, Collection<Integer> existing,
ImplicitLinkedHashCollection<TestElement> coll) {
int removeIdx = random.nextInt(existing.size());
Expand Down
Expand Up @@ -28,6 +28,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -44,6 +45,15 @@ public void testNullForbidden() {
assertFalse(multiSet.add(null));
}

@Test
public void testFindFindAllContainsRemoveOnEmptyCollection() {
ImplicitLinkedHashMultiCollection<TestElement> coll = new ImplicitLinkedHashMultiCollection<>();
assertNull(coll.find(new TestElement(2)));
assertFalse(coll.contains(new TestElement(2)));
assertFalse(coll.remove(new TestElement(2)));
assertTrue(coll.findAll(new TestElement(2)).isEmpty());
}

@Test
public void testInsertDelete() {
ImplicitLinkedHashMultiCollection<TestElement> multiSet = new ImplicitLinkedHashMultiCollection<>(100);
Expand Down

0 comments on commit 9cb27f5

Please sign in to comment.