diff --git a/pom.xml b/pom.xml index 4f8c2e474..78f3c0777 100644 --- a/pom.xml +++ b/pom.xml @@ -481,6 +481,9 @@ Felipe Adorno + + Adrian Ber + diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ab6245eb4..56f440104 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Inconsistent behavior of swap for malformed inputs StringUtils join with var args Fix critical issues reported by SonarQube StrBuilder.equals(StrBuilder) doesn't check for null inputs diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index afb384d72..d9b65166a 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -1853,24 +1853,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); @@ -1879,24 +1880,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); @@ -1905,24 +1909,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); @@ -1931,24 +1936,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); @@ -1957,24 +1963,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); @@ -1983,24 +1990,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); @@ -2009,24 +2017,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); @@ -2035,24 +2044,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); @@ -2061,24 +2071,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); @@ -2087,45 +2098,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: + *

*

* @@ -2133,31 +2160,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: *

*

* @@ -2165,30 +2203,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: *

*

* @@ -2196,30 +2245,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: *

*

* @@ -2227,30 +2287,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: *

*

* @@ -2258,30 +2330,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: *

*

* @@ -2289,30 +2372,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: *

*

* @@ -2320,30 +2414,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: *

*

* @@ -2351,16 +2456,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; } } @@ -2535,6 +2649,8 @@ public static void shift(final boolean[] array, int startIndexInclusive, int end if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2593,6 +2709,8 @@ public static void shift(final byte[] array, int startIndexInclusive, int endInd if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2651,6 +2769,8 @@ public static void shift(final char[] array, int startIndexInclusive, int endInd if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2709,6 +2829,8 @@ public static void shift(final double[] array, int startIndexInclusive, int endI if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2767,6 +2889,8 @@ public static void shift(final float[] array, int startIndexInclusive, int endIn if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2825,6 +2949,8 @@ public static void shift(final int[] array, int startIndexInclusive, int endInde if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2882,6 +3008,8 @@ public static void shift(final long[] array, int startIndexInclusive, int endInd if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2939,6 +3067,8 @@ public static void shift(final Object[] array, int startIndexInclusive, int endI if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; @@ -2997,6 +3127,8 @@ public static void shift(final short[] array, int startIndexInclusive, int endIn if (offset < 0) { offset += n; } + // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity + // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/ while (n > 1 && offset > 0) { int n_offset = n - offset; diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 9e1c592ed..588068e75 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -2140,18 +2140,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 @@ -2171,18 +2171,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 @@ -2202,18 +2202,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 @@ -2233,6 +2233,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 @@ -2247,18 +2259,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}; @@ -2276,18 +2276,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 @@ -2307,18 +2307,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]); } //----------------------------------------------------------------------- @@ -2479,6 +2482,13 @@ public void testShiftShort() { assertEquals(4, array[1]); assertEquals(1, array[2]); assertEquals(2, array[3]); + array = new short[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 2); + assertEquals(4, array[0]); + assertEquals(5, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + assertEquals(3, array[4]); } @Test