diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 0afe0e406..177a0ba2c 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -1854,24 +1854,25 @@ public static void reverse(final short[] array, final int startIndexInclusive, f * *

There is no special handling for multi-dimensional arrays.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final Object[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -1880,24 +1881,27 @@ public static void swap(final Object[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

There is no special handling for multi-dimensional arrays.

+ * + *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final long[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -1906,24 +1910,25 @@ public static void swap(final long[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final int[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -1932,24 +1937,25 @@ public static void swap(final int[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final short[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -1958,24 +1964,25 @@ public static void swap(final short[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final char[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -1984,24 +1991,25 @@ public static void swap(final char[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final byte[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -2010,24 +2018,25 @@ public static void swap(final byte[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final double[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -2036,24 +2045,25 @@ public static void swap(final double[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final float[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -2062,24 +2072,25 @@ public static void swap(final float[] array, int offset1, int offset2) { /** *

Swaps two elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero).

* - *

Examples: + *

Examples: *

*

* * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap - * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range */ public static void swap(final boolean[] array, int offset1, int offset2) { - if (array == null) { + if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); @@ -2088,45 +2099,61 @@ public static void swap(final boolean[] array, int offset1, int offset2) { /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

- * - *

Examples: - *

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. *

* - * @param array the array to swap, may be {@code null} - * @param offset1 the index of the first element in the series to swap - * @param offset2 the index of the second element in the series to swap - * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range - */ - public static void swap(final boolean[] array, int offset1, int offset2, int len) { - if (array == null) { - return; - } - for (int i = 0; i < len; i++) { - boolean aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } - } - - /** - *

Swaps a series of elements in the given array.

- * - *

This method does nothing for a {@code null} input array.

- * *

Examples: *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + */ + public static void swap(final boolean[] array, int offset1, int offset2, int len) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { + return; + } + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + boolean aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

+ * + *

Examples: + *

*

* @@ -2134,31 +2161,42 @@ public static void swap(final boolean[] array, int offset1, int offset2, int le * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final byte[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - byte aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + byte aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; } } /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

* *

Examples: *

*

* @@ -2166,30 +2204,41 @@ public static void swap(final byte[] array, int offset1, int offset2, int len) * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final char[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - char aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + char aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; } } /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

* *

Examples: *

*

* @@ -2197,30 +2246,41 @@ public static void swap(final char[] array, int offset1, int offset2, int len) * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final double[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - double aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + double aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; } } /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

* *

Examples: *

*

* @@ -2228,30 +2288,42 @@ public static void swap(final double[] array, int offset1, int offset2, int len * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final float[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - float aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + if (offset1 < 0) { + offset1 = 0; } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + float aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; + } + } /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

* *

Examples: *

*

* @@ -2259,30 +2331,41 @@ public static void swap(final float[] array, int offset1, int offset2, int len) * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final int[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - int aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + int aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; } } /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

* *

Examples: *

*

* @@ -2290,30 +2373,41 @@ public static void swap(final int[] array, int offset1, int offset2, int len) { * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final long[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - long aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + long aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; + } } /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

* *

Examples: *

*

* @@ -2321,30 +2415,41 @@ public static void swap(final long[] array, int offset1, int offset2, int len) * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final Object[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - Object aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + Object aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; } } /** *

Swaps a series of elements in the given array.

* - *

This method does nothing for a {@code null} input array.

+ *

This method does nothing for a {@code null} or empty input array or for overflow indices. + * Negative indices are promoted to 0(zero). + * If any of the sub-arrays to swap falls outside of the given array, + * then the swap is stopped at the end of the array and as many as possible elements are swapped. + *

* *

Examples: *

*

* @@ -2352,16 +2457,25 @@ public static void swap(final Object[] array, int offset1, int offset2, int len * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices - * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range */ public static void swap(final short[] array, int offset1, int offset2, int len) { - if (array == null) { + if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } - for (int i = 0; i < len; i++) { - short aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + if (offset1 < 0) { + offset1 = 0; + } + if (offset2 < 0) { + offset2 = 0; + } + if (offset1 == offset2) { + return; + } + len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); + for (int i = 0; i < len; i++, offset1++, offset2++) { + short aux = array[offset1]; + array[offset1] = array[offset2]; + array[offset2] = aux; } } diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index be3e11960..b0217193b 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -2142,18 +2142,18 @@ public void testSwapCharRange() { assertEquals(4, array[1]); assertEquals(1, array[2]); assertEquals(2, array[3]); - } - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapCharOutOfRange() { - char[] array = new char[] {1, 2, 3}; + array = new char[] {1, 2, 3}; ArrayUtils.swap(array, 0, 3); - } - - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapCharOutOfRangeLen() { - char[] array = new char[] {1, 2, 3}; + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + + array = new char[] {1, 2, 3}; ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0]); + assertEquals(2, array[1]); + assertEquals(1, array[2]); } @Test @@ -2173,18 +2173,18 @@ public void testSwapFloatRange() { assertEquals(4, array[1], 0); assertEquals(1, array[2], 0); assertEquals(2, array[3], 0); - } - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapFloatOutOfRange() { - float[] array = new float[] {1, 2, 3}; + array = new float[] {1, 2, 3}; ArrayUtils.swap(array, 0, 3); - } - - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapFloatOutOfRangeLen() { - float[] array = new float[] {1, 2, 3}; + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + + array = new float[] {1, 2, 3}; ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(1, array[2], 0); } @Test @@ -2204,18 +2204,18 @@ public void testSwapDoubleRange() { assertEquals(4, array[1], 0); assertEquals(1, array[2], 0); assertEquals(2, array[3], 0); - } - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapDoubleOutOfRange() { - double[] array = new double[] {1, 2, 3}; + array = new double[] {1, 2, 3}; ArrayUtils.swap(array, 0, 3); - } - - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapDoubleOutOfRangeLen() { - double[] array = new double[] {1, 2, 3}; + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + + array = new double[] {1, 2, 3}; ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(1, array[2], 0); } @Test @@ -2235,6 +2235,18 @@ public void testSwapIntRange() { assertEquals(4, array[1]); assertEquals(1, array[2]); assertEquals(2, array[3]); + + array = new int[] {1, 2, 3}; + ArrayUtils.swap(array, 3, 0); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + + array = new int[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0]); + assertEquals(2, array[1]); + assertEquals(1, array[2]); } @Test @@ -2249,18 +2261,6 @@ public void testSwapIntExchangedOffsets() { assertArrayEquals(new int[] {2, 3, 1}, array); } - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapIntOutOfRange() { - int[] array = new int[] {1, 2, 3}; - ArrayUtils.swap(array, 0, 3); - } - - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapIntOutOfRangeLen() { - int[] array = new int[] {1, 2, 3}; - ArrayUtils.swap(array, 0, 2, 2); - } - @Test public void testSwapLong() { long[] array = new long[] {1, 2, 3}; @@ -2278,18 +2278,18 @@ public void testSwapLongRange() { assertEquals(4, array[1]); assertEquals(1, array[2]); assertEquals(2, array[3]); - } - - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapLongOutOfRange() { - long[] array = new long[] {1, 2, 3}; + + array = new long[] {1, 2, 3}; ArrayUtils.swap(array, 0, 3); - } - - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapLongOutOfRangeLen() { - long[] array = new long[] {1, 2, 3}; + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + + array = new long[] {1, 2, 3}; ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0]); + assertEquals(2, array[1]); + assertEquals(1, array[2]); } @Test @@ -2309,18 +2309,21 @@ public void testSwapObjectRange() { assertEquals("4", array[1]); assertEquals("1", array[2]); assertEquals("2", array[3]); - } - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapObjectOutOfRange() { - String[] array = new String[] {"1", "2", "3"}; - ArrayUtils.swap(array, 0, 3); - } + array = new String[] {"1", "2", "3", "4"}; + ArrayUtils.swap(array, -1, 2, 3); + assertEquals("3", array[0]); + assertEquals("4", array[1]); + assertEquals("1", array[2]); + assertEquals("2", array[3]); - @Test(expected = ArrayIndexOutOfBoundsException.class) - public void testSwapObjectOutOfRangeLen() { - String[] array = new String[] {"1", "2", "3"}; - ArrayUtils.swap(array, 0, 2, 2); + array = new String[] {"1", "2", "3", "4", "5"}; + ArrayUtils.swap(array, -3, 2, 3); + assertEquals("3", array[0]); + assertEquals("4", array[1]); + assertEquals("5", array[2]); + assertEquals("2", array[3]); + assertEquals("1", array[4]); } //-----------------------------------------------------------------------