Package org.squarebrackets

Source Code of org.squarebrackets.MutableShortArrayTest

package org.squarebrackets;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.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;

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

    private MutableShortArray array;

    @Before
    public void setUp() throws Exception {
        UnboundedShortArray a = UnboundedShortArray.newInstance();
        a.addShort((short) 0);
        a.addShort((short) 1);
        a.addShort((short) 1);
        a.addShort((short) 2);
        a.addShort((short) 1);
        a.addShort((short) 2);
        a.addShort((short) 3);
        a.addShort((short) 3);
        a.addShort((short) 4);
        array = MutableShortArray.copyOf(a);
        array = serializeAndDeserialize(array); // Test serialization.
        array = Arrays.synchronizedArray(array, array);    // Test synchronized array.
        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 unbounded() {
        assertFalse(array.hasCharacteristics(ArrayCharacteristics.REPLACEABLE));
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Related Classes of org.squarebrackets.MutableShortArrayTest

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.