You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/12/06 00:37:19 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
scolebourne 2003/12/05 15:37:18
Modified: lang/src/java/org/apache/commons/lang ArrayUtils.java
lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
Log:
Add subarray methods for primitive types
Change subarray for Objects to return same type as input
from Ashwin S
Revision Changes Path
1.29 +310 -13 jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java
Index: ArrayUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- ArrayUtils.java 30 Nov 2003 13:36:08 -0000 1.28
+++ ArrayUtils.java 5 Dec 2003 23:37:18 -0000 1.29
@@ -53,6 +53,7 @@
*/
package org.apache.commons.lang;
+import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;
@@ -62,8 +63,8 @@
import org.apache.commons.lang.builder.ToStringStyle;
/**
- * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and primitive wrapper arrays
- * (like <code>Integer[]</code>).</p>
+ * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
+ * primitive wrapper arrays (like <code>Integer[]</code>).</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code>
@@ -441,21 +442,244 @@
/**
* <p>Produces a new array containing the elements between
* the start and end indices.</p>
- *
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * <p>The component type of the subarray is always the same as
+ * that of the input array. Thus, if the input is an array of type
+ * <code>Date</code>, the following usage is envisaged:</p>
+ *
+ * <pre>
+ * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
+ * </pre>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ Class type = array.getClass().getComponentType();
+ if (newSize <= 0) {
+ return (Object[]) Array.newInstance(type, 0);
+ }
+ Object[] subarray = (Object[]) Array.newInstance(type, newSize);
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>long</code> array containing the elements
+ * between the start and end indices.</p>
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static long[] subarray(long[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_LONG_ARRAY;
+ }
+
+ long[] subarray = new long[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>int</code> array containing the elements
+ * between the start and end indices.</p>
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static int[] subarray(int[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_INT_ARRAY;
+ }
+
+ int[] subarray = new int[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>short</code> array containing the elements
+ * between the start and end indices.</p>
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static short[] subarray(short[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_SHORT_ARRAY;
+ }
+
+ short[] subarray = new short[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>char</code> array containing the elements
+ * between the start and end indices.</p>
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static char[] subarray(char[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_CHAR_ARRAY;
+ }
+
+ char[] subarray = new char[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>byte</code> array containing the elements
+ * between the start and end indices.</p>
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_BYTE_ARRAY;
+ }
+
+ byte[] subarray = new byte[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>double</code> array containing the elements
+ * between the start and end indices.</p>
+ *
* <p>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.
- * The result is always an <code>Object[]</code> instance</p>
+ * Null array input produces null output.</p>
*
* @param array the array
- * @param startIndex the starting index. Undervalue (<0)
+ * @param startIndexInclusive the starting index. Undervalue (<0)
* is promoted to 0, overvalue (>array.length) results
* in an empty array.
- * @param endIndex elements upto endIndex-1 are present in the
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
* returned subarray. Undervalue (< startIndex) produces
* empty array, overvalue (>array.length) is demoted to
* array length.
*/
- public static Object[] subArray(Object[] array, int startIndexInclusive, int endIndexExclusive) {
+ public static double[] subarray(double[] array, int startIndexInclusive, int endIndexExclusive) {
if (array == null) {
return null;
}
@@ -467,11 +691,84 @@
}
int newSize = endIndexExclusive - startIndexInclusive;
if (newSize <= 0) {
- return EMPTY_OBJECT_ARRAY;
+ return EMPTY_DOUBLE_ARRAY;
}
- Object[] subArray = new Object[newSize];
- System.arraycopy(array, startIndexInclusive, subArray, 0, newSize);
- return subArray;
+
+ double[] subarray = new double[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>float</code> array containing the elements
+ * between the start and end indices.</p>
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static float[] subarray(float[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_FLOAT_ARRAY;
+ }
+
+ float[] subarray = new float[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * <p>Produces a new <code>boolean</code> array containing the elements
+ * between the start and end indices.</p>
+ *
+ * <p>The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.</p>
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ */
+ public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_BOOLEAN_ARRAY;
+ }
+
+ boolean[] subarray = new boolean[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
}
// Is same length
1.18 +646 -26 jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java
Index: ArrayUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- ArrayUtilsTest.java 30 Nov 2003 13:36:08 -0000 1.17
+++ ArrayUtilsTest.java 5 Dec 2003 23:37:18 -0000 1.18
@@ -56,6 +56,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Arrays;
+import java.util.Date;
import java.util.Map;
import junit.framework.Test;
@@ -274,47 +275,666 @@
//-----------------------------------------------------------------------
- public void testSubArray() {
- Object[] inarray = { "a", "b", "c", "d", "e", "f"};
+ public void testSubarrayObject() {
+ Object[] nullArray = null;
+ Object[] objectArray = { "a", "b", "c", "d", "e", "f"};
assertEquals("0 start, mid end", "abcd",
- StringUtils.join(ArrayUtils.subArray(inarray, 0, 4)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 0, 4)));
assertEquals("0 start, length end", "abcdef",
- StringUtils.join(ArrayUtils.subArray(inarray, 0, inarray.length)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 0, objectArray.length)));
assertEquals("mid start, mid end", "bcd",
- StringUtils.join(ArrayUtils.subArray(inarray, 1, 4)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 1, 4)));
assertEquals("mid start, length end", "bcdef",
- StringUtils.join(ArrayUtils.subArray(inarray, 1, inarray.length)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 1, objectArray.length)));
- assertNull("null input", ArrayUtils.subArray(null, 0, 3));
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
assertEquals("empty array", "",
- StringUtils.join(ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2)));
+ StringUtils.join(ArrayUtils.subarray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2)));
assertEquals("start > end", "",
- StringUtils.join(ArrayUtils.subArray(inarray, 4, 2)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 4, 2)));
assertEquals("start == end", "",
- StringUtils.join(ArrayUtils.subArray(inarray, 3, 3)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 3, 3)));
assertEquals("start undershoot, normal end", "abcd",
- StringUtils.join(ArrayUtils.subArray(inarray, -2, 4)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, -2, 4)));
assertEquals("start overshoot, any end", "",
- StringUtils.join(ArrayUtils.subArray(inarray, 33, 4)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 33, 4)));
assertEquals("normal start, end overshoot", "cdef",
- StringUtils.join(ArrayUtils.subArray(inarray, 2, 33)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, 2, 33)));
assertEquals("start undershoot, end overshoot", "abcdef",
- StringUtils.join(ArrayUtils.subArray(inarray, -2, 12)));
+ StringUtils.join(ArrayUtils.subarray(objectArray, -2, 12)));
- // object-level tests
- assertSame("empty array, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
- ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2));
- assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
- ArrayUtils.subArray(inarray, 4, 1));
- assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
- ArrayUtils.subArray(inarray, 33, 1));
- assertSame("start == end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
- ArrayUtils.subArray(inarray, 3, 3));
- assertSame("start overshoot, any end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
- ArrayUtils.subArray(inarray, 8733, 4));
+ // array type tests
+ Date[] dateArray = { new java.sql.Date(new Date().getTime()),
+ new Date(), new Date(), new Date(), new Date() };
+
+ assertSame("Object type", Object.class,
+ ArrayUtils.subarray(objectArray, 2, 4).getClass().getComponentType());
+ assertSame("java.util.Date type", java.util.Date.class,
+ ArrayUtils.subarray(dateArray, 1, 4).getClass().getComponentType());
+ assertNotSame("java.sql.Date type", java.sql.Date.class,
+ ArrayUtils.subarray(dateArray, 1, 4).getClass().getComponentType());
+ try {
+ Object dummy = (java.sql.Date[])ArrayUtils.subarray(dateArray, 1,3);
+ fail("Invalid downcast");
+ } catch (ClassCastException e) {}
+ }
+
+ public void testSubarrayLong() {
+ long[] nullArray = null;
+ long[] array = { 999910, 999911, 999912, 999913, 999914, 999915 };
+ long[] leftSubarray = { 999910, 999911, 999912, 999913 };
+ long[] midSubarray = { 999911, 999912, 999913, 999914 };
+ long[] rightSubarray = { 999912, 999913, 999914, 999915 };
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_LONG_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_LONG_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("long type", long.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
+ }
+
+ public void testSubarrayInt() {
+ int[] nullArray = null;
+ int[] array = { 10, 11, 12, 13, 14, 15 };
+ int[] leftSubarray = { 10, 11, 12, 13 };
+ int[] midSubarray = { 11, 12, 13, 14 };
+ int[] rightSubarray = { 12, 13, 14, 15 };
+
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_INT_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_INT_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("int type", int.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
+ }
+
+ public void testSubarrayShort() {
+ short[] nullArray = null;
+ short[] array = { 10, 11, 12, 13, 14, 15 };
+ short[] leftSubarray = { 10, 11, 12, 13 };
+ short[] midSubarray = { 11, 12, 13, 14 };
+ short[] rightSubarray = { 12, 13, 14, 15 };
+
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_SHORT_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_SHORT_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_SHORT_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("short type", short.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
+ }
+
+ public void testSubarrChar() {
+ char[] nullArray = null;
+ char[] array = { 'a', 'b', 'c', 'd', 'e', 'f' };
+ char[] leftSubarray = { 'a', 'b', 'c', 'd', };
+ char[] midSubarray = { 'b', 'c', 'd', 'e', };
+ char[] rightSubarray = { 'c', 'd', 'e', 'f', };
+
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_CHAR_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("char type", char.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
}
+ public void testSubarrayByte() {
+ byte[] nullArray = null;
+ byte[] array = { 10, 11, 12, 13, 14, 15 };
+ byte[] leftSubarray = { 10, 11, 12, 13 };
+ byte[] midSubarray = { 11, 12, 13, 14 };
+ byte[] rightSubarray = { 12, 13, 14, 15 };
+
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_BYTE_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_BYTE_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("byte type", byte.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
+ }
+
+ public void testSubarrayDouble() {
+ double[] nullArray = null;
+ double[] array = { 10.123, 11.234, 12.345, 13.456, 14.567, 15.678 };
+ double[] leftSubarray = { 10.123, 11.234, 12.345, 13.456, };
+ double[] midSubarray = { 11.234, 12.345, 13.456, 14.567, };
+ double[] rightSubarray = { 12.345, 13.456, 14.567, 15.678 };
+
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_DOUBLE_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_DOUBLE_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("double type", double.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
+ }
+
+ public void testSubarrayFloat() {
+ float[] nullArray = null;
+ float[] array = { 10, 11, 12, 13, 14, 15 };
+ float[] leftSubarray = { 10, 11, 12, 13 };
+ float[] midSubarray = { 11, 12, 13, 14 };
+ float[] rightSubarray = { 12, 13, 14, 15 };
+
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_FLOAT_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_FLOAT_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("float type", float.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
+ }
+
+ public void testSubarrayBoolean() {
+ boolean[] nullArray = null;
+ boolean[] array = { true, true, false, true, false, true };
+ boolean[] leftSubarray = { true, true, false, true };
+ boolean[] midSubarray = { true, false, true, false };
+ boolean[] rightSubarray = { false, true, false, true };
+
+
+ assertTrue("0 start, mid end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, 0, 4)));
+
+ assertTrue("0 start, length end",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, 0, array.length)));
+
+ assertTrue("mid start, mid end",
+ ArrayUtils.isEquals(midSubarray,
+ ArrayUtils.subarray(array, 1, 5)));
+
+ assertTrue("mid start, length end",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, array.length)));
+
+
+ assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3));
+
+ assertEquals("empty array", ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_BOOLEAN_ARRAY, 1, 2));
+
+ assertEquals("start > end", ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(array, 4, 2));
+
+ assertEquals("start == end", ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertTrue("start undershoot, normal end",
+ ArrayUtils.isEquals(leftSubarray,
+ ArrayUtils.subarray(array, -2, 4)));
+
+ assertEquals("start overshoot, any end",
+ ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(array, 33, 4));
+
+ assertTrue("normal start, end overshoot",
+ ArrayUtils.isEquals(rightSubarray,
+ ArrayUtils.subarray(array, 2, 33)));
+
+ assertTrue("start undershoot, end overshoot",
+ ArrayUtils.isEquals(array,
+ ArrayUtils.subarray(array, -2, 12)));
+
+ // empty-return tests
+
+ assertSame("empty array, object test",
+ ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(ArrayUtils.EMPTY_BOOLEAN_ARRAY, 1, 2));
+
+ assertSame("start > end, object test",
+ ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(array, 4, 1));
+
+ assertSame("start == end, object test",
+ ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(array, 3, 3));
+
+ assertSame("start overshoot, any end, object test",
+ ArrayUtils.EMPTY_BOOLEAN_ARRAY,
+ ArrayUtils.subarray(array, 8733, 4));
+
+ // array type tests
+
+ assertSame("boolean type", boolean.class,
+ ArrayUtils.subarray(array, 2, 4).getClass().getComponentType());
+
+ }
+
//-----------------------------------------------------------------------
public void testSameLength() {
Object[] nullArray = null;
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org