Package com.carrotsearch.hppc

Source Code of com.carrotsearch.hppc.ByteOpenHashSetTest

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.ByteHashFunction;
import com.carrotsearch.hppc.predicates.BytePredicate;

/**
* Unit tests for {@link ByteOpenHashSet}.
*/
public class ByteOpenHashSetTest
{
    /**
     * Per-test fresh initialized instance.
     */
    public ByteOpenHashSet set;

    /* replaceIf:applied. */  byte  /* end */   key1 = 1;
    /* replaceIf:applied. */  byte  /* end */   key2 = 2;

    /* replaceIf:applied. */  byte  /* end */   defaultValue
        = ((byte) 0);

    /**
     * Require assertions for all tests.
     */
    @Rule
    public MethodRule requireAssertions = new RequireAssertionsRule();

    /* */
    @Before
    public void initialize()
    {
        set = new ByteOpenHashSet();
    }

    @After
    public void checkTrailingSpaceUninitialized()
    {
        if (set != null)
        {
            int occupied = 0;
            for (int i = 0; i < set.keys.length; i++)
            {
                if (set.states[i] == ByteOpenHashSet.EMPTY)
                {
                    assertEquals2(((byte) 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()
    {
        ByteOpenHashSet set2 = new ByteOpenHashSet();
        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( (byte2));
        assertFalse(set.remove( (byte2));
        assertEquals(4, set.size());
        assertSortedListEquals(set.toArray(), 0, 1, 3, 4);
    }

    /* */
    @Test
    public void testInitialCapacityAndGrowth()
    {
        for (int i = 0; i < 256; i++)
        {
            ByteOpenHashSet set = new ByteOpenHashSet(i);
           
            for (int j = 0; j < i; j++)
            {
                set.add( (bytej);
            }

            assertEquals(i, set.size());
        }
    }

    /* */
    @Test
    public void testRemoveAllFromLookupContainer()
    {
        set.add(newArray(set.keys, 0, 1, 2, 3, 4));

        ByteOpenHashSet list2 = new ByteOpenHashSet();
        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 BytePredicate()
        {
            public boolean apply(/* replaceIf:applied. */ byte  /* 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 BytePredicate()
        {
            public boolean apply(/* replaceIf:applied. */ byte  /* 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( (byte2);
        assertEquals(3, set.size());

        int count = 0;
        for (ByteCursor 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()
    {
        ByteOpenHashSet l0 = ByteOpenHashSet.from();
        assertEquals(0, l0.hashCode());
        assertEquals(l0, ByteOpenHashSet.from());

        ByteOpenHashSet l1 = ByteOpenHashSet.from(
             (byte1,
             (byte2,
             (byte3);

        ByteOpenHashSet l2 = ByteOpenHashSet.from(
             (byte1,
             (byte2,
             (byte3);

        assertEquals(l1.hashCode(), l2.hashCode());
        assertEquals(l1, l2);
    }

    /* removeIf:applied. */
   

    /* removeIf:applied. */   
}
TOP

Related Classes of com.carrotsearch.hppc.ByteOpenHashSetTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.