package com.humaorie.dollar;
/*
* Dollar, http://bitbucket.org/dfa/dollar
* (c) 2010, 2011 Davide Angelocola <davide.angelocola@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import com.humaorie.dollar.ArrayWrapper.BooleanArrayWrapper;
import com.humaorie.dollar.ArrayWrapper.ByteArrayWrapper;
import com.humaorie.dollar.ArrayWrapper.CharArrayWrapper;
import com.humaorie.dollar.ArrayWrapper.DoubleArrayWrapper;
import com.humaorie.dollar.ArrayWrapper.FloatArrayWrapper;
import com.humaorie.dollar.ArrayWrapper.IntegerArrayWrapper;
import com.humaorie.dollar.ArrayWrapper.LongArrayWrapper;
import com.humaorie.dollar.ArrayWrapper.ShortArrayWrapper;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
public class ArrayWrapperTest {
@Test
public void toStringYieldsTheStringRepresentation() {
final String[] array = {"a", "b", "c"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertEquals("[a, b, c]", arrayWrapper.toString());
}
@Test
public void sizeYieldArrayLenght() {
final String[] array = {"string"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertEquals(array.length, arrayWrapper.size());
}
@Test
public void toArrayYieldsOriginalArray() {
final String[] array = new String[]{"string"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertArrayEquals(array, arrayWrapper.toArray());
}
@Test(expected = IllegalArgumentException.class)
public void canWrapANullArray() {
new ArrayWrapper<String>(null);
}
@Test
public void canWrapAnEmptyArray() {
final String[] array = new String[0];
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertEquals("", arrayWrapper.join());
}
@Test
public void canCopyArray() {
final String[] expected = {"a", "b", "c"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(expected);
Assert.assertArrayEquals(expected, arrayWrapper.toArray());
}
@Test
public void canReverseAnArray() {
final String[] array = new String[]{"a", "b"};
final String[] expected = new String[]{"b", "a"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertArrayEquals(expected, arrayWrapper.reverse().toArray());
}
@Test
public void canFillAnArray() {
final String[] array = {"a", "a", "a"};
final String[] expected = {"c", "c", "c"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertArrayEquals(expected, arrayWrapper.fill("c").toArray());
}
@Test
public void canSliceAnArrayUntilEnd() {
final String[] array = {"a", "b", "c", "d", "e", "f"};
final String[] expected = {"a", "b", "c"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertArrayEquals(expected, arrayWrapper.slice(3).toArray());
}
@Test
public void canSliceArray() {
final String[] array = {"a", "b", "c", "d", "e", "f"};
final String[] expected = {"b", "c", "d", "e"};
final ArrayWrapper<String> arrayWrapper = new ArrayWrapper<String>(array);
Assert.assertArrayEquals(expected, arrayWrapper.slice(1, 5).toArray());
}
@Test
public void sortStrings() {
final String[] strings = {"e", "d", "c", "b", "a"};
Assert.assertEquals(Arrays.asList("a", "b", "c", "d", "e"), new ArrayWrapper<String>(strings).sort().toList());
}
@Test
public void maxStrings() {
final String[] strings = { "e", "d", "c", "b", "a" };
Assert.assertEquals("e",
new ComparableArrayWrapper<String>(strings).max());
}
@Test
public void minStrings() {
final String[] strings = { "e", "d", "c", "b", "a" };
Assert.assertEquals("a",
new ComparableArrayWrapper<String>(strings).min());
}
@Test
public void maxLongs() {
final Integer[] ints = { 3, 9, 42, 0 };
Assert.assertEquals(42, new ComparableArrayWrapper<Integer>(ints).max()
.intValue());
}
@Test
public void minInts() {
final Integer[] ints = { 3, 9, 42, 0 };
Assert.assertEquals(0, new ComparableArrayWrapper<Integer>(ints).min()
.intValue());
}
// array boxing tests
@Test
public void boxCharacterArray() {
char[] unboxed = new char[]{'f', 'o', 'o'};
CharArrayWrapper arrayWrapper = new CharArrayWrapper(unboxed);
Assert.assertArrayEquals(new Character[]{'f', 'o', 'o'}, arrayWrapper.toArray());
}
@Test
public void boxBooleanArray() {
boolean[] unboxed = new boolean[]{true, true, true};
BooleanArrayWrapper arrayWrapper = new BooleanArrayWrapper(unboxed);
Assert.assertArrayEquals(new Boolean[]{true, true, true}, arrayWrapper.toArray());
}
@Test
public void boxByteArray() {
byte[] unboxed = new byte[]{42, 42, 42};
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(unboxed);
Assert.assertArrayEquals(new Byte[]{42, 42, 42}, arrayWrapper.toArray());
}
@Test
public void boxShortArray() {
short[] unboxed = new short[]{42, 42, 42};
ShortArrayWrapper arrayWrapper = new ShortArrayWrapper(unboxed);
Assert.assertArrayEquals(new Short[]{42, 42, 42}, arrayWrapper.toArray());
}
@Test
public void boxIntArray() {
int[] unboxed = new int[]{42, 42, 42};
IntegerArrayWrapper arrayWrapper = new IntegerArrayWrapper(unboxed);
Assert.assertArrayEquals(new Integer[]{42, 42, 42}, arrayWrapper.toArray());
}
@Test
public void boxLongArray() {
long[] unboxed = new long[]{42L, 42L, 42L};
LongArrayWrapper arrayWrapper = new LongArrayWrapper(unboxed);
Assert.assertArrayEquals(new Long[]{42L, 42L, 42L}, arrayWrapper.toArray());
}
@Test
public void boxFloatArray() {
float[] unboxed = new float[]{42.0f, 42.0f, 42.0f};
FloatArrayWrapper arrayWrapper = new FloatArrayWrapper(unboxed);
Assert.assertArrayEquals(new Float[]{42F, 42F, 42F}, arrayWrapper.toArray());
}
@Test
public void boxDoubleArray() {
double[] unboxed = new double[]{42.0, 42.0, 42.0};
DoubleArrayWrapper arrayWrapper = new DoubleArrayWrapper(unboxed);
Assert.assertArrayEquals(new Double[]{42D, 42D, 42D}, arrayWrapper.toArray());
}
// unboxing
@Test
public void unboxCharacterArray() {
Character[] boxed = new Character[]{'f', 'o', 'o'};
CharArrayWrapper arrayWrapper = new CharArrayWrapper(boxed);
Assert.assertArrayEquals(new char[]{'f', 'o', 'o'}, arrayWrapper.toCharArray());
}
@Test
public void unboxCharacterArrayToByteArray() {
Character[] boxed = new Character[] { 'f', 'o', 'o' };
CharArrayWrapper arrayWrapper = new CharArrayWrapper(boxed);
Assert.assertArrayEquals(new byte[] { 'f', 'o', 'o' },
arrayWrapper.toByteArray());
}
@Test
public void unboxCharacterArrayToIntArray() {
Character[] boxed = new Character[]{'f', 'o', 'o'};
CharArrayWrapper arrayWrapper = new CharArrayWrapper(boxed);
Assert.assertArrayEquals(new int[]{'f', 'o', 'o'}, arrayWrapper.toIntArray());
}
@Test
public void unboxCharacterArrayToLongArray() {
Character[] boxed = new Character[]{'f', 'o', 'o'};
CharArrayWrapper arrayWrapper = new CharArrayWrapper(boxed);
Assert.assertArrayEquals(new long[]{'f', 'o', 'o'}, arrayWrapper.toLongArray());
}
@Test
public void unboxCharacterArrayWithNulls() {
Character[] boxed = new Character[3];
CharArrayWrapper arrayWrapper = new CharArrayWrapper(boxed);
Assert.assertArrayEquals(new char[]{0, 0, 0}, arrayWrapper.toCharArray());
}
@Test
public void unboxBooleanArray() {
Boolean[] boxed = new Boolean[]{true, true, false};
BooleanArrayWrapper arrayWrapper = new BooleanArrayWrapper(boxed);
final boolean[] expected = new boolean[]{true, true, false};
Assert.assertTrue(Arrays.equals(expected, arrayWrapper.toBooleanArray()));
}
@Test
public void unboxBooleanArrayWithNulls() {
Boolean[] boxed = new Boolean[3];
BooleanArrayWrapper arrayWrapper = new BooleanArrayWrapper(boxed);
final boolean[] expected = new boolean[]{false, false, false};
Assert.assertTrue(Arrays.equals(expected, arrayWrapper.toBooleanArray()));
}
@Test
public void unboxByteArray() {
Byte[] boxed = new Byte[]{42, 43, 44};
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(boxed);
Assert.assertArrayEquals(new byte[]{42, 43, 44}, arrayWrapper.toByteArray());
}
@Test
public void unboxByteArrayToCharArray() {
Byte[] boxed = new Byte[] { 'f', 'o', 'o' };
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(boxed);
Assert.assertArrayEquals(new char[] { 'f', 'o', 'o' },
arrayWrapper.toCharArray());
}
@Test
public void unboxByteArrayToShortArray() {
Byte[] boxed = new Byte[]{42, 43, 44};
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(boxed);
Assert.assertArrayEquals(new short[]{42, 43, 44}, arrayWrapper.toShortArray());
}
@Test
public void unboxByteArrayToIntArray() {
Byte[] boxed = new Byte[]{42, 43, 44};
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(boxed);
Assert.assertArrayEquals(new int[]{42, 43, 44}, arrayWrapper.toIntArray());
}
@Test
public void unboxByteArrayToLongArray() {
Byte[] boxed = new Byte[]{42, 43, 44};
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(boxed);
Assert.assertArrayEquals(new long[]{42, 43, 44}, arrayWrapper.toLongArray());
}
@Test
public void unboxByteArrayWithNulls() {
Byte[] boxed = new Byte[3];
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(boxed);
Assert.assertArrayEquals(new byte[]{0, 0, 0}, arrayWrapper.toByteArray());
}
@Test
public void unboxShortArray() {
final Short[] boxed = {42};
final short[] unboxed = {42};
final ShortArrayWrapper arrayWrapper = new ShortArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toShortArray());
}
@Test
public void unboxShortArrayToIntArray() {
final Short[] boxed = {42};
final int[] unboxed = {42};
final ShortArrayWrapper arrayWrapper = new ShortArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toIntArray());
}
@Test
public void unboxShortArrayToLongArray() {
final Short[] boxed = {42};
final long[] unboxed = {42};
final ShortArrayWrapper arrayWrapper = new ShortArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toLongArray());
}
@Test
public void unboxShortArrayWithNulls() {
final Short[] boxed = {null};
final short[] unboxed = {0};
final ShortArrayWrapper arrayWrapper = new ShortArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toShortArray());
}
@Test
public void unboxIntegerArray() {
final Integer[] boxed = {42};
final int[] unboxed = {42};
final IntegerArrayWrapper arrayWrapper = new IntegerArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toIntArray());
}
@Test
public void unboxIntegerArrayWithNulls() {
final Integer[] boxed = {null};
final int[] unboxed = {0};
final IntegerArrayWrapper arrayWrapper = new IntegerArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toIntArray());
}
@Test
public void unboxIntegerArrayToLongArray() {
final Integer[] boxed = {42};
final long[] unboxed = {42};
final IntegerArrayWrapper arrayWrapper = new IntegerArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toLongArray());
}
@Test
public void unboxIntegerArrayToFloatArray() {
final Integer[] boxed = { 42 };
final float[] unboxed = { 42f };
final IntegerArrayWrapper arrayWrapper = new IntegerArrayWrapper(
boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toFloatArray(), 0.001f);
}
@Test
public void unboxIntegerArrayToDoubleArray() {
final Integer[] boxed = { 42 };
final double[] unboxed = { 42.0 };
final IntegerArrayWrapper arrayWrapper = new IntegerArrayWrapper(
boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toDoubleArray(), 0.001);
}
@Test
public void unboxLongArray() {
final Long[] boxed = {null};
final long[] unboxed = {0};
final LongArrayWrapper arrayWrapper = new LongArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toLongArray());
}
@Test
public void unboxLongArrayWithNulls() {
final Long[] boxed = {null};
final long[] unboxed = {0};
final LongArrayWrapper arrayWrapper = new LongArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toLongArray());
}
@Test
public void unboxFloatArray() {
final Float[] boxed = {42.0f};
final float[] unboxed = {42};
final FloatArrayWrapper arrayWrapper = new FloatArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toFloatArray(), 0.1f);
}
@Test
public void unboxFloatArrayToDoubleArray() {
final Float[] boxed = {42f};
final double[] unboxed = {42};
final FloatArrayWrapper arrayWrapper = new FloatArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toDoubleArray(), 0.1d);
}
@Test
public void unboxFloatArrayWithNulls() {
final Float[] boxed = {null};
final float[] unboxed = {0};
final FloatArrayWrapper arrayWrapper = new FloatArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toFloatArray(), 0.1f);
}
@Test
public void unboxDoubleArray() {
final Double[] boxed = {42.0};
final double[] unboxed = {42};
final DoubleArrayWrapper arrayWrapper = new DoubleArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toDoubleArray(), 0.1d);
}
@Test
public void unboxDoubleArrayWithNullsLeadsToZero() {
final Double[] boxed = {null};
final double[] unboxed = {0};
final DoubleArrayWrapper arrayWrapper = new DoubleArrayWrapper(boxed);
Assert.assertArrayEquals(unboxed, arrayWrapper.toDoubleArray(), 0.1d);
}
}