All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliasi.test.unit.util.CompactHashSetTest Maven / Gradle / Ivy

Go to download

This is the original Lingpipe: http://alias-i.com/lingpipe/web/download.html There were not made any changes to the source code.

There is a newer version: 4.1.2-JL1.0
Show newest version
package com.aliasi.test.unit.util;

import com.aliasi.util.AbstractExternalizable;
import com.aliasi.util.CompactHashSet;

import static com.aliasi.test.unit.Asserts.succeed;
import static com.aliasi.test.unit.Asserts.assertFullEquals;
import static com.aliasi.test.unit.Asserts.assertFullSerialization;

import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

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

import java.io.IOException;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

public class CompactHashSetTest  {


    @Test
    public void testConstruct() {
        new CompactHashSet(1);
        new CompactHashSet(2);
        new CompactHashSet(3);
        new CompactHashSet(4);
        new CompactHashSet(912);
        new CompactHashSet(15485863);
    }

    @Test(expected=IllegalArgumentException.class)
    public void testConstruxNeg() {
        new CompactHashSet(-1);
    }

    @Test(expected=IllegalArgumentException.class)
    public void testConstrux0() {
        new CompactHashSet(0);
    }

    @Test(expected=OutOfMemoryError.class)
    public void testConstruxTooBig() {
        // if you find a JVM that'll handle this big an array
        // let us know via e-mail to [email protected]
        new CompactHashSet(Integer.MAX_VALUE);
    }

    @Test
    public void testZeroSize() {
        Set set = new CompactHashSet(1);
        assertSetEquals(new HashSet(), set);
    }

    @Test
    public void test1Element() {
        Set xSet = hashSet("foo");
        Set ySet = new CompactHashSet(15);
        ySet.add("foo");
        assertSetEquals(xSet,ySet);

        Set ySet2 = new CompactHashSet(1);
        ySet2.add("foo");
        assertSetEquals(xSet,ySet2);
    }

    @Test
    public void test2Elements15() {
        Set xSet = hashSet("foo","bar");
        Set ySet = new CompactHashSet(15);
        ySet.addAll(xSet);
        assertSetEquals(xSet,ySet);
    }

    @Test
    public void testSimp() {
        Set ySet = new CompactHashSet(1);
        assertTrue(ySet.add("foo"));
        assertTrue(ySet.add("bar"));
    }

    @Test
    public void test2Elements1() {
        Set xSet = hashSet("bing","badkdki3lkawelkfj");
        Set ySet = new CompactHashSet(1);
        assertTrue(ySet.add("bing"));
	assertEquals(1,ySet.size());
        assertFalse(ySet.add("bing"));
	assertEquals(1,ySet.size());
        assertTrue(ySet.add("badkdki3lkawelkfj"));
	assertEquals(2,ySet.size());
        assertEquals(xSet,ySet);
    }

    @Test
    public void test5Elements1() {
        assertAdds(1,"abcd","12345","ZCXKDKD","s","yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
    }

    @Test
    public void testIteratorRemoves() {
        assertRemoves(1,"a");
    }

    static void assertAdds(int initialSize, String... elts) {
        Random random = new Random(42);
        String s1 = Arrays.asList(elts).toString();
        com.aliasi.util.Arrays.permute(elts,random);
        Set xSet = new HashSet();
        Set ySet = new CompactHashSet(initialSize);
        for (String elt : elts) {
            assertTrue(xSet.add(elt));
            assertFalse(xSet.add(elt));
            assertTrue(ySet.add(elt));
            assertFalse(ySet.add(elt));
            assertFullEquals("init=" + initialSize + " ",xSet,ySet);
        }
        com.aliasi.util.Arrays.permute(elts,random);
        String s2 = Arrays.asList(elts).toString();
        for (String elt : elts) {
            assertTrue(xSet.remove(elt));
            assertFalse(xSet.remove(elt));
            assertTrue(ySet.remove(elt));
            assertFalse(ySet.remove(elt));
            for (String x : xSet)
                assertTrue("\nx=" + x + "ySet=" + ySet + " s1=" + s1 + " s2=" + s2,ySet.contains(x));
            assertEquals(ySet,xSet);
            assertFullEquals("init=" + initialSize + "\n" + "xSet=" + xSet + "\nySet=" + ySet + "\ns1=" + s1 + "\ns2=" + s2 + "\n",
                             xSet,ySet);
        }
    }

    static void assertRemoves(int initialSize, String... elts) {
        Set xSet = hashSet(elts);
        Set ySet = new CompactHashSet(10);
        ySet.addAll(xSet);
        Iterator it = ySet.iterator();
        for (int i = 0; i < elts.length; ++i) {
            assertEquals(xSet,ySet);
            assertIllegalRemoveState(it);
            String s = it.next();
            it.remove();
            xSet.remove(s);
        }
        assertIllegalRemoveState(it);
        assertSetEquals(xSet,ySet);
    }

    static void assertIllegalRemoveState(Iterator it) {
        try {
            it.remove();
            fail();
        } catch (IllegalStateException e) {
            succeed();
        }
    }


    static Set hashSet(String... xs) {
        Set set = new HashSet();
        for (String x : xs)
            set.add(x);
        return set;
    }

    // xs will be expected; ys will be the small set
    static void assertSetEquals(Set xSet, Set ySet) {
        assertFullEquals(xSet,ySet);
        assertFullSerialization(ySet);

        assertEquals(xSet.size(), ySet.size());
        assertEquals(xSet.isEmpty(), ySet.isEmpty());

        Iterator xIt = xSet.iterator();
        Iterator yIt = ySet.iterator();
        Set xItSet = new HashSet();
        Set yItSet = new HashSet();
        while (xIt.hasNext()) {
            xItSet.add(xIt.next());
            yItSet.add(yIt.next());
        }
        assertFalse(yIt.hasNext());
        assertEquals(xItSet,yItSet);

        String[] xsS = xSet.toArray(new String[0]);
        String[] ysS = ySet.toArray(new String[ySet.size()]);
        Arrays.sort(xsS);
        Arrays.sort(ysS);
        assertArrayEquals(xsS,ysS);

        Object[] xs = xSet.toArray();
        Object[] ys = ySet.toArray();
        Arrays.sort(xs);
        Arrays.sort(ys);
        assertArrayEquals(xs,ys);

        for (String x : xSet)
            assertTrue(ySet.contains(x));
        
        for (String y : ySet)
            assertTrue(xSet.contains(y));


    }
    

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy