Add to ArrayUtils and test it
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137134 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7477a1e08f
commit
7184f0650c
|
@ -56,6 +56,8 @@ package org.apache.commons.lang;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang.builder.ToStringStyle;
|
||||
/**
|
||||
|
@ -65,7 +67,7 @@ import org.apache.commons.lang.builder.ToStringStyle;
|
|||
* @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a>
|
||||
* @author Moritz Petersen
|
||||
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
|
||||
* @version $Id: ArrayUtils.java,v 1.4 2002/11/16 10:41:03 scolebourne Exp $
|
||||
* @version $Id: ArrayUtils.java,v 1.5 2002/11/16 12:56:43 scolebourne Exp $
|
||||
*/
|
||||
public class ArrayUtils {
|
||||
|
||||
|
@ -100,6 +102,7 @@ public class ArrayUtils {
|
|||
public ArrayUtils() {
|
||||
}
|
||||
|
||||
// Basic methods handling multi-dimensional arrays
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
@ -110,7 +113,7 @@ public class ArrayUtils {
|
|||
*
|
||||
* <p>The format is that of Java source code, for example {a,b}.</p>
|
||||
*
|
||||
* @param array the array to get a toString for, may not be <code>null</code>
|
||||
* @param array the array to get a toString for, may be <code>null</code>
|
||||
* @return a String representation of the array, '{}' if <code>null</code> passed in
|
||||
*/
|
||||
public static String toString(Object array) {
|
||||
|
@ -136,6 +139,34 @@ public class ArrayUtils {
|
|||
return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Get a hashCode for an array handling multi-dimensional arrays correctly.</p>
|
||||
*
|
||||
* <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
|
||||
*
|
||||
* @param array the array to get a hashCode for, may be <code>null</code>
|
||||
* @return a hashCode for the array
|
||||
*/
|
||||
public static int hashCode(Object array) {
|
||||
return new HashCodeBuilder().append(array).toHashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Compares two arrays, using equals(), handling multi-dimensional arrays
|
||||
* correctly.</p>
|
||||
*
|
||||
* <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
|
||||
*
|
||||
* @param array1 the array to get a hashCode for, may be <code>null</code>
|
||||
* @param array2 the array to get a hashCode for, may be <code>null</code>
|
||||
* @return <code>true</code> if the arrays are equal
|
||||
*/
|
||||
public static boolean isEquals(Object array1, Object array2) {
|
||||
return new EqualsBuilder().append(array1, array2).isEquals();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Converts the given array into a {@link Map}. Each element of the array
|
||||
* must be either a {@link Map.Entry} or an Array, containing at least two
|
||||
|
@ -298,9 +329,10 @@ public class ArrayUtils {
|
|||
* <p>Shallow clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>The objecs in the array are not cloned.</p>
|
||||
* <p>The objecs in the array are not cloned, thus there is no special
|
||||
* handling for multi-dimensional arrays.</p>
|
||||
*
|
||||
* @param array the array to shallow clone, may not be <code>null</code>
|
||||
* @param array the array to shallow clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -314,8 +346,10 @@ public class ArrayUtils {
|
|||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may not be <code>null</code>
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -329,8 +363,10 @@ public class ArrayUtils {
|
|||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may not be <code>null</code>
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -344,8 +380,10 @@ public class ArrayUtils {
|
|||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may not be <code>null</code>
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -359,8 +397,27 @@ public class ArrayUtils {
|
|||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may not be <code>null</code>
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
public static char[] clone(char[] array) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
}
|
||||
return (char[]) array.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -374,8 +431,10 @@ public class ArrayUtils {
|
|||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may not be <code>null</code>
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -389,8 +448,10 @@ public class ArrayUtils {
|
|||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may not be <code>null</code>
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -404,8 +465,10 @@ public class ArrayUtils {
|
|||
/**
|
||||
* <p>Clones an array returning a typecast result and handling
|
||||
* <code>null</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array the array to clone, may not be <code>null</code>
|
||||
* @param array the array to clone, may be <code>null</code>
|
||||
* @return the cloned array, or <code>null</code> if <code>null</code>
|
||||
* passed in
|
||||
*/
|
||||
|
@ -498,6 +561,26 @@ public class ArrayUtils {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks whether two arrays are the same length, treating
|
||||
* <code>null</code> arrays as length <code>0</code>.</p>
|
||||
*
|
||||
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
|
||||
*
|
||||
* @param array1 the first array, may be <code>null</code>
|
||||
* @param array2 the second array, may be <code>null</code>
|
||||
* @return <code>true</code> if length of arrays matches, treating
|
||||
* <code>null</code> as an empty array
|
||||
*/
|
||||
public static boolean isSameLength(char[] array1, char[] array2) {
|
||||
if ((array1 == null && array2 != null && array2.length > 0) ||
|
||||
(array2 == null && array1 != null && array1.length > 0) ||
|
||||
(array1 != null && array2 != null && array1.length != array2.length)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks whether two arrays are the same length, treating
|
||||
* <code>null</code> arrays as length <code>0</code>.</p>
|
||||
|
@ -582,6 +665,8 @@ public class ArrayUtils {
|
|||
* <p>Checks whether two arrays are the same type taking into account
|
||||
* multi-dimensional arrays.</p>
|
||||
*
|
||||
* <p>Primitive arrays may be compared using this method too.</p>
|
||||
*
|
||||
* @param array1 the first array, must not be <code>null</code>
|
||||
* @param array2 the second array, must not be <code>null</code>
|
||||
* @return <code>true</code> if type of arrays matches
|
||||
|
|
|
@ -54,6 +54,7 @@ package org.apache.commons.lang;
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
@ -65,7 +66,7 @@ import junit.textui.TestRunner;
|
|||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author Moritz Petersen
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.1 2002/10/13 22:42:59 scolebourne Exp $
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.2 2002/11/16 12:56:44 scolebourne Exp $
|
||||
*/
|
||||
public class ArrayUtilsTest extends TestCase {
|
||||
|
||||
|
@ -92,7 +93,47 @@ public class ArrayUtilsTest extends TestCase {
|
|||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testToString() {
|
||||
assertEquals("{}", ArrayUtils.toString(null));
|
||||
assertEquals("{}", ArrayUtils.toString(new Object[0]));
|
||||
assertEquals("{}", ArrayUtils.toString(new String[0]));
|
||||
assertEquals("{<null>}", ArrayUtils.toString(new String[] {null}));
|
||||
assertEquals("{pink,blue}", ArrayUtils.toString(new String[] {"pink","blue"}));
|
||||
|
||||
assertEquals("<empty>", ArrayUtils.toString(null, "<empty>"));
|
||||
assertEquals("{}", ArrayUtils.toString(new Object[0], "<empty>"));
|
||||
assertEquals("{}", ArrayUtils.toString(new String[0], "<empty>"));
|
||||
assertEquals("{<null>}", ArrayUtils.toString(new String[] {null}, "<empty>"));
|
||||
assertEquals("{pink,blue}", ArrayUtils.toString(new String[] {"pink","blue"}, "<empty>"));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testHashCode() {
|
||||
long[][] array1 = new long[][] {{2,5}, {4,5}};
|
||||
long[][] array2 = new long[][] {{2,5}, {4,6}};
|
||||
assertEquals(true, ArrayUtils.hashCode(array1) == ArrayUtils.hashCode(array1));
|
||||
assertEquals(false, ArrayUtils.hashCode(array1) == ArrayUtils.hashCode(array2));
|
||||
|
||||
Object[] array3 = new Object[] {new String(new char[] {'A', 'B'})};
|
||||
Object[] array4 = new Object[] {"AB"};
|
||||
assertEquals(true, ArrayUtils.hashCode(array3) == ArrayUtils.hashCode(array3));
|
||||
assertEquals(true, ArrayUtils.hashCode(array3) == ArrayUtils.hashCode(array4));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testIsEquals() {
|
||||
long[][] array1 = new long[][] {{2,5}, {4,5}};
|
||||
long[][] array2 = new long[][] {{2,5}, {4,6}};
|
||||
assertEquals(true, ArrayUtils.isEquals(array1, array1));
|
||||
assertEquals(false, ArrayUtils.isEquals(array1, array2));
|
||||
|
||||
Object[] array3 = new Object[] {new String(new char[] {'A', 'B'})};
|
||||
Object[] array4 = new Object[] {"AB"};
|
||||
assertEquals(true, ArrayUtils.isEquals(array3, array3));
|
||||
assertEquals(true, ArrayUtils.isEquals(array3, array4));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testToMap() {
|
||||
Map map = ArrayUtils.toMap(new String[][] {{"foo", "bar"}, {"hello", "world"}});
|
||||
|
||||
|
@ -102,30 +143,19 @@ public class ArrayUtilsTest extends TestCase {
|
|||
try {
|
||||
ArrayUtils.toMap(null);
|
||||
fail("exception expected");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected.
|
||||
}
|
||||
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
try {
|
||||
ArrayUtils.toMap(new String[][] {{"foo", "bar"}, {"short"}});
|
||||
fail("exception expected");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected.
|
||||
}
|
||||
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
try {
|
||||
ArrayUtils.toMap(new Object[] {new Object[] {"foo", "bar"}, "illegal type"});
|
||||
fail("exception expected");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected.
|
||||
}
|
||||
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
try {
|
||||
ArrayUtils.toMap(new Object[] {new Object[] {"foo", "bar"}, null});
|
||||
fail("exception expected");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected.
|
||||
}
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
|
||||
map = ArrayUtils.toMap(new Object[] {new Map.Entry() {
|
||||
public Object getKey() {
|
||||
|
@ -147,6 +177,344 @@ public class ArrayUtilsTest extends TestCase {
|
|||
assertEquals("bar", map.get("foo"));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testClone() {
|
||||
assertEquals(null, ArrayUtils.clone((Object[]) null));
|
||||
Object[] original1 = new Object[0];
|
||||
Object[] cloned1 = ArrayUtils.clone(original1);
|
||||
assertTrue(Arrays.equals(original1, cloned1));
|
||||
assertTrue(original1 != cloned1);
|
||||
|
||||
StringBuffer buf = new StringBuffer("pick");
|
||||
original1 = new Object[] {buf, "a", new String[] {"stick"}};
|
||||
cloned1 = ArrayUtils.clone(original1);
|
||||
assertTrue(Arrays.equals(original1, cloned1));
|
||||
assertTrue(original1 != cloned1);
|
||||
assertSame(original1[0], cloned1[0]);
|
||||
assertSame(original1[1], cloned1[1]);
|
||||
assertSame(original1[2], cloned1[2]);
|
||||
}
|
||||
|
||||
public void testCloneBoolean() {
|
||||
boolean[] original = new boolean[] {true, false};
|
||||
boolean[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
public void testCloneLong() {
|
||||
long[] original = new long[] {0L, 1L};
|
||||
long[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
public void testCloneInt() {
|
||||
int[] original = new int[] {5, 8};
|
||||
int[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
public void testCloneShort() {
|
||||
short[] original = new short[] {1, 4};
|
||||
short[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
public void testCloneChar() {
|
||||
char[] original = new char[] {'a', '4'};
|
||||
char[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
public void testCloneByte() {
|
||||
byte[] original = new byte[] {1, 6};
|
||||
byte[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
public void testCloneDouble() {
|
||||
double[] original = new double[] {2.4d, 5.7d};
|
||||
double[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
public void testCloneFloat() {
|
||||
float[] original = new float[] {2.6f, 6.4f};
|
||||
float[] cloned = ArrayUtils.clone(original);
|
||||
assertTrue(Arrays.equals(original, cloned));
|
||||
assertTrue(original != cloned);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testSameLength() {
|
||||
Object[] nullArray = null;
|
||||
Object[] emptyArray = new Object[0];
|
||||
Object[] oneArray = new Object[] {"pick"};
|
||||
Object[] twoArray = new Object[] {"pick", "stick"};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthBoolean() {
|
||||
boolean[] nullArray = null;
|
||||
boolean[] emptyArray = new boolean[0];
|
||||
boolean[] oneArray = new boolean[] {true};
|
||||
boolean[] twoArray = new boolean[] {true, false};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthLong() {
|
||||
long[] nullArray = null;
|
||||
long[] emptyArray = new long[0];
|
||||
long[] oneArray = new long[] {0L};
|
||||
long[] twoArray = new long[] {0L, 76L};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthInt() {
|
||||
int[] nullArray = null;
|
||||
int[] emptyArray = new int[0];
|
||||
int[] oneArray = new int[] {4};
|
||||
int[] twoArray = new int[] {5, 7};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthShort() {
|
||||
short[] nullArray = null;
|
||||
short[] emptyArray = new short[0];
|
||||
short[] oneArray = new short[] {4};
|
||||
short[] twoArray = new short[] {6, 8};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthChar() {
|
||||
char[] nullArray = null;
|
||||
char[] emptyArray = new char[0];
|
||||
char[] oneArray = new char[] {'f'};
|
||||
char[] twoArray = new char[] {'d', 't'};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthByte() {
|
||||
byte[] nullArray = null;
|
||||
byte[] emptyArray = new byte[0];
|
||||
byte[] oneArray = new byte[] {3};
|
||||
byte[] twoArray = new byte[] {4, 6};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthDouble() {
|
||||
double[] nullArray = null;
|
||||
double[] emptyArray = new double[0];
|
||||
double[] oneArray = new double[] {1.3d};
|
||||
double[] twoArray = new double[] {4.5d, 6.3d};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
public void testSameLengthFloat() {
|
||||
float[] nullArray = null;
|
||||
float[] emptyArray = new float[0];
|
||||
float[] oneArray = new float[] {2.5f};
|
||||
float[] twoArray = new float[] {6.4f, 5.8f};
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(nullArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(nullArray, twoArray));
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, nullArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(emptyArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(emptyArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, emptyArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(oneArray, oneArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(oneArray, twoArray));
|
||||
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, nullArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, emptyArray));
|
||||
assertEquals(false, ArrayUtils.isSameLength(twoArray, oneArray));
|
||||
assertEquals(true, ArrayUtils.isSameLength(twoArray, twoArray));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testSameType() {
|
||||
try {
|
||||
ArrayUtils.isSameType(null, null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
try {
|
||||
ArrayUtils.isSameType(null, new Object[0]);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
try {
|
||||
ArrayUtils.isSameType(new Object[0], null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
|
||||
assertEquals(true, ArrayUtils.isSameType(new Object[0], new Object[0]));
|
||||
assertEquals(false, ArrayUtils.isSameType(new String[0], new Object[0]));
|
||||
assertEquals(true, ArrayUtils.isSameType(new String[0][0], new String[0][0]));
|
||||
assertEquals(false, ArrayUtils.isSameType(new String[0], new String[0][0]));
|
||||
assertEquals(false, ArrayUtils.isSameType(new String[0][0], new String[0]));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue