Package org.squarebrackets

Source Code of org.squarebrackets.UnboundedShortArrayTest

package org.squarebrackets;

import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.squarebrackets.ArrayCharacteristics.DISTINCT;
import static org.squarebrackets.ArrayCharacteristics.SORTED;

/**
* @author Leon van Zantvoort
*/
@SuppressWarnings({"MissingMethodJavaDoc", "MissingFieldJavaDoc"})
public class UnboundedShortArrayTest {

    private UnboundedShortArray array;

    @Before
    public void setUp() throws Exception {
        array = UnboundedShortArray.newInstance();
        array.addShort((short) 0);
        array.addShort((short) 1);
        array.addShort((short) 1);
        array.addShort((short) 2);
        array.addShort((short) 1);
        array.addShort((short) 2);
        array.addShort((short) 3);
        array.addShort((short) 3);
        array.addShort((short) 4);
        array = Arrays.synchronizedArray(array, array);    // Test synchronized array.
        array = serializeAndDeserialize(array); // Test serialization.
        array = array.offset(1);
    }

    @Test
    public void nullable() {
        assertTrue(array.hasCharacteristics(ArrayCharacteristics.NONNULL));
    }

    @Test
    public void mutable() {
        assertTrue(array.hasCharacteristics(ArrayCharacteristics.MUTABLE));
    }

    @Test
    public void resizable() {
        assertTrue(array.hasCharacteristics(ArrayCharacteristics.RESIZABLE));
    }

    @Test
    public void replaceable() {
        assertTrue(array.hasCharacteristics(ArrayCharacteristics.REPLACEABLE));
    }

