Package org.squarebrackets

Source Code of org.squarebrackets.UnboundedLongArrayTest

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.LongBuffer;
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 UnboundedLongArrayTest {

    private UnboundedLongArray array;

    @Before
    public void setUp() throws Exception {
        array = UnboundedLongArray.newInstance();
        array.addLong(0);
        array.addLong(1);
        array.addLong(1);
        array.addLong(2);
        array.addLong(1);
        array.addLong(2);
        array.addLong(3);
        array.addLong(3);
        array.addLong(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 {
        UnboundedLongArray d = UnboundedLongArray.copyOf(array);
        d.setLong(0, 0);
        assertArrayEquals(new long[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray());
        assertArrayEquals(new long[]{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 {
        UnboundedLongArray smaller = UnboundedLongArray.copyOf(array);
        smaller.longRemove();
        array.setAll(smaller);
        assertArrayEquals(new long[]{1, 1, 2, 1, 2, 3, 3}, array.toArray());
        array.addLong(4);
        MutableLongArray backedByArray = MutableLongArray.copyOf(array);
        Arrays.reverse(backedByArray);
        array.setAll(backedByArray);
        assertArrayEquals(new long[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray());
        LongBuffer buffer = ByteBuffer.allocateDirect(Long.SIZE / Byte.SIZE * 8).asLongBuffer();
        MutableLongArray notBackedByArray = Arrays.newMutableArray(buffer);
        notBackedByArray.setAll(backedByArray);
        array.setAll(notBackedByArray);
        assertArrayEquals(new long[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray());
        UnboundedLongArray bigger = UnboundedLongArray.copyOf(array);
        bigger.addLong((long) 0);
        array.setAll(bigger);
        assertArrayEquals(new long[]{4, 3, 3, 2, 1, 2, 1, 1, 0}, array.toArray());
        bigger.addLong((long) 0);
        array.setAll(bigger);
        assertArrayEquals(new long[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0}, array.toArray());
        bigger.addLong((long) 0);
        array.setAll(bigger);
        assertArrayEquals(new long[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0, 0}, array.toArray());
    }

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

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

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

    @Test
    public void testContainsAll() throws Exception {
        MutableLongArray testTrue = MutableLongArray.valueOf(1, 2, 3);
        MutableLongArray testFalse = MutableLongArray.valueOf(3, 4, 5);
        assertTrue(array.containsAll(testTrue));
        assertFalse(array.length(5).containsAll(testTrue));
        assertFalse(array.containsAll(testFalse));
        UnboundedObjectArray<Long> objectTestTrue = UnboundedObjectArray.valueOf(1L, 2L, 3L);
        assertTrue(array.containsAll(objectTestTrue));
        UnboundedObjectArray<Long> objectTestFalse = UnboundedObjectArray.valueOf(3L, 4L, 5L);
        assertFalse(array.containsAll(objectTestFalse));
        UnboundedObjectArray<Integer> objectTestOther = UnboundedObjectArray.valueOf(1, 2, 3);
        assertFalse(array.containsAll(objectTestOther));
    }

    @Test
    public void testContainsAllSorted() throws Exception {
        array.sort();
        MutableLongArray testTrue = MutableLongArray.valueOf(1, 2, 3);
        testTrue.sort();
        MutableLongArray testFalse = MutableLongArray.valueOf(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.indexOfLong(1));
        assertEquals(7, array.indexOfLong(4));
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @Test
    public void testRemoveAllDistinct4() throws Exception {
        array.distinct();
        MutableLongArray a = MutableLongArray.valueOf(3, 4, 5, 3, 4, 5, 3, 4, 5);
        a.sort();
        assertTrue(array.removeAll(a));
        assertArrayEquals(new long[]{1, 2}, array.toArray());
    }
   
    @Test
    public void testRemoveAllObject1() throws Exception {
        MutableObjectArray<Long> a = MutableObjectArray.valueOf(1L, 2L, 3L);
        assertTrue(array.removeAll(a));
        assertArrayEquals(new long[]{4}, array.toArray());
    }

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

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

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

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

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

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

    @Test
    public void testAdd() throws Exception {
        try {
            array.addLong(-1, 5);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.addLong(5);
        assertArrayEquals(new long[]{1, 1, 2, 1, 2, 3, 3, 4, 5}, array.toArray());
        array.addLong(2, 6);
        assertArrayEquals(new long[]{1, 1, 6, 2, 1, 2, 3, 3, 4, 5}, array.toArray());
        array.addLong(0, 7);
        assertArrayEquals(new long[]{7, 1, 1, 6, 2, 1, 2, 3, 3, 4, 5}, array.toArray());
        array.addLong(array.length(), 8);
        assertArrayEquals(new long[]{7, 1, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8}, array.toArray());
        try {
            array.addLong(array.length() + 1, 9);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.addAll(LongArray.unsafeValueOf(9, 9));
        assertArrayEquals(new long[]{7, 1, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8, 9, 9}, array.toArray());
        try {
            array.addAll(-1, LongArray.unsafeValueOf(10, 10));
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.addAll(2, LongArray.unsafeValueOf(10, 10));
        assertArrayEquals(new long[]{7, 1, 10, 10, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8, 9, 9}, array.toArray());
        array.addAll(0, LongArray.unsafeValueOf(11, 11));
        assertArrayEquals(new long[]{11, 11, 7, 1, 10, 10, 1, 6, 2, 1, 2, 3, 3, 4, 5, 8, 9, 9}, array.toArray());
        array.addAll(array.length(), LongArray.unsafeValueOf(12, 12));
        assertArrayEquals(new long[]{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, LongArray.unsafeValueOf(12, 12));
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
    }

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

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

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

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

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

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

    @Test
    public void testTypeRemove() throws Exception {
        try {
            array.longRemove(-1);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        assertArrayEquals(new long[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray());
        array.longRemove(0);
        assertArrayEquals(new long[]{1, 2, 1, 2, 3, 3, 4}, array.toArray());
        array.longRemove(array.length() - 1);
        assertArrayEquals(new long[]{1, 2, 1, 2, 3, 3}, array.toArray());
        try {
            array.longRemove(array.length());
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.offset(1).longRemove(3);
        assertArrayEquals(new long[]{1, 2, 1, 2, 3}, array.toArray());
        array.offset(1).longRemove(2);
        assertArrayEquals(new long[]{1, 2, 1, 3}, array.toArray());
        try {
            array.offset(2).longRemove(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 long[]{}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        array.addLong(1);
        array.addLong(1);
        array.addLong(1);
        array.addLong(2);
        array.addLong(2);
        array.addLong(3);
        array.addLong(3);
        array.addLong(4);
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(4);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(1);
        a.addLong(2);
        a.addLong(3);
        array.addAll(1, a);
        assertArrayEquals(new long[]{0, 1, 2, 3, 4}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        UnboundedLongArray b = UnboundedLongArray.newInstance();
        b.addLong(4);
        b.addLong(5);
        b.addLong(6);
        array.addAll(b);
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(4);
        UnboundedLongArray sub = array.offset(1);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(2);
        a.addLong(3);
        a.addLong(4);
        sub.addAll(0, a);
        assertArrayEquals(new long[]{2, 3, 4, 4}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertFalse(sub.hasCharacteristics(DISTINCT));
        assertFalse(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(4);
        UnboundedLongArray sub = array.offset(1);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(0);
        a.addLong(1);
        a.addLong(2);
        sub.addAll(0, a);
        assertArrayEquals(new long[]{0, 1, 2, 4}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(4);
        UnboundedLongArray sub = array.length(1);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(2);
        a.addLong(3);
        a.addLong(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.addLong(0);
        array.addLong(4);
        UnboundedLongArray sub = array.length(1);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(0);
        a.addLong(1);
        a.addLong(2);
        sub.addAll(a);
        assertArrayEquals(new long[]{0, 0, 1, 2}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertFalse(sub.hasCharacteristics(DISTINCT));
        assertFalse(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(4);
        UnboundedLongArray sub = array.length(1);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(1);
        a.addLong(2);
        a.addLong(3);
        sub.addAll(a);
        assertArrayEquals(new long[]{0, 1, 2, 3}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new long[]{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.addLong((long) 0);
        array.addLong((long) 4);
        UnboundedLongArray sub = array.offset(1);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong((long) 0);
        a.addLong((long) 1);
        a.addLong((long) 2);
        sub.addAll(a);
        assertArrayEquals(new long[]{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 long[]{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.addLong(0);
        array.addLong(4);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(1);
        a.addLong(2);
        a.addLong(3);
        array.setAll(a);
        assertArrayEquals(new long[]{1, 2, 3}, array.toArray());
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        UnboundedLongArray b = UnboundedLongArray.newInstance();
        b.addLong(4);
        b.addLong(5);
        b.addLong(6);
        array.setAll(b);
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(2);
        array.addLong(4);
        UnboundedLongArray sub = array.subArray(1, 2);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(0);
        a.addLong(1);
        a.addLong(2);
        sub.setAll(a);
        assertArrayEquals(new long[]{0, 1, 2}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(2);
        array.addLong(4);
        UnboundedLongArray sub = array.subArray(1, 2);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(2);
        a.addLong(3);
        a.addLong(4);
        sub.setAll(a);
        assertArrayEquals(new long[]{2, 3, 4}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new long[]{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.addLong(0);
        array.addLong(2);
        array.addLong(4);
        UnboundedLongArray sub = array.subArray(1, 2);
        UnboundedLongArray a = UnboundedLongArray.newInstance();
        a.addLong(1);
        a.addLong(2);
        a.addLong(3);
        sub.setAll(a);
        assertArrayEquals(new long[]{1, 2, 3}, sub.toArray());
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new long[]{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 long[]{1, 2, 3, 4}, array.toArray());
    }

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

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

    @Test
    public void testClearConcurrentModification() throws Exception {
        UnboundedLongArray 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(LongArray.unsafeValueOf());
        assertArrayEquals(new long[]{}, array.toArray());
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        long[] 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.longRemove();
        }
        assertArrayEquals(new long[]{}, array.toArray());
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        long[] 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 long[]{}, array.toArray());
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        long[] 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) {
        }

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

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

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

    @Test
    public void testConcurrentModificationSubArray() throws Exception {
        UnboundedLongArray subArray = array.offset(2);
        assertEquals(2, subArray.getLong(0));
        array.longRemove(0);
        try {
            subArray.getLong(0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        try {
            subArray.addLong(0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        try {
            subArray.setLong(0, 0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        UnboundedLongArray subArray2 = array.offset(2);
        subArray2.addLong(0, 5);
        assertArrayEquals(new long[]{5, 1, 2, 3, 3, 4}, subArray2.toArray());
        assertArrayEquals(new long[]{1, 2, 5, 1, 2, 3, 3, 4}, array.toArray());
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    private static UnboundedLongArray serializeAndDeserialize(UnboundedLongArray 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 (UnboundedLongArray) is.readObject();
        }
    }
}
TOP

Related Classes of org.squarebrackets.UnboundedLongArrayTest

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.