package com.carrotsearch.hppc;
import static com.carrotsearch.hppc.TestUtils.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Comparator;
import org.junit.*;
import org.junit.rules.MethodRule;
import com.carrotsearch.hppc.cursors.*;
import com.carrotsearch.hppc.hash.IntHashFunction;
import com.carrotsearch.hppc.predicates.IntPredicate;
/**
* Unit tests for {@link IntOpenHashSet}.
*/
public class IntOpenHashSetTest
{
/**
* Per-test fresh initialized instance.
*/
public IntOpenHashSet set;
/* replaceIf:applied. */ int /* end */ key1 = 1;
/* replaceIf:applied. */ int /* end */ key2 = 2;
/* replaceIf:applied. */ int /* end */ defaultValue
= ((int) 0);
/**
* Require assertions for all tests.
*/
@Rule
public MethodRule requireAssertions = new RequireAssertionsRule();
/* */
@Before
public void initialize()
{
set = new IntOpenHashSet();
}
@After
public void checkTrailingSpaceUninitialized()
{
if (set != null)
{
int occupied = 0;
for (int i = 0; i < set.keys.length; i++)
{
if (set.states[i] == IntOpenHashSet.EMPTY)
{
assertEquals2(((int) 0), set.keys[i]);
}
else
{
occupied++;
}
}
assertEquals(occupied, set.deleted + set.assigned);
}
}
/* */
@Test
public void testInitiallyEmpty()
{
assertEquals(0, set.size());
}
/* */
@Test
public void testAdd()
{
assertTrue(set.add(key1));
assertFalse(set.add(key1));
assertEquals(1, set.size());
}
/* */
@Test
public void testAdd2()
{
set.add(key1, key1);
assertEquals(1, set.size());
assertEquals(1, set.add(key1, key2));
assertEquals(2, set.size());
}
/* */
@Test
public void testAddVarArgs()
{
set.add(newArray(set.keys, 0, 1, 2, 1, 0));
assertEquals(3, set.size());
assertSortedListEquals(set.toArray(), 0, 1, 2);
}
/* */
@Test
public void testAddAll()
{
IntOpenHashSet set2 = new IntOpenHashSet();
set2.add(newArray(set2.keys, 1, 2));
set.add(newArray(set2.keys, 0, 1));
assertEquals(1, set.addAll(set2));
assertEquals(0, set.addAll(set2));
assertEquals(3, set.size());
assertSortedListEquals(set.toArray(), 0, 1, 2);
}
/* */
@Test
public void testRemove()
{
set.add(newArray(set.keys, 0, 1, 2, 3, 4));
assertTrue(set.remove( (int) 2));
assertFalse(set.remove( (int) 2));
assertEquals(4, set.size());
assertSortedListEquals(set.toArray(), 0, 1, 3, 4);
}
/* */
@Test
public void testInitialCapacityAndGrowth()
{
for (int i = 0; i < 256; i++)
{
IntOpenHashSet set = new IntOpenHashSet(i);
for (int j = 0; j < i; j++)
{
set.add( (int) j);
}
assertEquals(i, set.size());
}
}
/* */
@Test
public void testRemoveAllFromLookupContainer()
{
set.add(newArray(set.keys, 0, 1, 2, 3, 4));
IntOpenHashSet list2 = new IntOpenHashSet();
list2.add(newArray(list2.keys, 1, 3, 5));
assertEquals(2, set.removeAll(list2));
assertEquals(3, set.size());
assertSortedListEquals(set.toArray(), 0, 2, 4);
}
/* */
@Test
public void testRemoveAllWithPredicate()
{
set.add(newArray(set.keys, 0, key1, key2));
assertEquals(1, set.removeAll(new IntPredicate()
{
public boolean apply(/* replaceIf:applied. */ int /* end */ v)
{
return v == key1;
};
}));
assertSortedListEquals(set.toArray(), 0, key2);
}
/* */
@Test
public void testRetainAllWithPredicate()
{
set.add(newArray(set.keys, 0, key1, key2, 3, 4, 5));
assertEquals(4, set.retainAll(new IntPredicate()
{
public boolean apply(/* replaceIf:applied. */ int /* end */ v)
{
return v == key1 || v == key2;
};
}));
assertSortedListEquals(set.toArray(), key1, key2);
}
/* */
@Test
public void testClear()
{
set.add(newArray(set.keys, 1, 2, 3));
set.clear();
checkTrailingSpaceUninitialized();
assertEquals(0, set.size());
}
/* */
@Test
public void testIterable()
{
set.add(newArray(set.keys, 1, 2, 2, 3, 4));
set.remove( (int) 2);
assertEquals(3, set.size());
int count = 0;
for (IntCursor cursor : set)
{
count++;
assertTrue(set.contains(cursor.value));
assertEquals2(cursor.value, set.lget());
}
assertEquals(count, set.size());
set.clear();
assertFalse(set.iterator().hasNext());
}
/* removeIf:applied. */
/* removeIf:applied. */
/* */
@Test
public void testHashCodeEquals()
{
IntOpenHashSet l0 = IntOpenHashSet.from();
assertEquals(0, l0.hashCode());
assertEquals(l0, IntOpenHashSet.from());
IntOpenHashSet l1 = IntOpenHashSet.from(
(int) 1,
(int) 2,
(int) 3);
IntOpenHashSet l2 = IntOpenHashSet.from(
(int) 1,
(int) 2,
(int) 3);
assertEquals(l1.hashCode(), l2.hashCode());
assertEquals(l1, l2);
}
/* removeIf:applied. */
/* removeIf:applied. */
}