Package org.squarebrackets

Source Code of org.squarebrackets.BoundedFloatBufferArrayTest

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.FloatBuffer;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

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 BoundedFloatBufferArrayTest {

    private BoundedFloatArray array;

    @Before
    public void setUp() throws Exception {
        float[] a = new float[] {0, 1, 1, 2, 1, 2, 3, 3, 4, 0, 0};
        FloatBuffer buffer = FloatBuffer.wrap(a, 1, 8);
        array = BoundedFloatArray.valueOf(buffer);
        array = serializeAndDeserialize(array); // Test serialization.
        float[] b = new float[11];
        array.toArray(b, 1, 8);
        buffer = FloatBuffer.wrap(b, 1, 8);
        array = BoundedFloatArray.valueOf(buffer);
        array = Arrays.synchronizedArray(array, array);    // Test synchronized array.
    }

    @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() {
        assertFalse(array.hasCharacteristics(ArrayCharacteristics.REPLACEABLE));
    }

    @Test
    public void copyFactory() throws Exception {
        UnboundedFloatArray d = UnboundedFloatArray.copyOf(array);
        d.setFloat(0, (float) 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray(), 0);
        assertArrayEquals(new float[]{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(2, array.remainingCapacity());
    }

    @Test
    public void testSetAll() throws Exception {
        UnboundedFloatArray smaller = UnboundedFloatArray.copyOf(array);
        smaller.floatRemove();
        array.setAll(smaller);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3}, array.toArray(), 0);
        array.addFloat((float) 4);
        MutableFloatArray backedByArray = MutableFloatArray.copyOf(array);
        Arrays.reverse(backedByArray);
        array.setAll(backedByArray);
        assertArrayEquals(new float[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray(), 0);
        FloatBuffer buffer = ByteBuffer.allocateDirect(Float.SIZE / Byte.SIZE * 8).asFloatBuffer();
        MutableFloatArray notBackedByArray = Arrays.newMutableArray(buffer);
        notBackedByArray.setAll(backedByArray);
        array.setAll(notBackedByArray);
        assertArrayEquals(new float[]{4, 3, 3, 2, 1, 2, 1, 1}, array.toArray(), 0);
        UnboundedFloatArray bigger = UnboundedFloatArray.copyOf(array);
        bigger.addFloat((float) 0);
        array.setAll(bigger);
        assertArrayEquals(new float[]{4, 3, 3, 2, 1, 2, 1, 1, 0}, array.toArray(), 0);
        bigger.addFloat((float) 0);
        array.setAll(bigger);
        assertArrayEquals(new float[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0}, array.toArray(), 0);
        bigger.addFloat((float) 0);
        try {
            array.setAll(bigger);
            fail();
        } catch (IllegalStateException ignore) {
        }
        assertArrayEquals(new float[]{4, 3, 3, 2, 1, 2, 1, 1, 0, 0}, array.toArray(), 0);
    }

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

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

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

    @Test
    public void testContainsAll() throws Exception {
        MutableFloatArray testTrue = MutableFloatArray.valueOf(1, 2, 3);
        MutableFloatArray testFalse = MutableFloatArray.valueOf(3, 4, 5);
        assertTrue(array.containsAll(testTrue));
        assertFalse(array.length(5).containsAll(testTrue));
        assertFalse(array.containsAll(testFalse));
        UnboundedObjectArray<Float> objectTestTrue = UnboundedObjectArray.valueOf((float) 1, (float) 2, (float) 3);
        assertTrue(array.containsAll(objectTestTrue));
        UnboundedObjectArray<Float> objectTestFalse = UnboundedObjectArray.valueOf((float) 3, (float) 4, (float) 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();
        MutableFloatArray testTrue = MutableFloatArray.valueOf(1, 2, 3);
        testTrue.sort();
        MutableFloatArray testFalse = MutableFloatArray.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.indexOfFloat((float) 1));
        assertEquals(7, array.indexOfFloat((float) 4));
    }

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

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

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

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

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

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

    @Test
    public void testLastIndexOfSortedSubArray() throws Exception {
        array.sort();
        assertEquals(-1, array.offset(1).lastIndexOf(null));
        assertEquals(1, array.offset(1).lastIndexOfFloat((float) 1));
        assertEquals(6, array.offset(1).lastIndexOfFloat((float) 4));
    }

    @Test
    public void testGet() throws Exception {
        try {
            array.getFloat(-1);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        assertEquals(1, array.getFloat(0), 0);
        assertEquals(1, array.getFloat(1), 0);
        assertEquals(2, array.getFloat(2), 0);
        assertEquals(1, array.getFloat(3), 0);
        assertEquals(2, array.getFloat(4), 0);
        assertEquals(3, array.getFloat(5), 0);
        assertEquals(3, array.getFloat(6), 0);
        assertEquals(4, array.getFloat(7), 0);
        try {
            assertEquals(1, array.getFloat(8), 0);
            fail();
        } catch (IndexOutOfBoundsException e) {
        }
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray(), 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(4 + 1, array.offset(4).offset());
        assertEquals(3 + 1, array.offset(3).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) {
        }
        MutableFloatArray array2 = array.subArray(2, array.length());
        assertEquals(2 + 1, array2.offset());
        assertArrayEquals(new float[]{2, 1, 2, 3, 3, 4}, array2.toArray(), 0);
    }

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

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

    @Test
    public void testIteratorRemove() throws Exception {
        int length = array.length();
        try {
            for (Iterator<Float> i = array.iterator(); i.hasNext();) {
                i.next();
                i.remove();
            }
            fail();
        } catch (UnsupportedOperationException e) {
        }
        assertEquals(length, array.length());
    }

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

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

    @Test
    public void testArrayIteratorRemove() throws Exception {
        int length = array.length();
        try {
            for (Iterator<Float> i = array.iterator(1); i.hasNext();) {
                i.next();
                i.remove();
            }
            fail();
        } catch (UnsupportedOperationException e) {
        }
        assertEquals(length, array.length());
    }

    @Test
    public void testAdd() throws Exception {
        array.addFloat((float) 5);
        assertEquals(9, array.length());
        array.addFloat((float) 5);
        assertEquals(10, array.length());
        try {
            array.addFloat((float) 5);
            fail();
        } catch (IllegalStateException ignore) {
        }
        assertEquals(10, array.length());
    }

    @Test
    public void testAddAll0() throws Exception {
        array.addAll(FloatArray.unsafeValueOf());
        assertEquals(8, array.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray(), 0);
    }

    @Test
    public void testAddAll1() throws Exception {
        array.addAll(FloatArray.unsafeValueOf((float) 5));
        assertEquals(9, array.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4, 5}, array.toArray(), 0);
    }

    @Test
    public void testAddAll2() throws Exception {
        array.addAll(FloatArray.unsafeValueOf((float) 5, (float) 5));
        assertEquals(10, array.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4, 5, 5}, array.toArray(), 0);
    }

    @Test
    public void testAddAll3() throws Exception {
        try {
            array.addAll(FloatArray.unsafeValueOf((float) 5, (float) 5, (float) 5));
            fail();
        } catch (IllegalStateException ignore) {
        }
        assertEquals(8, array.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4}, array.toArray(), 0);
    }

    @Test
    public void testAddAllNoBackingArray0() throws Exception {
        FloatBuffer buffer = ByteBuffer.allocateDirect(Float.SIZE / Byte.SIZE * 10).asFloatBuffer();
        buffer.position(1).limit(9);
        BoundedFloatArray notBackedByArray = BoundedFloatArray.valueOf(buffer);
        notBackedByArray.setAll(array);
        notBackedByArray.addAll(FloatArray.unsafeValueOf());
        assertEquals(8, notBackedByArray.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4}, notBackedByArray.toArray(), 0);
    }

    @Test
    public void testAddAllNoBackingArray1() throws Exception {
        FloatBuffer buffer = ByteBuffer.allocateDirect(Float.SIZE / Byte.SIZE * 11).asFloatBuffer();
        buffer.position(1).limit(9);
        BoundedFloatArray notBackedByArray = BoundedFloatArray.valueOf(buffer);
        notBackedByArray.setAll(array);
        notBackedByArray.addAll(FloatArray.unsafeValueOf((float) 5));
        assertEquals(9, notBackedByArray.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4, 5}, notBackedByArray.toArray(), 0);
    }

    @Test
    public void testAddAllNoBackingArray2() throws Exception {
        FloatBuffer buffer = ByteBuffer.allocateDirect(Float.SIZE / Byte.SIZE * 11).asFloatBuffer();
        buffer.position(1).limit(9);
        BoundedFloatArray notBackedByArray = BoundedFloatArray.valueOf(buffer);
        notBackedByArray.setAll(array);
        notBackedByArray.addAll(FloatArray.unsafeValueOf((float) 5, (float) 5));
        assertEquals(10, notBackedByArray.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4, 5, 5}, notBackedByArray.toArray(), 0);
    }

    @Test
    public void testAddAllNoBackingArray3() throws Exception {
        FloatBuffer buffer = ByteBuffer.allocateDirect(Float.SIZE / Byte.SIZE * 11).asFloatBuffer();
        buffer.position(1).limit(9);
        BoundedFloatArray notBackedByArray = BoundedFloatArray.valueOf(buffer);
        notBackedByArray.setAll(array);
        try {
            notBackedByArray.addAll(FloatArray.unsafeValueOf((float) 5, (float) 5, (float) 5));
            fail();
        } catch (IllegalStateException ignore) {
        }
        assertEquals(8, notBackedByArray.length());
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4}, notBackedByArray.toArray(), 0);
    }
                            
    @Test
    public void testAddSubArrayConcurrentModification1() throws Exception {
        BoundedFloatArray sub = array.offset(0);
        array.addFloat((float) 5);
        assertEquals(array.length() - 1, sub.length());
        try {
            sub.iterator();
            fail();
        } catch (ConcurrentModificationException ignore) {
        }
    }

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

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

    @Test
    public void testAddSubArray() throws Exception {
        BoundedFloatArray sub = array.offset(1);
        sub.addFloat((float) 5);
        assertArrayEquals(new float[]{1, 2, 1, 2, 3, 3, 4, 5}, sub.toArray(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4, 5}, array.toArray(), 0);
    }

    @Test
    public void testAddAllSubArray() throws Exception {
        BoundedFloatArray sub = array.offset(1);
        sub.addAll(FloatArray.copyOf((float) 5, (float) 6));
        assertArrayEquals(new float[]{1, 2, 1, 2, 3, 3, 4, 5, 6}, sub.toArray(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3, 4, 5, 6}, array.toArray(), 0);
    }
   
    @Test
    public void testRemove() throws Exception {
        assertEquals(4, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3}, array.toArray(), 0);
        assertEquals(3, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3}, array.toArray(), 0);
        assertEquals(3, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2}, array.toArray(), 0);
        assertEquals(2, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2}, array.toArray(), 0);
        assertEquals(2, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        try {
            array.floatRemove();
            fail();
        } catch (NoSuchElementException e) {
        }
    }

    @Test
    public void testRemoveClearReservedElements() throws Exception {
        array.setClearReservedElements(true);
        assertEquals(4, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3, 3}, array.toArray(), 0);
        assertEquals(3, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2, 3}, array.toArray(), 0);
        assertEquals(3, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1, 2}, array.toArray(), 0);
        assertEquals(2, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2, 1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 2}, array.toArray(), 0);
        assertEquals(2, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        try {
            array.floatRemove();
            fail();
        } catch (NoSuchElementException e) {
        }
    }

    @Test
    public void testRemoveSort() throws Exception {
        array.sort();
        assertEquals(4, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 1, 2, 2, 3, 3}, array.toArray(), 0);
        assertEquals(3, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 1, 2, 2, 3}, array.toArray(), 0);
        assertEquals(3, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 1, 2, 2}, array.toArray(), 0);
        assertEquals(2, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 1, 2}, array.toArray(), 0);
        assertEquals(2, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1, 1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1, 1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{1}, array.toArray(), 0);
        assertEquals(1, array.floatRemove(), 0);
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        try {
            array.floatRemove();
            fail();
        } catch (NoSuchElementException e) {
        }
    }

    @Test
    public void testClear() throws Exception {
        int offset = array.offset();
        array.clear();
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
    }

    @Test
    public void testSetAllReservedElements() throws Exception {
        array.setClearReservedElements(true);
        int offset = array.offset();
        array.setAll(FloatArray.unsafeValueOf());
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        FloatBuffer buffer = array.buffer();
        buffer.rewind().limit(buffer.capacity());
        while (buffer.hasRemaining()) {
            assertEquals(0, buffer.get(), 0);
        }
    }

    @Test
    public void testRemoveElements() throws Exception {
        array.setClearReservedElements(true);
        int offset = array.offset();
        while (!array.isEmpty()) {
            array.floatRemove();
        }
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        FloatBuffer buffer = array.buffer();
        buffer.rewind().limit(buffer.capacity());
        while (buffer.hasRemaining()) {
            assertEquals(0, buffer.get(), 0);
        }
    }

    @Test
    public void testClearReservedElements() throws Exception {
        array.setClearReservedElements(true);
        int offset = array.offset();
        array.clear();
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        assertEquals(0, array.length());
        assertEquals(offset, array.offset());
        FloatBuffer buffer = array.buffer();
        buffer.rewind().limit(buffer.capacity());
        while (buffer.hasRemaining()) {
            assertEquals(0, buffer.get(), 0);
        }
    }

    @Test
    public void testCharacteristics() {
        assertFalse(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
        array.clear();
        assertArrayEquals(new float[]{}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        array.addFloat((float) 1);
        array.addFloat((float) 1);
        array.addFloat((float) 1);
        array.addFloat((float) 2);
        array.addFloat((float) 2);
        array.addFloat((float) 3);
        array.addFloat((float) 3);
        array.addFloat((float) 4);
        assertArrayEquals(new float[]{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.addFloat((float) 0);
        array.addFloat((float) 1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 2);
        a.addFloat((float) 3);
        a.addFloat((float) 4);
        array.addAll(a);
        assertArrayEquals(new float[]{0, 1, 2, 3, 4}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        UnboundedFloatArray b = UnboundedFloatArray.newInstance();
        b.addFloat((float) 4);
        b.addFloat((float) 5);
        b.addFloat((float) 6);
        array.addAll(b);
        assertArrayEquals(new float[]{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.addFloat((float) 0);
        array.addFloat((float) 1);
        BoundedFloatArray sub = array.offset(1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 2);
        a.addFloat((float) 3);
        a.addFloat((float) 4);
        sub.addAll(a);
        assertArrayEquals(new float[]{1, 2, 3, 4}, sub.toArray(), 0);
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new float[]{0, 1, 2, 3, 4}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
    }

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

    @Test
    public void testCharacteristicsSubArrayAdd3() {
        array.clear();
        array.addFloat((float) 0);
        array.addFloat((float) 1);
        BoundedFloatArray sub = array.offset(1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 2);
        a.addFloat((float) 3);
        a.addFloat((float) 3);
        sub.addAll(a);
        assertArrayEquals(new float[]{1, 2, 3, 3}, sub.toArray(), 0);
        assertTrue(sub.hasCharacteristics(SORTED));
        assertFalse(sub.hasCharacteristics(DISTINCT));
        assertFalse(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new float[]{0, 1, 2, 3, 3}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArrayAdd4() {
        array.clear();
        array.addFloat((float) 0);
        array.addFloat((float) 4);
        BoundedFloatArray sub = array.offset(1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 0);
        a.addFloat((float) 1);
        a.addFloat((float) 2);
        sub.addAll(a);
        assertArrayEquals(new float[]{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 float[]{0, 4, 0, 1, 2}, array.toArray(), 0);
        assertFalse(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArrayAdd5() {
        array.clear();
        array.addFloat((float) 4);
        array.addFloat((float) 5);
        BoundedFloatArray sub = array.offset(1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 0);
        a.addFloat((float) 1);
        a.addFloat((float) 2);
        sub.addAll(a);
        assertArrayEquals(new float[]{5, 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 float[]{4, 5, 0, 1, 2}, array.toArray(), 0);
        assertFalse(array.hasCharacteristics(SORTED));
        // Array not sorted anymore, cannot asses distinct status.
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSet() {
        array.clear();
        array.addFloat((float) 0);
        array.addFloat((float) 4);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 1);
        a.addFloat((float) 2);
        a.addFloat((float) 3);
        array.setAll(a);
        assertArrayEquals(new float[]{1, 2, 3}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
        UnboundedFloatArray b = UnboundedFloatArray.newInstance();
        b.addFloat((float) 4);
        b.addFloat((float) 5);
        b.addFloat((float) 6);
        array.setAll(b);
        assertArrayEquals(new float[]{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.addFloat((float) 0);
        array.addFloat((float) 2);
        array.addFloat((float) 4);
        BoundedFloatArray sub = array.offset(1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 1);
        a.addFloat((float) 2);
        a.addFloat((float) 3);
        sub.setAll(a);
        assertArrayEquals(new float[]{1, 2, 3}, sub.toArray(), 0);
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new float[]{0, 1, 2, 3}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertTrue(array.hasCharacteristics(DISTINCT));
        assertTrue(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArraySet2() {
        array.clear();
        array.addFloat((float) 2);
        array.addFloat((float) 3);
        array.addFloat((float) 4);
        BoundedFloatArray sub = array.offset(1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 1);
        a.addFloat((float) 2);
        a.addFloat((float) 3);
        sub.setAll(a);
        assertArrayEquals(new float[]{1, 2, 3}, sub.toArray(), 0);
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new float[]{2, 1, 2, 3}, array.toArray(), 0);
        assertFalse(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

    @Test
    public void testCharacteristicsSubArraySet3() {
        array.clear();
        array.addFloat((float) 1);
        array.addFloat((float) 2);
        array.addFloat((float) 4);
        BoundedFloatArray sub = array.offset(1);
        UnboundedFloatArray a = UnboundedFloatArray.newInstance();
        a.addFloat((float) 1);
        a.addFloat((float) 2);
        a.addFloat((float) 3);
        sub.setAll(a);
        assertArrayEquals(new float[]{1, 2, 3}, sub.toArray(), 0);
        assertTrue(sub.hasCharacteristics(SORTED));
        assertTrue(sub.hasCharacteristics(DISTINCT));
        assertTrue(sub.hasCharacteristics(SORTED | DISTINCT));
        assertArrayEquals(new float[]{1, 1, 2, 3}, array.toArray(), 0);
        assertTrue(array.hasCharacteristics(SORTED));
        assertFalse(array.hasCharacteristics(DISTINCT));
        assertFalse(array.hasCharacteristics(SORTED | DISTINCT));
    }

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

Related Classes of org.squarebrackets.BoundedFloatBufferArrayTest

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.