Package org.squarebrackets

Source Code of org.squarebrackets.UnboundedDoubleArrayTest

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.DoubleBuffer;
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 UnboundedDoubleArrayTest {

    private UnboundedDoubleArray array;

    @Before
    public void setUp() throws Exception {
        array = UnboundedDoubleArray.newInstance();
        array.addDouble(0);
        array.addDouble(1);
        array.addDouble(1);
        array.addDouble(2);
        array.addDouble(1);
        array.addDouble(2);
        array.addDouble(3);
        array.addDouble(3);
        array.addDouble(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 {
        UnboundedDoubleArray d = UnboundedDoubleArray.copyOf(array);
        d.setDouble(0, 0);
        assertArrayEquals(new double[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray(), 0);
        assertArrayEquals(new double[]{0, 1, 2, 1, 2, 3, 3, 4}, d.toArray(), 0);
    }

    @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 {
        UnboundedDoubleArray smaller = UnboundedDoubleArray.copyOf(array);
        smaller.doubleRemove();
        array.setAll(smaller);
        assertArrayEquals(new double[]{1, 1, 2, 1, 2, 3, 3}, array.toArray(), 0);
        array.addDouble(4);
        MutableDoubleArray backedByArray = MutableDoubleArray.copyOf(array);
        Arrays.reverse(backedByArray);
        array.setAll(backedByArray);
        assertArrayEquals(new double[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray(), 0);
        DoubleBuffer buffer = ByteBuffer.allocateDirect(Double.SIZE / Byte.SIZE * 8).asDoubleBuffer();
        MutableDoubleArray notBackedByArray = Arrays.newMutableArray(buffer);
        notBackedByArray.setAll(backedByArray);
        array.setAll(notBackedByArray);
        assertArrayEquals(new double[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray(), 0);
        UnboundedDoubleArray bigger = UnboundedDoubleArray.copyOf(array);
        bigger.addDouble((double) 0);
        array.setAll(bigger);
        assertArrayEquals(new double[]{4, 3, 3, 2, 1, 2, 1, 1, 0}, array.toArray(), 0);
        bigger.addDouble((double) 0);
        array.setAll(bigger);
        assertArrayEquals(new double[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0}, array.toArray(), 0);
        bigger.addDouble((double) 0);
        array.setAll(bigger);
        assertArrayEquals(new double[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0, 0}, array.toArray(), 0);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @Test
    public void testTypeRemove() throws Exception {
        try {
            array.doubleRemove(-1);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        assertArrayEquals(new double[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray(), 0);
        array.doubleRemove(0);
        assertArrayEquals(new double[]{1, 2, 1, 2, 3, 3, 4}, array.toArray(), 0);
        array.doubleRemove(array.length() - 1);
        assertArrayEquals(new double[]{1, 2, 1, 2, 3, 3}, array.toArray(), 0);
        try {
            array.doubleRemove(array.length());
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        array.offset(1).doubleRemove(3);
        assertArrayEquals(new double[]{1, 2, 1, 2, 3}, array.toArray(), 0);
        array.offset(1).doubleRemove(2);
        assertArrayEquals(new double[]{1, 2, 1, 3}, array.toArray(), 0);
        try {
            array.offset(2).doubleRemove(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 double[]{}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        array.addDouble(1);
        array.addDouble(1);
        array.addDouble(1);
        array.addDouble(2);
        array.addDouble(2);
        array.addDouble(3);
        array.addDouble(3);
        array.addDouble(4);
        assertArrayEquals(new double[]{1, 1, 1, 2, 2, 3, 3, 4}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

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

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

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

    @Test
    public void testCharacteristicsSubArrayAdd3() {
        array.clear();
        array.addDouble(0);
        array.addDouble(4);
        UnboundedDoubleArray sub = array.length(1);
        UnboundedDoubleArray a = UnboundedDoubleArray.newInstance();
        a.addDouble(2);
        a.addDouble(3);
        a.addDouble(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.addDouble(0);
        array.addDouble(4);
        UnboundedDoubleArray sub = array.length(1);
        UnboundedDoubleArray a = UnboundedDoubleArray.newInstance();
        a.addDouble(0);
        a.addDouble(1);
        a.addDouble(2);
        sub.addAll(a);
        assertArrayEquals(new double[]{0, 0, 1, 2}, sub.toArray(), 0);
        assertTrue(sub.hasCharacteristics(SORTED));
        assertFalse(sub.hasCharacteristics(DISTINCT));
        assertFalse(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new double[]{0, 0, 1, 2, 4}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

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

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

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

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

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

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

    @Test
    public void testClearConcurrentModification() throws Exception {
        UnboundedDoubleArray 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(DoubleArray.unsafeValueOf());
        assertArrayEquals(new double[]{}, array.toArray(), 0);
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        double[] a = array.array();
        for (int i = array.offset(); i < a.length; i++) {
            assertEquals(0, a[i], 0);
        }
    }

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

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

    @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) {
        }

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

        UnboundedDoubleArray array3 = array.offset(8);
        array3.addDouble(5);
        assertEquals(9, array.length());
        assertArrayEquals(new double[]{1, 1, 2, 1, 2, 3, 3, 4, 5}, array.toArray(), 0);
        assertEquals(1, array3.length());
        assertArrayEquals(new double[]{5}, array3.toArray(), 0);
    }

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

    @Test
    public void testConcurrentModificationSubArray() throws Exception {
        UnboundedDoubleArray subArray = array.offset(2);
        assertEquals(2, subArray.getDouble(0), 0);
        array.doubleRemove(0);
        try {
            subArray.getDouble(0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        try {
            subArray.addDouble(0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        try {
            subArray.setDouble(0, 0);
            fail();
        } catch (ConcurrentModificationException e) {
        }
        UnboundedDoubleArray subArray2 = array.offset(2);
        subArray2.addDouble(0, 5);
        assertArrayEquals(new double[]{5, 1, 2, 3, 3, 4}, subArray2.toArray(), 0);
        assertArrayEquals(new double[]{1, 2, 5, 1, 2, 3, 3, 4}, array.toArray(), 0);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Related Classes of org.squarebrackets.UnboundedDoubleArrayTest

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.