This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 623235ebc [LANG-1811] ArrayUtils.shuffle() throws NullPointerException
for null array input.
623235ebc is described below
commit 623235ebca46bea1a6e8cc02b7d7084c1698ab80
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Jan 6 08:17:47 2026 -0500
[LANG-1811] ArrayUtils.shuffle() throws NullPointerException for null
array input.
- Null test assertions are from PR #1553.
- null inputs are now no-ops
- This follows the class Javadoc and other method implementations in
this class
- Javadoc @param updates
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/ArrayUtils.java | 90 +++++++++++++---------
.../org/apache/commons/lang3/ArrayUtilsTest.java | 90 ++++++++++++++++++++++
3 files changed, 145 insertions(+), 36 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b93e99d19..56bb33eec 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -91,6 +91,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1805" type="fix" dev="ggregory" due-to="Gary Gregory,
jher235">Speedup StringUtils.join(AllPrimitiveTypes[], char, int, int), see
StringUtilsJoinBenchmark, based on #1532. Also better StringBuilder
allocation</action>
<action issue="LANG-1749" type="fix" dev="ggregory" due-to="jinwoo choi,
Gary Gregory, YuMing Ma, Ivan Šarić">TypeUtils.isAssignable returns a wrong
result for ParameterizedType when raw class is "Class" #1548.</action>
<action issue="LANG-1700" type="fix" dev="ggregory" due-to="seokhyeon
moon, Gary Gregory, Ivan Šarić">Improve handling of parameterized types and
variable unrolling.</action>
+ <action issue="LANG-1811" type="fix" dev="ggregory" due-to="Raju Gupta,
Gary Gregory">ArrayUtils.shuffle() throws NullPointerException for null array
input.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add JavaVersion.JAVA_27.</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add SystemUtils.IS_JAVA_27.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index cbd3fe2f6..4369d4f7e 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -7428,14 +7428,16 @@ public static void shuffle(final boolean[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final boolean[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7462,14 +7464,16 @@ public static void shuffle(final byte[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final byte[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7496,14 +7500,16 @@ public static void shuffle(final char[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final char[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7530,14 +7536,16 @@ public static void shuffle(final double[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final double[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7564,14 +7572,16 @@ public static void shuffle(final float[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final float[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7598,14 +7608,16 @@ public static void shuffle(final int[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final int[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7632,14 +7644,16 @@ public static void shuffle(final long[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final long[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7666,14 +7680,16 @@ public static void shuffle(final Object[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final Object[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
@@ -7700,14 +7716,16 @@ public static void shuffle(final short[] array) {
* Shuffles randomly the elements of the specified array using the <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle
* algorithm</a>.
*
- * @param array the array to shuffle.
- * @param random the source of randomness used to permute the elements.
+ * @param array the array to shuffle, no-op if {@code null}.
+ * @param random the source of randomness used to permute the elements,
no-op if {@code null}.
* @see <a
href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates
shuffle algorithm</a>
* @since 3.6
*/
public static void shuffle(final short[] array, final Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i), 1);
+ if (array != null && random != null) {
+ for (int i = array.length; i > 1; i--) {
+ swap(array, i - 1, random.nextInt(i), 1);
+ }
}
}
diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
index 355ccbee0..6aefafa7b 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
@@ -5199,6 +5199,8 @@ void testShiftShortArrayNull() {
@Test
void testShuffleBoolean() {
+ ArrayUtils.shuffle((boolean[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_BOOLEAN_ARRAY);
// Don't use a "small" array to reduce spurious failures.
final boolean[] array1 = { true, false, true, true, false, false,
true, false, false, true, true, false, true, true, false, false, true, false,
false,
true, true, false, true, true, false, false, true, false,
false, true, false, true, true, false, true, true, false, false, true, false,
false,
@@ -5210,17 +5212,27 @@ void testShuffleBoolean() {
@Test
void testShuffleBooleanRandom() {
+ ArrayUtils.shuffle((boolean[]) null, null);
+ ArrayUtils.shuffle((boolean[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_BOOLEAN_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_BOOLEAN_ARRAY, seededRandom());
// Don't use a "small" array to reduce spurious failures.
final boolean[] array1 = { true, false, true, true, false, false,
true, false, false, true, true, false, true, true, false, false, true, false,
false,
true, true, false, true, true, false, false, true, false,
false, true, false, true, true, false, true, true, false, false, true, false,
false,
false, true, true, false, false, true, false, false, true,
false, true, true, false, true, true, false, false, true, false };
final boolean[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
}
@Test
void testShuffleByte() {
+ ArrayUtils.shuffle((byte[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_BYTE_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final byte[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
@@ -5235,11 +5247,19 @@ void testShuffleByte() {
@Test
void testShuffleByteRandom() {
+ ArrayUtils.shuffle((byte[]) null, null);
+ ArrayUtils.shuffle((byte[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_BOOLEAN_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_BOOLEAN_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final byte[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final byte[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
for (final byte element : array2) {
@@ -5249,6 +5269,8 @@ void testShuffleByteRandom() {
@Test
void testShuffleChar() {
+ ArrayUtils.shuffle((char[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_CHAR_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final char[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
@@ -5263,11 +5285,19 @@ void testShuffleChar() {
@Test
void testShuffleCharRandom() {
+ ArrayUtils.shuffle((char[]) null, null);
+ ArrayUtils.shuffle((char[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_CHAR_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_CHAR_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final char[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final char[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
for (final char element : array2) {
@@ -5277,6 +5307,8 @@ void testShuffleCharRandom() {
@Test
void testShuffleDouble() {
+ ArrayUtils.shuffle((double[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_DOUBLE_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final double[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
@@ -5291,11 +5323,19 @@ void testShuffleDouble() {
@Test
void testShuffleDoubleRandom() {
+ ArrayUtils.shuffle((double[]) null, null);
+ ArrayUtils.shuffle((double[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_DOUBLE_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_DOUBLE_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final double[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final double[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
for (final double element : array2) {
@@ -5305,6 +5345,8 @@ void testShuffleDoubleRandom() {
@Test
void testShuffleFloat() {
+ ArrayUtils.shuffle((float[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_FLOAT_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final float[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
@@ -5319,11 +5361,19 @@ void testShuffleFloat() {
@Test
void testShuffleFloatRandom() {
+ ArrayUtils.shuffle((float[]) null, null);
+ ArrayUtils.shuffle((float[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_FLOAT_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_FLOAT_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final float[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final float[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
for (final float element : array2) {
@@ -5333,6 +5383,8 @@ void testShuffleFloatRandom() {
@Test
void testShuffleInt() {
+ ArrayUtils.shuffle((int[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_INT_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final int[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
@@ -5347,11 +5399,19 @@ void testShuffleInt() {
@Test
void testShuffleIntRandom() {
+ ArrayUtils.shuffle((int[]) null, null);
+ ArrayUtils.shuffle((int[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_INT_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_INT_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final int[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final int[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1);
assertFalse(Arrays.equals(array1, array2));
for (final int element : array2) {
@@ -5361,6 +5421,8 @@ void testShuffleIntRandom() {
@Test
void testShuffleLong() {
+ ArrayUtils.shuffle((long[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_LONG_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final long[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
@@ -5375,11 +5437,19 @@ void testShuffleLong() {
@Test
void testShuffleLongRandom() {
+ ArrayUtils.shuffle((long[]) null, null);
+ ArrayUtils.shuffle((long[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_LONG_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_LONG_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final long[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final long[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
for (final long element : array2) {
@@ -5389,6 +5459,8 @@ void testShuffleLongRandom() {
@Test
void testShuffleShort() {
+ ArrayUtils.shuffle((short[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_SHORT_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final short[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
@@ -5403,11 +5475,19 @@ void testShuffleShort() {
@Test
void testShuffleShortRandom() {
+ ArrayUtils.shuffle((short[]) null, null);
+ ArrayUtils.shuffle((short[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_SHORT_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_SHORT_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final short[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final short[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
for (final short element : array2) {
@@ -5417,6 +5497,8 @@ void testShuffleShortRandom() {
@Test
void testShuffleString() {
+ ArrayUtils.shuffle((String[]) null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_STRING_ARRAY);
// Don't use a "small" array, reduce spurious failures.
final String[] array1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "0", "1",
"2",
"3", "4", "5", "6", "7", "8", "9", "10", "0", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "0", "1", "2", "3", "4", "5", "6", "7", "8",
@@ -5431,11 +5513,19 @@ void testShuffleString() {
@Test
void testShuffleStringRandom() {
+ ArrayUtils.shuffle((String[]) null, null);
+ ArrayUtils.shuffle((String[]) null, seededRandom());
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_STRING_ARRAY, null);
+ ArrayUtils.shuffle(ArrayUtils.EMPTY_STRING_ARRAY, seededRandom());
// Don't use a "small" array, reduce spurious failures.
final String[] array1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "0", "1",
"2",
"3", "4", "5", "6", "7", "8", "9", "10", "0", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
final String[] array2 = ArrayUtils.clone(array1);
+ // no shuffle
+ ArrayUtils.shuffle(array1, null);
+ assertTrue(Arrays.equals(array1, array2));
+ // actual shuffle
ArrayUtils.shuffle(array1, seededRandom());
assertFalse(Arrays.equals(array1, array2));
for (final String element : array2) {