    @Test
    public void copyFactory() throws Exception {
        UnboundedShortArray d = UnboundedShortArray.copyOf(array);
        d.setShort(0, (short) 0);
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray());
        assertArrayEquals(new short[]{0, 1, 2, 1, 2, 3, 3, 4}, d.toArray());
    }

    @Test
    public void testSerialization() throws Exception {
        Array<?> a = serializeAndDeserialize(array);
        assertEquals(array, a);
    }

    @Test
    public void testRemainingCapacity() {
        assertEquals(Integer.MAX_VALUE, array.remainingCapacity());
    }

    @Test
    public void testSetAll() throws Exception {
        UnboundedShortArray smaller = UnboundedShortArray.copyOf(array);
        smaller.shortRemove();
        array.setAll(smaller);
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3}, array.toArray());
        array.addShort((short) 4);
        MutableShortArray backedByArray = MutableShortArray.copyOf(array);
        Arrays.reverse(backedByArray);
        array.setAll(backedByArray);
        assertArrayEquals(new short[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray());
        ShortBuffer buffer = ByteBuffer.allocateDirect(Short.SIZE / Byte.SIZE * 8).asShortBuffer();
        MutableShortArray notBackedByArray = Arrays.newMutableArray(buffer);
        notBackedByArray.setAll(backedByArray);
        array.setAll(notBackedByArray);
        assertArrayEquals(new short[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray());
        UnboundedShortArray bigger = UnboundedShortArray.copyOf(array);
        bigger.addShort((short) 0);
        array.setAll(bigger);
        assertArrayEquals(new short[]{4, 3, 3, 2, 1, 2, 1, 1, 0}, array.toArray());
        bigger.addShort((short) 0);
        array.setAll(bigger);
        assertArrayEquals(new short[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0}, array.toArray());
        bigger.addShort((short) 0);
        array.setAll(bigger);
        assertArrayEquals(new short[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0, 0}, array.toArray());
    }

    @Test
    public void testContains() throws Exception {
        assertTrue(array.containsShort((short) 1));
        assertTrue(array.containsShort((short) 4));
        assertFalse(array.containsShort((short) 5));
        assertFalse(array.length(7).containsShort((short) 4));
        assertFalse(array.contains(null));
    }

    @Test
    public void testContainsObject() throws Exception {
        assertTrue(array.contains((short) 1));
        assertTrue(array.contains((short) 4));
        assertFalse(array.contains((short) 5));
        assertFalse(array.length(7).contains((short) 4));
        assertFalse(array.contains(null));
    }

    @Test
    public void testContainsSorted() throws Exception {
        array.sort();
        assertTrue(array.containsShort((short) 1));
        assertTrue(array.containsShort((short) 4));
        assertFalse(array.containsShort((short) 5));
        assertFalse(array.length(7).containsShort((short) 4));
        assertFalse(array.contains(null));
    }

    @Test
    public void testContainsAll() throws Exception {
        MutableShortArray testTrue = MutableShortArray.valueOf(new short[]{1, 2, 3});
        MutableShortArray testFalse = MutableShortArray.valueOf(new short[]{3, 4, 5});
        assertTrue(array.containsAll(testTrue));
        assertFalse(array.length(5).containsAll(testTrue));
        assertFalse(array.containsAll(testFalse));
        UnboundedObjectArray<Short> objectTestTrue = UnboundedObjectArray.valueOf((short) 1, (short) 2, (short) 3);
        assertTrue(array.containsAll(objectTestTrue));
        UnboundedObjectArray<Short> objectTestFalse = UnboundedObjectArray.valueOf((short) 3, (short) 4, (short) 5);
        assertFalse(array.containsAll(objectTestFalse));
        UnboundedObjectArray<Integer> objectTestOther = UnboundedObjectArray.valueOf(1, 2, 3);
        assertFalse(array.containsAll(objectTestOther));
    }

    @Test
    public void testContainsAllSorted() throws Exception {
        array.sort();
        MutableShortArray testTrue = MutableShortArray.valueOf(new short[]{1, 2, 3});
        testTrue.sort();
        MutableShortArray testFalse = MutableShortArray.valueOf(new short[]{3, 4, 5});
        testFalse.sort();
        assertTrue(array.containsAll(testTrue));
        assertFalse(array.length(5).containsAll(testTrue));
        assertFalse(array.containsAll(testFalse));
    }

    @Test
    public void testIndexOf() throws Exception {
        assertEquals(-1, array.indexOf(null));
        assertEquals(0, array.indexOfShort((short) 1));
        assertEquals(7, array.indexOfShort((short) 4));
    }

    @Test
    public void testLastIndexOf() throws Exception {
        assertEquals(-1, array.lastIndexOf(null));
        assertEquals(3, array.lastIndexOfShort((short) 1));
        assertEquals(7, array.lastIndexOfShort((short) 4));
    }

    @Test
    public void testIndexOfSubArray() throws Exception {
        assertEquals(-1, array.offset(1).indexOf(null));
        assertEquals(0, array.offset(1).indexOfShort((short) 1));
        assertEquals(6, array.offset(1).indexOfShort((short) 4));
    }

    @Test
    public void testLastIndexOfSubArray() throws Exception {
        assertEquals(-1, array.offset(1).lastIndexOf(null));
        assertEquals(2, array.offset(1).lastIndexOfShort((short) 1));
        assertEquals(6, array.offset(1).lastIndexOfShort((short) 4));
    }

    @Test
    public void testIndexOfSorted() throws Exception {
        array.sort();
        assertEquals(-1, array.indexOf(null));
        assertEquals(0, array.indexOfShort((short) 1));
        assertEquals(7, array.indexOfShort((short) 4));
    }

    @Test
    public void testLastIndexOfSorted() throws Exception {
        array.sort();
        assertEquals(-1, array.lastIndexOf(null));
        assertEquals(2, array.lastIndexOfShort((short) 1));
        assertEquals(7, array.lastIndexOfShort((short) 4));
    }

    @Test
    public void testIndexOfSortedSubArray() throws Exception {
        array.sort();
        assertEquals(-1, array.offset(1).indexOf(null));
        assertEquals(0, array.offset(1).indexOfShort((short) 1));
        assertEquals(6, array.offset(1).indexOfShort((short) 4));
    }

    @Test
    public void testLastIndexOfSortedSubArray() throws Exception {
        array.sort();
        assertEquals(-1, array.offset(1).lastIndexOf(null));
        assertEquals(1, array.offset(1).lastIndexOfShort((short) 1));
        assertEquals(6, array.offset(1).lastIndexOfShort((short) 4));
    }
   
    @Test
    public void testRemove() throws Exception {
        assertTrue(array.removeShort((short) 1));
        assertArrayEquals(new short[]{1, 2, 1, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.removeShort((short) 1));
        assertArrayEquals(new short[]{2, 1, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.removeShort((short) 1));
        assertArrayEquals(new short[]{2, 2, 3, 3, 4}, array.toArray());
        assertFalse(array.removeShort((short) 1));
        assertArrayEquals(new short[]{2, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.removeShort((short) 4));
        assertArrayEquals(new short[]{2, 2, 3, 3}, array.toArray());
        assertFalse(array.removeShort((short) 5));
        assertArrayEquals(new short[]{2, 2, 3, 3}, array.toArray());
    }

    @Test
    public void testRemoveSort() throws Exception {
        array.sort();
        assertTrue(array.removeShort((short) 1));
        assertArrayEquals(new short[]{1, 1, 2, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.removeShort((short) 1));
        assertArrayEquals(new short[]{1, 2, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.removeShort((short) 1));
        assertArrayEquals(new short[]{2, 2, 3, 3, 4}, array.toArray());
        assertFalse(array.removeShort((short) 1));
        assertArrayEquals(new short[]{2, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.removeShort((short) 4));
        assertArrayEquals(new short[]{2, 2, 3, 3}, array.toArray());
        assertFalse(array.removeShort((short) 5));
        assertArrayEquals(new short[]{2, 2, 3, 3}, array.toArray());
    }

    @Test
    public void testRemoveObject() throws Exception {
        assertTrue(array.remove((Object) (short) 1));
        assertArrayEquals(new short[]{1, 2, 1, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.remove((Object) (short) 1));
        assertArrayEquals(new short[]{2, 1, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.remove((Object) (short) 1));
        assertArrayEquals(new short[]{2, 2, 3, 3, 4}, array.toArray());
        assertFalse(array.remove((Object) (short) 1));
        assertArrayEquals(new short[]{2, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.remove((Object) (short) 4));
        assertArrayEquals(new short[]{2, 2, 3, 3}, array.toArray());
        assertFalse(array.remove((Object) (short) 5));
        assertArrayEquals(new short[]{2, 2, 3, 3}, array.toArray());
    }

    @Test
    public void testRemoveAll1() throws Exception {
        MutableShortArray a = MutableShortArray.valueOf(new short[]{1, 2, 3});
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{4}, array.toArray());
    }

    @Test
    public void testRemoveAll2() throws Exception {
        MutableShortArray a = MutableShortArray.valueOf(new short[]{3, 4, 5});
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{1, 1, 2, 1, 2}, array.toArray());
    }

    @Test
    public void testRemoveAllSubArray() {
        MutableShortArray a = MutableShortArray.valueOf(new short[]{1, 2, 3});
        assertTrue(array.subArray(4, 7).removeAll(a));
        assertArrayEquals(new short[]{1, 1, 2, 1, 4}, array.toArray());
    }

    @Test
    public void testRemoveAllSorted1() throws Exception {
        array.sort();
        MutableShortArray a = MutableShortArray.valueOf(new short[]{1, 2, 3});
        a.sort();
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{4}, array.toArray());
    }

    @Test
    public void testRemoveAllSorted2() throws Exception {
        array.sort();
        MutableShortArray a = MutableShortArray.valueOf(new short[]{3, 4, 5});
        a.sort();
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{1, 1, 1, 2, 2}, array.toArray());
    }

    @Test
    public void testRemoveAllDistinct1() throws Exception {
        array.distinct();
        MutableShortArray a = MutableShortArray.valueOf((short) 1, (short) 2, (short) 3);
        a.sort();
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{4}, array.toArray());
    }

    @Test
    public void testRemoveAllDistinct2() throws Exception {
        array.distinct();
        MutableShortArray a = MutableShortArray.valueOf((short) 3, (short) 4, (short) 5);
        a.sort();
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{1, 2}, array.toArray());
    }

    @Test
    public void testRemoveAllDistinct3() throws Exception {
        array.distinct();
        MutableShortArray a = MutableShortArray.valueOf((short) 1, (short) 2, (short) 3, (short) 1, (short) 2, (short) 3, (short) 1, (short) 2, (short) 3);
        a.sort();
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{4}, array.toArray());
    }

    @Test
    public void testRemoveAllDistinct4() throws Exception {
        array.distinct();
        MutableShortArray a = MutableShortArray.valueOf((short) 3, (short) 4, (short) 5, (short) 3, (short) 4, (short) 5, (short) 3, (short) 4, (short) 5);
        a.sort();
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{1, 2}, array.toArray());
    }

    @Test
    public void testRemoveAllObject1() throws Exception {
        MutableObjectArray<Short> a = MutableObjectArray.valueOf((short) 1, (short) 2, (short) 3);
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{4}, array.toArray());
    }

    @Test
    public void testRemoveAllObject2() throws Exception {
        MutableObjectArray<Short> a = MutableObjectArray.valueOf((short) 3, (short) 4, (short) 5);
        assertTrue(array.removeAll(a));
        assertArrayEquals(new short[]{1, 1, 2, 1, 2}, array.toArray());
    }

    @Test
    public void testRetainAll1() throws Exception {
        MutableShortArray a = MutableShortArray.valueOf(new short[]{1, 2, 3});
        assertTrue(array.retainAll(a));
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3}, array.toArray());
    }

    @Test
    public void testRetainAll2() throws Exception {
        MutableShortArray a = MutableShortArray.valueOf(new short[]{3, 4, 5});
        assertTrue(array.retainAll(a));
        assertArrayEquals(new short[]{3, 3, 4}, array.toArray());
    }

    @Test
    public void testRetainAllSorted1() throws Exception {
        array.sort();
        MutableShortArray a = MutableShortArray.valueOf(new short[]{1, 2, 3});
        a.sort();
        assertTrue(array.retainAll(a));
        assertArrayEquals(new short[]{1, 1, 1, 2, 2, 3, 3}, array.toArray());
    }

    @Test
    public void testRetainAllSorted2() throws Exception {
        array.sort();
        MutableShortArray a = MutableShortArray.valueOf(new short[]{3, 4, 5});
        a.sort();
        assertTrue(array.retainAll(a));
        assertArrayEquals(new short[]{3, 3, 4}, array.toArray());
    }

    @Test
    public void testGet() throws Exception {
        try {
            array.getShort(-1);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        assertEquals(1, array.getShort(0));
        assertEquals(1, array.getShort(1));
        assertEquals(2, array.getShort(2));
        assertEquals(1, array.getShort(3));
        assertEquals(2, array.getShort(4));
        assertEquals(3, array.getShort(5));
        assertEquals(3, array.getShort(6));
        assertEquals(4, array.getShort(7));
        try {
            assertEquals(1, array.getShort(8));
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray());
    }

    @Test
    public void testAdd() throws Exception {
        try {
            array.addShort(-1, (short) 5);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.addShort((short) 5);
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3, 4, 5}, array.toArray());
        array.addShort(2, (short) 6);
        assertArrayEquals(new short[]{1, 1, 6, 2, 1, 2, 3, 3, 4, 5}, array.toArray());
        array.addShort(0, (short) 7);
        assertArrayEquals(new short[]{7, 1, 1, 6, 2, 1, 2, 3, 3, 4, 5}, array.toArray());
        array.addShort(array.length(), (short) 8);
        assertArrayEquals(new short[]{7, 1, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8}, array.toArray());
        try {
            array.addShort(array.length() + 1, (short) 9);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.addAll(ShortArray.unsafeValueOf(new short[]{9, 9}));
        assertArrayEquals(new short[]{7, 1, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8, 9, 9}, array.toArray());
        try {
            array.addAll(-1, ShortArray.unsafeValueOf(new short[]{10, 10}));
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.addAll(2, ShortArray.unsafeValueOf(new short[]{10, 10}));
        assertArrayEquals(new short[]{7, 1, 10, 10, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8, 9, 9}, array.toArray());
        array.addAll(0, ShortArray.unsafeValueOf(new short[]{11, 11}));
        assertArrayEquals(new short[]{11, 11, 7, 1, 10, 10, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8, 9, 9}, array.toArray());
        array.addAll(array.length(), ShortArray.unsafeValueOf(new short[]{12, 12}));
        assertArrayEquals(new short[]{11, 11, 7, 1, 10, 10, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8, 9, 9, 12, 12}, array.toArray());
        try {
            array.addAll(array.length() + 1, ShortArray.unsafeValueOf(new short[]{12, 12}));
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
    }

    @Test
    public void testAddSubArrayConcurrentModification1() throws Exception {
        UnboundedShortArray sub = array.offset(0);
        array.addShort((short) 5);
        assertEquals(array.length() - 1, sub.length());
        try {
            sub.iterator();
            fail();
        } catch (ConcurrentModificationException ignore) {
        }
    }

    @Test
    public void testAddSubArrayConcurrentModification2() throws Exception {
        UnboundedShortArray sub = array.length(array.length());
        array.addShort((short) 5);
        assertEquals(array.length() - 1, sub.length());
        try {
            sub.iterator();
            fail();
        } catch (ConcurrentModificationException ignore) {
        }
    }

    @Test
    public void testAddSubArrayConcurrentModification3() throws Exception {
        UnboundedShortArray sub = array.subArray(0, array.length());
        array.addShort((short) 5);
        assertEquals(array.length() - 1, sub.length());
        try {
            sub.iterator();
            fail();
        } catch (ConcurrentModificationException ignore) {
        }
    }

    @Test
    public void testAddSubArray() throws Exception {
        UnboundedShortArray sub = array.length(4);
        sub.addShort((short) 5);
        assertArrayEquals(new short[]{1, 1, 2, 1, 5}, sub.toArray());
        assertArrayEquals(new short[]{1, 1, 2, 1, 5, 2, 3, 3, 4}, array.toArray());
        sub.addShort(1, (short) 5);
        assertArrayEquals(new short[]{1, 5, 1, 2, 1, 5}, sub.toArray());
        assertArrayEquals(new short[]{1, 5, 1, 2, 1, 5, 2, 3, 3, 4}, array.toArray());
    }

    @Test
    public void testAddAllSubArray() throws Exception {
        UnboundedShortArray sub = array.length(4);
        sub.addAll(ShortArray.copyOf((short) 5, (short) 6));
        assertArrayEquals(new short[]{1, 1, 2, 1, 5, 6}, sub.toArray());
        assertArrayEquals(new short[]{1, 1, 2, 1, 5, 6, 2, 3, 3, 4}, array.toArray());
        sub.addAll(1, ShortArray.copyOf((short) 5, (short) 6));
        assertArrayEquals(new short[]{1, 5, 6, 1, 2, 1, 5, 6}, sub.toArray());
        assertArrayEquals(new short[]{1, 5, 6, 1, 2, 1, 5, 6, 2, 3, 3, 4}, array.toArray());
    }

    @Test
    public void testAddAllObject() throws Exception {
        array.addAll(ObjectArray.unsafeValueOf(new Short[]{9, 9}));
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3, 4, 9, 9}, array.toArray());
        try {
            array.addAll(ObjectArray.unsafeValueOf(new Short[]{null, 10}));
            fail();
        } catch (NullPointerException e) {
        }
    }

    @Test
    public void testTypeRemove() throws Exception {
        try {
            array.shortRemove(-1);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray());
        array.shortRemove(0);
        assertArrayEquals(new short[]{1, 2, 1, 2, 3, 3, 4}, array.toArray());
        array.shortRemove(array.length() - 1);
        assertArrayEquals(new short[]{1, 2, 1, 2, 3, 3}, array.toArray());
        try {
            array.shortRemove(array.length());
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.offset(1).shortRemove(3);
        assertArrayEquals(new short[]{1, 2, 1, 2, 3}, array.toArray());
        array.offset(1).shortRemove(2);
        assertArrayEquals(new short[]{1, 2, 1, 3}, array.toArray());
        try {
            array.offset(2).shortRemove(2);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
    }

    @Test
    public void testCharacteristics() {
        assertFalse(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
        array.clear();
        assertArrayEquals(new short[]{}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        array.addShort((short) 1);
        array.addShort((short) 1);
        array.addShort((short) 1);
        array.addShort((short) 2);
        array.addShort((short) 2);
        array.addShort((short) 3);
        array.addShort((short) 3);
        array.addShort((short) 4);
        assertArrayEquals(new short[]{1, 1, 1, 2, 2, 3, 3, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsAdd() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 1);
        a.addShort((short) 2);
        a.addShort((short) 3);
        array.addAll(1, a);
        assertArrayEquals(new short[]{0, 1, 2, 3, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        UnboundedShortArray b = UnboundedShortArray.newInstance();
        b.addShort((short) 4);
        b.addShort((short) 5);
        b.addShort((short) 6);
        array.addAll(b);
        assertArrayEquals(new short[]{0, 1, 2, 3, 4, 4, 5, 6}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArrayAdd1() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.offset(1);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 2);
        a.addShort((short) 3);
        a.addShort((short) 4);
        sub.addAll(0, a);
        assertArrayEquals(new short[]{2, 3, 4, 4}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertFalse(sub.hasCharacteristics(DISTINCT));
        assertFalse(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 2, 3, 4, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArrayAdd2() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.offset(1);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 0);
        a.addShort((short) 1);
        a.addShort((short) 2);
        sub.addAll(0, a);
        assertArrayEquals(new short[]{0, 1, 2, 4}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 0, 1, 2, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArrayAdd3() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.length(1);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 2);
        a.addShort((short) 3);
        a.addShort((short) 4);
        sub.addAll(a);
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArrayAdd4() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.length(1);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 0);
        a.addShort((short) 1);
        a.addShort((short) 2);
        sub.addAll(a);
        assertArrayEquals(new short[]{0, 0, 1, 2}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertFalse(sub.hasCharacteristics(DISTINCT));
        assertFalse(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 0, 1, 2, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArrayAdd5() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.length(1);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 1);
        a.addShort((short) 2);
        a.addShort((short) 3);
        sub.addAll(a);
        assertArrayEquals(new short[]{0, 1, 2, 3}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 1, 2, 3, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
    }
                
    @Test
    public void testCharacteristicsSubArrayAdd6() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.offset(1);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 0);
        a.addShort((short) 1);
        a.addShort((short) 2);
        sub.addAll(a);
        assertArrayEquals(new short[]{4, 0, 1, 2}, sub.toArray());
        assertFalse(sub.hasCharacteristics(SORTED));
        // Array not sorted anymore, cannot asses distinct status.
        assertFalse(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 4, 0, 1, 2}, array.toArray());
        assertFalse(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }
   
    @Test
    public void testCharacteristicsSet() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 4);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 1);
        a.addShort((short) 2);
        a.addShort((short) 3);
        array.setAll(a);
        assertArrayEquals(new short[]{1, 2, 3}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        UnboundedShortArray b = UnboundedShortArray.newInstance();
        b.addShort((short) 4);
        b.addShort((short) 5);
        b.addShort((short) 6);
        array.setAll(b);
        assertArrayEquals(new short[]{4, 5, 6}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArraySet1() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 2);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.subArray(1, 2);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 0);
        a.addShort((short) 1);
        a.addShort((short) 2);
        sub.setAll(a);
        assertArrayEquals(new short[]{0, 1, 2}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 0, 1, 2, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArraySet2() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 2);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.subArray(1, 2);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 2);
        a.addShort((short) 3);
        a.addShort((short) 4);
        sub.setAll(a);
        assertArrayEquals(new short[]{2, 3, 4}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 2, 3, 4, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArraySet3() {
        array.clear();
        array.addShort((short) 0);
        array.addShort((short) 2);
        array.addShort((short) 4);
        UnboundedShortArray sub = array.subArray(1, 2);
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 1);
        a.addShort((short) 2);
        a.addShort((short) 3);
        sub.setAll(a);
        assertArrayEquals(new short[]{1, 2, 3}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new short[]{0, 1, 2, 3, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
    }
   
    @Test
    public void testDistinct() {
        array.distinct();
        assertArrayEquals(new short[]{1, 2, 3, 4}, array.toArray());
    }

    @Test
    public void testDistinctSubArray() {
        array.subArray(4, 7).distinct();
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 4}, array.toArray());
    }

    @Test
    public void testClear() throws Exception {
        UnboundedShortArray array2 = array.subArray(2, 5);
        array2.clear();
        assertArrayEquals(new short[]{}, array2.toArray());
        assertEquals(0, array2.length());
        assertEquals(2 + 1, array2.offset());
        assertArrayEquals(new short[]{1, 1, 3, 3, 4}, array.toArray());
    }

    @Test
    public void testClearConcurrentModification() throws Exception {
        UnboundedShortArray array2 = array.subArray(2, 5).offset(1);
        array.clear();
        try {
            array2.toArray();
            fail();
        } catch (ConcurrentModificationException e) {
        }
    }

    @Test
    public void testSetAllReservedElements() throws Exception {
        array.setClearReservedElements(true);
        int offset = array.offset();
        array.setAll(ShortArray.unsafeValueOf());
        assertArrayEquals(new short[]{}, array.toArray());
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        short[] a = array.array();
        for (int i = array.offset(); i < a.length; i++) {
            assertEquals(0, a[i]);
        }
    }

    @Test
    public void testRemoveElements() throws Exception {
        array.setClearReservedElements(true);
        int offset = array.offset();
        while (!array.isEmpty()) {
            array.shortRemove();
        }
        assertArrayEquals(new short[]{}, array.toArray());
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        short[] a = array.array();
        for (int i = array.offset(); i < a.length; i++) {
            assertEquals(0, a[i]);
        }
    }

    @Test
    public void testClearReservedElements() throws Exception {
        array.setClearReservedElements(true);
        int offset = array.offset();
        array.clear();
        assertArrayEquals(new short[]{}, array.toArray());
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        short[] a = array.array();
        for (int i = array.offset(); i < a.length; i++) {
            assertEquals(0, a[i]);
        }
    }

    @Test
    public void testOffset() throws Exception {
        try {
            array.offset(-1);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        //noinspection PointlessArithmeticExpression
        assertEquals(0 + 1, array.offset(0).offset());
        assertEquals(1 + 1, array.offset(1).offset());
        assertEquals(2 + 1, array.offset(2).offset());
        assertEquals(3 + 1, array.offset(3).offset());
        assertEquals(4 + 1, array.offset(4).offset());
        assertEquals(5 + 1, array.offset(5).offset());
        assertEquals(6 + 1, array.offset(6).offset());
        assertEquals(7 + 1, array.offset(7).offset());
        assertEquals(8 + 1, array.offset(8).offset());
        assertEquals(8, array.offset(0).length());
        assertEquals(7, array.offset(1).length());
        assertEquals(6, array.offset(2).length());
        assertEquals(5, array.offset(3).length());
        assertEquals(4, array.offset(4).length());
        assertEquals(3, array.offset(5).length());
        assertEquals(2, array.offset(6).length());
        assertEquals(1, array.offset(7).length());
        assertEquals(0, array.offset(8).length());
        try {
            array.offset(9);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }

        UnboundedShortArray array2 = array.subArray(2, array.length());
        assertEquals(2 + 1, array2.offset());
        assertArrayEquals(new short[]{2, 1, 2, 3, 3, 4}, array2.toArray());

        UnboundedShortArray array3 = array.offset(8);
        array3.addShort((short) 5);
        assertEquals(9, array.length());
        assertArrayEquals(new short[]{1, 1, 2, 1, 2, 3, 3, 4, 5}, array.toArray());
        assertEquals(1, array3.length());
        assertArrayEquals(new short[]{5}, array3.toArray());
    }

    @Test
    public void testLengthRemove() throws Exception {
        array.setClearReservedElements(true);
        UnboundedShortArray array2 = array.length(4);
        array2.shortRemove(0);
        assertArrayEquals(new short[]{1, 2, 1, 2, 3, 3, 4}, array.toArray());
    }

    @Test
    public void testConcurrentModificationSubArray() throws Exception {
        UnboundedShortArray subArray = array.offset(2);
        assertEquals(2, subArray.getShort(0));
        array.shortRemove(0);
        try {
            subArray.getShort(0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        try {
            subArray.addShort((short) 0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        try {
            subArray.setShort(0, (short) 0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        UnboundedShortArray subArray2 = array.offset(2);
        subArray2.addShort(0, (short) 5);
        assertArrayEquals(new short[]{5, 1, 2, 3, 3, 4}, subArray2.toArray());
        assertArrayEquals(new short[]{1, 2, 5, 1, 2, 3, 3, 4}, array.toArray());
    }

    @Test
    public void testIterator() {
        List<Short> list = new ArrayList<>();
        //noinspection ForLoopReplaceableByForEach
        for (Iterator<Short> it = array.iterator(); it.hasNext();) {
            list.add(it.next());
        }
        assertArrayEquals(new Short[]{1, 1, 2, 1, 2, 3, 3, 4}, list.toArray());
    }

    @Test
    public void testSubArrayIterator() {
        List<Short> list = new ArrayList<>();
        //noinspection ForLoopReplaceableByForEach
        for (Iterator<Short> it = array.subArray(2, 5).iterator(); it.hasNext();) {
            list.add(it.next());
        }
        assertArrayEquals(new Short[]{2, 1, 2}, list.toArray());
    }

    @Test
    public void testIteratorRemove() throws Exception {
        for (Iterator<Short> i = array.iterator(); i.hasNext();) {
            i.next();
            i.remove();
        }
        assertEquals(0, array.length());
    }

    @Test
    public void testArrayIterator() {
        List<Short> list = new ArrayList<>();
        //noinspection ForLoopReplaceableByForEach
        for (Iterator<Short> it = array.iterator(1); it.hasNext();) {
            list.add(it.next());
        }
        assertArrayEquals(new Short[]{1, 2, 1, 2, 3, 3, 4}, list.toArray());
    }

    @Test
    public void testSubArrayArrayIterator() {
        List<Short> list = new ArrayList<>();
        //noinspection ForLoopReplaceableByForEach
        for (Iterator<Short> it = array.subArray(2, 5).iterator(1); it.hasNext();) {
            list.add(it.next());
        }
        assertArrayEquals(new Short[]{1, 2}, list.toArray());
    }

    @Test
    public void testArrayIteratorRemove() throws Exception {
        for (Iterator<Short> i = array.iterator(1); i.hasNext();) {
            i.next();
            i.remove();
        }
        assertEquals(1, array.length());
    }

    @Test
    public void testConcurrentModificationIterator() throws Exception {
        Iterator<Short> i1 = array.iterator();
        i1.next();
        array.shortRemove(0);

        Iterator<Short> i2 = array.iterator();
        array.shortRemove(0);
        try {
            i2.next();
            fail();
        } catch (ConcurrentModificationException e) {
        }

        Iterator<Short> i3 = array.iterator();
        i3.next();
        array.shortRemove(0);
        try {
            i3.remove();    // Remove doesn't count, since it should fail.
            fail();
        } catch (ConcurrentModificationException e) {
        }

        Iterator<Short> i4 = array.iterator();
        i4.next();
        i4.remove();

        Iterator<Short> i5 = array.iterator();
        i5.next();
        i5.remove();
        i5.next();

        Iterator<Short> i6 = array.iterator();
        array.addShort((short) 0);
        try {
            i6.next();
            fail();
        } catch (ConcurrentModificationException e) {
        }

        assertEquals(4, array.length());
    }

    private static UnboundedShortArray serializeAndDeserialize(UnboundedShortArray array) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (ObjectOutputStream os = new ObjectOutputStream(bos)) {
            os.writeObject(array);
        }
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        try (ObjectInputStream is = new ObjectInputStream(bis)) {
            return (UnboundedShortArray) is.readObject();
        }
    }
}
TOP

Related Classes of org.squarebrackets.UnboundedShortArrayTest

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.