[LANG-926] Add ArrayUtils.reverse(array, from, to) methods.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1535078 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b0cd0eabd1
commit
7869bf058e
|
@ -22,6 +22,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="3.2" date="TBA" description="Next release">
|
<release version="3.2" date="TBA" description="Next release">
|
||||||
|
<action issue="LANG-926" type="add" dev="ggregory">Add ArrayUtils.reverse(array, from, to) methods.</action>
|
||||||
<action issue="LANG-795" type="add" due-to="Aaron Digulla">StringUtils.toString(byte[], String) deprecated in favour of a new StringUtils.toString(byte[], CharSet)</action>
|
<action issue="LANG-795" type="add" due-to="Aaron Digulla">StringUtils.toString(byte[], String) deprecated in favour of a new StringUtils.toString(byte[], CharSet)</action>
|
||||||
<action issue="LANG-902" type="fix" due-to="Andrzej Winnicki">RandomStringUtils.random javadoc was incorrectly promising letters and numbers would, as opposed to may, appear</action>
|
<action issue="LANG-902" type="fix" due-to="Andrzej Winnicki">RandomStringUtils.random javadoc was incorrectly promising letters and numbers would, as opposed to may, appear</action>
|
||||||
<action issue="LANG-921" type="fix" dev="britter">BooleanUtils.xor(boolean...) produces wrong results</action>
|
<action issue="LANG-921" type="fix" dev="britter">BooleanUtils.xor(boolean...) produces wrong results</action>
|
||||||
|
|
|
@ -1435,16 +1435,7 @@ public static void reverse(final Object[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
Object tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1458,16 +1449,7 @@ public static void reverse(final long[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
long tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1481,16 +1463,7 @@ public static void reverse(final int[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
int tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1504,16 +1477,7 @@ public static void reverse(final short[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
short tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1527,16 +1491,7 @@ public static void reverse(final char[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
char tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1550,16 +1505,7 @@ public static void reverse(final byte[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
byte tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1573,16 +1519,7 @@ public static void reverse(final double[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
double tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1596,16 +1533,7 @@ public static void reverse(final float[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
|
||||||
float tmp;
|
|
||||||
while (j > i) {
|
|
||||||
tmp = array[j];
|
|
||||||
array[j] = array[i];
|
|
||||||
array[i] = tmp;
|
|
||||||
j--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1619,8 +1547,33 @@ public static void reverse(final boolean[] array) {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = 0;
|
reverse(array, 0, array.length);
|
||||||
int j = array.length - 1;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final boolean[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
boolean tmp;
|
boolean tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
tmp = array[j];
|
tmp = array[j];
|
||||||
|
@ -1631,6 +1584,278 @@ public static void reverse(final boolean[] array) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final byte[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
byte tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final char[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
char tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final double[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
double tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final float[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
float tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final int[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
int tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final long[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
long tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final Object[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
Object tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Reverses the order of the given array in the given range.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method does nothing for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to reverse, may be {@code null}
|
||||||
|
* @param startIndexInclusive
|
||||||
|
* the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
|
||||||
|
* change.
|
||||||
|
* @param endIndexExclusive
|
||||||
|
* elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
|
||||||
|
* change. Overvalue (>array.length) is demoted to array length.
|
||||||
|
*/
|
||||||
|
public static void reverse(final short[] array, int startIndexInclusive, int endIndexExclusive) {
|
||||||
|
if (array == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
||||||
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
|
short tmp;
|
||||||
|
while (j > i) {
|
||||||
|
tmp = array[j];
|
||||||
|
array[j] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
j--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IndexOf search
|
// IndexOf search
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -1806,6 +1806,295 @@ public void testReverseBoolean() {
|
||||||
assertEquals(null, array);
|
assertEquals(null, array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseBooleanRange() {
|
||||||
|
boolean[] array = new boolean[] {false, false, true};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertTrue(array[0]);
|
||||||
|
assertFalse(array[1]);
|
||||||
|
assertFalse(array[2]);
|
||||||
|
// a range
|
||||||
|
array = new boolean[] {false, false, true};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertFalse(array[0]);
|
||||||
|
assertFalse(array[1]);
|
||||||
|
assertTrue(array[2]);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new boolean[] {false, false, true};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertTrue(array[0]);
|
||||||
|
assertFalse(array[1]);
|
||||||
|
assertFalse(array[2]);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new boolean[] {false, false, true};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertTrue(array[0]);
|
||||||
|
assertFalse(array[1]);
|
||||||
|
assertFalse(array[2]);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseByteRange() {
|
||||||
|
byte[] array = new byte[] {1, 2, 3};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range
|
||||||
|
array = new byte[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals(2, array[0]);
|
||||||
|
assertEquals(1, array[1]);
|
||||||
|
assertEquals(3, array[2]);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new byte[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new byte[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseCharRange() {
|
||||||
|
char[] array = new char[] {1, 2, 3};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range
|
||||||
|
array = new char[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals(2, array[0]);
|
||||||
|
assertEquals(1, array[1]);
|
||||||
|
assertEquals(3, array[2]);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new char[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new char[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseDoubleRange() {
|
||||||
|
double[] array = new double[] {1, 2, 3};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(3, array[0], 0);
|
||||||
|
assertEquals(2, array[1], 0);
|
||||||
|
assertEquals(1, array[2], 0);
|
||||||
|
// a range
|
||||||
|
array = new double[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals(2, array[0], 0);
|
||||||
|
assertEquals(1, array[1], 0);
|
||||||
|
assertEquals(3, array[2], 0);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new double[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals(3, array[0], 0);
|
||||||
|
assertEquals(2, array[1], 0);
|
||||||
|
assertEquals(1, array[2], 0);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new double[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals(3, array[0], 0);
|
||||||
|
assertEquals(2, array[1], 0);
|
||||||
|
assertEquals(1, array[2], 0);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseFloatRange() {
|
||||||
|
float[] array = new float[] {1, 2, 3};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(3, array[0], 0);
|
||||||
|
assertEquals(2, array[1], 0);
|
||||||
|
assertEquals(1, array[2], 0);
|
||||||
|
// a range
|
||||||
|
array = new float[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals(2, array[0], 0);
|
||||||
|
assertEquals(1, array[1], 0);
|
||||||
|
assertEquals(3, array[2], 0);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new float[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals(3, array[0], 0);
|
||||||
|
assertEquals(2, array[1], 0);
|
||||||
|
assertEquals(1, array[2], 0);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new float[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals(3, array[0], 0);
|
||||||
|
assertEquals(2, array[1], 0);
|
||||||
|
assertEquals(1, array[2], 0);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseIntRange() {
|
||||||
|
int[] array = new int[] {1, 2, 3};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range
|
||||||
|
array = new int[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals(2, array[0]);
|
||||||
|
assertEquals(1, array[1]);
|
||||||
|
assertEquals(3, array[2]);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new int[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new int[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseLongRange() {
|
||||||
|
long[] array = new long[] {1, 2, 3};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range
|
||||||
|
array = new long[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals(2, array[0]);
|
||||||
|
assertEquals(1, array[1]);
|
||||||
|
assertEquals(3, array[2]);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new long[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new long[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseShortRange() {
|
||||||
|
short[] array = new short[] {1, 2, 3};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range
|
||||||
|
array = new short[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals(2, array[0]);
|
||||||
|
assertEquals(1, array[1]);
|
||||||
|
assertEquals(3, array[2]);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new short[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new short[] {1, 2, 3};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals(3, array[0]);
|
||||||
|
assertEquals(2, array[1]);
|
||||||
|
assertEquals(1, array[2]);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReverseObjectRange() {
|
||||||
|
String[] array = new String[] {"1", "2", "3"};
|
||||||
|
// The whole array
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals("3", array[0]);
|
||||||
|
assertEquals("2", array[1]);
|
||||||
|
assertEquals("1", array[2]);
|
||||||
|
// a range
|
||||||
|
array = new String[] {"1", "2", "3"};
|
||||||
|
ArrayUtils.reverse(array, 0, 2);
|
||||||
|
assertEquals("2", array[0]);
|
||||||
|
assertEquals("1", array[1]);
|
||||||
|
assertEquals("3", array[2]);
|
||||||
|
// a range with a negative start
|
||||||
|
array = new String[] {"1", "2", "3"};
|
||||||
|
ArrayUtils.reverse(array, -1, 3);
|
||||||
|
assertEquals("3", array[0]);
|
||||||
|
assertEquals("2", array[1]);
|
||||||
|
assertEquals("1", array[2]);
|
||||||
|
// a range with a large stop idnex
|
||||||
|
array = new String[] {"1", "2", "3"};
|
||||||
|
ArrayUtils.reverse(array, -1, array.length + 1000);
|
||||||
|
assertEquals("3", array[0]);
|
||||||
|
assertEquals("2", array[1]);
|
||||||
|
assertEquals("1", array[2]);
|
||||||
|
// null
|
||||||
|
array = null;
|
||||||
|
ArrayUtils.reverse(array, 0, 3);
|
||||||
|
assertEquals(null, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
@Test
|
@Test
|
||||||
public void testIndexOf() {
|
public void testIndexOf() {
|
||||||
|
|
Loading…
Reference in New Issue