diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java index d547ade..8ad67ee 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java @@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.commons.util; import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -260,12 +261,7 @@ public class ArrayTools { */ public static boolean isAllElementsNotNull(@Nonnull final T[] arr) { AssertTools.checkArgument(arr != null, "The array cannot be null."); - for (T element : arr) { - if (element == null) { - return false; - } - } - return true; + return Arrays.stream(arr).allMatch(Objects::nonNull); } // #endregion @@ -626,7 +622,7 @@ public class ArrayTools { // fill - char - public static void fill(char[] a, char... values) { + public static void fill(char[] a, char[] values) { fill(a, 0, a.length, values); } @@ -634,9 +630,9 @@ public class ArrayTools { fill(a, 0, a.length, values != null ? values.toCharArray() : EMPTY_CHAR_ARRAY); } - public static void fill(char[] a, int fromIndex, int toIndex, char... values) { + public static void fill(char[] a, int fromIndex, int toIndex, char[] values) { AssertTools.checkArgument(Objects.nonNull(a)); - if (values.length == 0) { + if (values == null || values.length == 0) { return; } final int start = Integer.max(fromIndex, 0); @@ -659,13 +655,13 @@ public class ArrayTools { // fill - byte - public static void fill(byte[] a, byte... values) { + public static void fill(byte[] a, byte[] values) { fill(a, 0, a.length, values); } - public static void fill(byte[] a, int fromIndex, int toIndex, byte... values) { + public static void fill(byte[] a, int fromIndex, int toIndex, byte[] values) { AssertTools.checkArgument(Objects.nonNull(a)); - if (values.length == 0) { + if (values == null || values.length == 0) { return; } final int start = Integer.max(fromIndex, 0); @@ -688,13 +684,13 @@ public class ArrayTools { // fill - short - public static void fill(short[] a, short... values) { + public static void fill(short[] a, short[] values) { fill(a, 0, a.length, values); } - public static void fill(short[] a, int fromIndex, int toIndex, short... values) { + public static void fill(short[] a, int fromIndex, int toIndex, short[] values) { AssertTools.checkArgument(Objects.nonNull(a)); - if (values.length == 0) { + if (values == null || values.length == 0) { return; } final int start = Integer.max(fromIndex, 0); @@ -717,13 +713,13 @@ public class ArrayTools { // fill - int - public static void fill(int[] a, int... values) { + public static void fill(int[] a, int[] values) { fill(a, 0, a.length, values); } - public static void fill(int[] a, int fromIndex, int toIndex, int... values) { + public static void fill(int[] a, int fromIndex, int toIndex, int[] values) { AssertTools.checkArgument(Objects.nonNull(a)); - if (values.length == 0) { + if (values == null || values.length == 0) { return; } final int start = Integer.max(fromIndex, 0); @@ -746,13 +742,13 @@ public class ArrayTools { // fill - long - public static void fill(long[] a, long... values) { + public static void fill(long[] a, long[] values) { fill(a, 0, a.length, values); } - public static void fill(long[] a, int fromIndex, int toIndex, long... values) { + public static void fill(long[] a, int fromIndex, int toIndex, long[] values) { AssertTools.checkArgument(Objects.nonNull(a)); - if (values.length == 0) { + if (values == null || values.length == 0) { return; } final int start = Integer.max(fromIndex, 0); @@ -775,13 +771,13 @@ public class ArrayTools { // fill - float - public static void fill(float[] a, float... values) { + public static void fill(float[] a, float[] values) { fill(a, 0, a.length, values); } - public static void fill(float[] a, int fromIndex, int toIndex, float... values) { + public static void fill(float[] a, int fromIndex, int toIndex, float[] values) { AssertTools.checkArgument(Objects.nonNull(a)); - if (values.length == 0) { + if (values == null || values.length == 0) { return; } final int start = Integer.max(fromIndex, 0); @@ -804,13 +800,13 @@ public class ArrayTools { // fill - double - public static void fill(double[] a, double... values) { + public static void fill(double[] a, double[] values) { fill(a, 0, a.length, values); } - public static void fill(double[] a, int fromIndex, int toIndex, double... values) { + public static void fill(double[] a, int fromIndex, int toIndex, double[] values) { AssertTools.checkArgument(Objects.nonNull(a)); - if (values.length == 0) { + if (values == null || values.length == 0) { return; } final int start = Integer.max(fromIndex, 0); @@ -868,7 +864,7 @@ public class ArrayTools { // #region - indexOf - public static int indexOf(T[] arr, Predicate predicate) { + public static int indexOfWithPredicate(T[] arr, Predicate predicate) { AssertTools.checkNotNull(predicate); if (isNullOrEmpty(arr)) { return NOT_FOUND_INDEX; @@ -882,7 +878,7 @@ public class ArrayTools { } public static int indexOf(T[] arr, T obj) { - return indexOf(arr, item -> Objects.equals(item, obj)); + return indexOfWithPredicate(arr, item -> Objects.equals(item, obj)); } public static int indexOf(char[] arr, char value) { @@ -973,7 +969,7 @@ public class ArrayTools { // #region - lastIndexOf - public static int lastIndexOf(T[] arr, @Nonnull Predicate predicate) { + public static int lastIndexOfWithPredicate(T[] arr, @Nonnull Predicate predicate) { AssertTools.checkNotNull(predicate); if (isNullOrEmpty(arr)) { return NOT_FOUND_INDEX; @@ -987,7 +983,7 @@ public class ArrayTools { } public static int lastIndexOf(T[] arr, T obj) { - return lastIndexOf(arr, item -> Objects.equals(item, obj)); + return lastIndexOfWithPredicate(arr, item -> Objects.equals(item, obj)); } public static int lastIndexOf(char[] arr, char value) { @@ -1111,7 +1107,7 @@ public class ArrayTools { } public static boolean containsValue(BigDecimal[] arr, BigDecimal obj) { - return indexOf(arr, item -> BigDecimals.equalsValue(item, obj)) > NOT_FOUND_INDEX; + return indexOfWithPredicate(arr, item -> BigDecimals.equalsValue(item, obj)) > NOT_FOUND_INDEX; } // #endregion diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java index ceafaf1..32423a1 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java @@ -27,44 +27,63 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.function.Predicate; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.function.Executable; @SuppressWarnings("null") public class ArrayToolsTests { - // TODO 【优化】 检查、完善测试用例 + static final String[] NULL_STRING_ARRAY = null; + static final Integer[] NULL_INTEGER_ARRAY = null; + static final char[] NULL_CHAR_ARRAY = null; + static final byte[] NULL_BYTE_ARRAY = null; + static final short[] NULL_SHORT_ARRAY = null; + static final int[] NULL_INT_ARRAY = null; + static final long[] NULL_LONG_ARRAY = null; + static final float[] NULL_FLOAT_ARRAY = null; + static final double[] NULL_DOUBLE_ARRAY = null; - //#region null or empty + static final String[] EMPTY_STRING_ARRAY = {}; + static final Integer[] EMPTY_INTEGER_ARRAY = {}; + static final char[] EMPTY_CHAR_ARRAY = {}; + static final byte[] EMPTY_BYTE_ARRAY = {}; + static final short[] EMPTY_SHORT_ARRAY = {}; + static final int[] EMPTY_INT_ARRAY = {}; + static final long[] EMPTY_LONG_ARRAY = {}; + static final float[] EMPTY_FLOAT_ARRAY = {}; + static final double[] EMPTY_DOUBLE_ARRAY = {}; + + // ================================ + // #region - isNullOrEmpty + // ================================ @Test void isNullOrEmpty_NullArray_ReturnsTrue() { assertAll( - () -> assertTrue(ArrayTools.isNullOrEmpty((String[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((Integer[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((char[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((byte[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((short[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((int[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((long[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((float[]) null)), - () -> assertTrue(ArrayTools.isNullOrEmpty((double[]) null))); + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_STRING_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_INTEGER_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_CHAR_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_BYTE_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_SHORT_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_INT_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_LONG_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_FLOAT_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(NULL_DOUBLE_ARRAY))); } @Test void isNullOrEmpty_EmptyArray_ReturnsTrue() { assertAll( - () -> assertTrue(ArrayTools.isNullOrEmpty(new String[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new Integer[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new char[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new byte[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new short[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new int[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new long[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new float[] {})), - () -> assertTrue(ArrayTools.isNullOrEmpty(new double[] {}))); + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_STRING_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_INTEGER_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_CHAR_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_BYTE_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_SHORT_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_INT_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_LONG_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_FLOAT_ARRAY)), + () -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_DOUBLE_ARRAY))); } @Test @@ -81,32 +100,40 @@ public class ArrayToolsTests { () -> assertFalse(ArrayTools.isNullOrEmpty(new double[] { 1 }))); } + // ================================ + // #endregion - isNullOrEmpty + // ================================ + + // ================================ + // #region - isNotEmpty + // ================================ + @Test void isNotEmpty_NullArray_ReturnsFalse() { assertAll( - () -> assertFalse(ArrayTools.isNotEmpty((String[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((Integer[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((char[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((byte[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((short[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((int[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((long[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((float[]) null)), - () -> assertFalse(ArrayTools.isNotEmpty((double[]) null))); + () -> assertFalse(ArrayTools.isNotEmpty(NULL_STRING_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_INTEGER_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_CHAR_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_BYTE_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_SHORT_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_INT_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_LONG_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_FLOAT_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(NULL_DOUBLE_ARRAY))); } @Test void isNotEmpty_EmptyArray_ReturnsFalse() { assertAll( - () -> assertFalse(ArrayTools.isNotEmpty(new String[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new Integer[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new char[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new byte[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new short[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new int[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new long[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new float[] {})), - () -> assertFalse(ArrayTools.isNotEmpty(new double[] {}))); + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_STRING_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_INTEGER_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_CHAR_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_BYTE_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_SHORT_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_INT_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_LONG_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_FLOAT_ARRAY)), + () -> assertFalse(ArrayTools.isNotEmpty(EMPTY_DOUBLE_ARRAY))); } @Test @@ -123,16 +150,24 @@ public class ArrayToolsTests { () -> assertTrue(ArrayTools.isNotEmpty(new double[] { 1 }))); } + // ================================ + // #endregion - isNotEmpty + // ================================ + + // ================================ + // #region - isAllElementsNotNull + // ================================ + @Test void isAllElementsNotNull_NullArray_ThrowsException() { - assertThrows(IllegalArgumentException.class, () -> ArrayTools.isAllElementsNotNull((String[]) null)); - assertThrows(IllegalArgumentException.class, () -> ArrayTools.isAllElementsNotNull((Integer[]) null)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.isAllElementsNotNull(NULL_STRING_ARRAY)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.isAllElementsNotNull(NULL_INTEGER_ARRAY)); } @Test void isAllElementsNotNull_EmptyArray_ReturnsTrue() { - assertTrue(ArrayTools.isAllElementsNotNull(new String[] {})); - assertTrue(ArrayTools.isAllElementsNotNull(new Integer[] {})); + assertTrue(ArrayTools.isAllElementsNotNull(EMPTY_STRING_ARRAY)); + assertTrue(ArrayTools.isAllElementsNotNull(EMPTY_INTEGER_ARRAY)); } @Test @@ -147,155 +182,122 @@ public class ArrayToolsTests { assertTrue(ArrayTools.isAllElementsNotNull(new Integer[] { 1, 2 })); } - // #endregion + // ================================ + // #endregion - isAllElementsNotNull + // ================================ + // ================================ // #region - concat + // ================================ - private static List charArrays; - private static List byteArrays; - private static List shortArrays; - private static List intArrays; - private static List longArrays; - private static List floatArrays; - private static List doubleArrays; + static final List charArrays; + static final List byteArrays; + static final List shortArrays; + static final List intArrays; + static final List longArrays; + static final List floatArrays; + static final List doubleArrays; - @BeforeAll - public static void setUp() { + static { charArrays = new ArrayList<>(); + charArrays.add(null); + charArrays.add(new char[0]); charArrays.add(new char[] { 'a', 'b' }); charArrays.add(new char[] { 'c', 'd', 'e' }); - charArrays.add(null); byteArrays = new ArrayList<>(); + byteArrays.add(null); + byteArrays.add(new byte[0]); byteArrays.add(new byte[] { 1, 2 }); byteArrays.add(new byte[] { 3, 4, 5 }); - byteArrays.add(null); shortArrays = new ArrayList<>(); + shortArrays.add(null); + shortArrays.add(new short[0]); shortArrays.add(new short[] { 10, 20 }); shortArrays.add(new short[] { 30, 40, 50 }); - shortArrays.add(null); intArrays = new ArrayList<>(); + intArrays.add(null); + intArrays.add(new int[0]); intArrays.add(new int[] { 100, 200 }); intArrays.add(new int[] { 300, 400, 500 }); - intArrays.add(null); longArrays = new ArrayList<>(); + longArrays.add(null); + longArrays.add(new long[0]); longArrays.add(new long[] { 1000L, 2000L }); longArrays.add(new long[] { 3000L, 4000L, 5000L }); - longArrays.add(null); floatArrays = new ArrayList<>(); + floatArrays.add(null); + floatArrays.add(new float[0]); floatArrays.add(new float[] { 1000.1f, 2000.2f }); floatArrays.add(new float[] { 3000.3f, 4000.4f, 5000.5f }); - floatArrays.add(null); doubleArrays = new ArrayList<>(); + doubleArrays.add(null); + doubleArrays.add(new double[0]); doubleArrays.add(new double[] { 1000.1d, 2000.2d }); doubleArrays.add(new double[] { 3000.3d, 4000.4d, 5000.5d }); - doubleArrays.add(null); } @Test - public void testConcatCharArray_NullOrEmptyCollection_ReturnsEmptyArray() { - assertArrayEquals(new char[] {}, ArrayTools.concatCharArray(null)); + void concat_NullOrEmptyCollection_ReturnsEmptyArray() { + assertEquals(0, ArrayTools.concatCharArray(null).length); assertEquals(0, ArrayTools.concatCharArray(Collections.emptyList()).length); - } - @Test - public void testConcatCharArray_ValidCollection_ReturnsConcatenatedArray() { - char[] expected = { 'a', 'b', 'c', 'd', 'e' }; - char[] result = ArrayTools.concatCharArray(charArrays); - assertArrayEquals(expected, result); - } - - @Test - public void testConcatByteArray_NullOrEmptyCollection_ReturnsEmptyArray() { - assertArrayEquals(new byte[0], ArrayTools.concatByteArray(null)); + assertEquals(0, ArrayTools.concatByteArray(null).length); assertEquals(0, ArrayTools.concatByteArray(Collections.emptyList()).length); - } - @Test - public void testConcatByteArray_ValidCollection_ReturnsConcatenatedArray() { - byte[] expected = { 1, 2, 3, 4, 5 }; - byte[] result = ArrayTools.concatByteArray(byteArrays); - assertArrayEquals(expected, result); - } - - @Test - public void testConcatShortArray_NullOrEmptyCollection_ReturnsEmptyArray() { - assertArrayEquals(new short[0], ArrayTools.concatShortArray(null)); + assertEquals(0, ArrayTools.concatShortArray(null).length); assertEquals(0, ArrayTools.concatShortArray(Collections.emptyList()).length); - } - @Test - public void testConcatShortArray_ValidCollection_ReturnsConcatenatedArray() { - short[] expected = { 10, 20, 30, 40, 50 }; - short[] result = ArrayTools.concatShortArray(shortArrays); - assertArrayEquals(expected, result); - } - - @Test - public void testConcatIntArray_NullOrEmptyCollection_ReturnsEmptyArray() { - assertArrayEquals(new int[0], ArrayTools.concatIntArray(null)); + assertEquals(0, ArrayTools.concatIntArray(null).length); assertEquals(0, ArrayTools.concatIntArray(Collections.emptyList()).length); - } - @Test - public void testConcatIntArray_ValidCollection_ReturnsConcatenatedArray() { - int[] expected = { 100, 200, 300, 400, 500 }; - int[] result = ArrayTools.concatIntArray(intArrays); - assertArrayEquals(expected, result); - } - - @Test - public void testConcatLongArray_NullOrEmptyCollection_ReturnsEmptyArray() { - assertArrayEquals(new long[0], ArrayTools.concatLongArray(null)); + assertEquals(0, ArrayTools.concatLongArray(null).length); assertEquals(0, ArrayTools.concatLongArray(Collections.emptyList()).length); - } - @Test - public void testConcatLongArray_ValidCollection_ReturnsConcatenatedArray() { - long[] expected = { 1000L, 2000L, 3000L, 4000L, 5000L }; - long[] result = ArrayTools.concatLongArray(longArrays); - assertArrayEquals(expected, result); - } - - @Test - public void testConcatFloatArray_NullOrEmptyCollection_ReturnsEmptyArray() { - assertArrayEquals(new float[0], ArrayTools.concatFloatArray(null)); + assertEquals(0, ArrayTools.concatFloatArray(null).length); assertEquals(0, ArrayTools.concatFloatArray(Collections.emptyList()).length); - } - @Test - public void testConcatFloatArray_ValidCollection_ReturnsConcatenatedArray() { - float[] expected = { 1000.1f, 2000.2f, 3000.3f, 4000.4f, 5000.5f }; - float[] result = ArrayTools.concatFloatArray(floatArrays); - assertArrayEquals(expected, result); - } - - @Test - public void testConcatDoubleArray_NullOrEmptyCollection_ReturnsEmptyArray() { - assertArrayEquals(new double[] {}, ArrayTools.concatDoubleArray(null)); + assertEquals(0, ArrayTools.concatDoubleArray(null).length); assertEquals(0, ArrayTools.concatDoubleArray(Collections.emptyList()).length); } @Test - public void testConcatDoubleArray_ValidCollection_ReturnsConcatenatedArray() { - double[] expected = { 1000.1d, 2000.2d, 3000.3d, 4000.4d, 5000.5d }; - double[] result = ArrayTools.concatDoubleArray(doubleArrays); - assertArrayEquals(expected, result); + void concat_ValidCollection_ReturnsConcatenatedArray() { + assertArrayEquals(new char[] { 'a', 'b', 'c', 'd', 'e' }, + ArrayTools.concatCharArray(charArrays)); + + assertArrayEquals(new byte[] { 1, 2, 3, 4, 5 }, + ArrayTools.concatByteArray(byteArrays)); + + assertArrayEquals(new short[] { 10, 20, 30, 40, 50 }, + ArrayTools.concatShortArray(shortArrays)); + + assertArrayEquals(new int[] { 100, 200, 300, 400, 500 }, + ArrayTools.concatIntArray(intArrays)); + + assertArrayEquals(new long[] { 1000L, 2000L, 3000L, 4000L, 5000L }, + ArrayTools.concatLongArray(longArrays)); + + assertArrayEquals(new float[] { 1000.1f, 2000.2f, 3000.3f, 4000.4f, 5000.5f }, + ArrayTools.concatFloatArray(floatArrays)); + + assertArrayEquals(new double[] { 1000.1d, 2000.2d, 3000.3d, 4000.4d, 5000.5d }, + ArrayTools.concatDoubleArray(doubleArrays)); } @Test - public void testConcatToList_NullOrEmptyCollection_ReturnsEmptyList() { + void concatToList_NullOrEmptyCollection_ReturnsEmptyList() { assertTrue(ArrayTools.concatToList(null).isEmpty()); assertTrue(ArrayTools.concatToList(Collections.emptyList()).isEmpty()); } @Test - public void testConcatToList_ValidCollection_ReturnsConcatenatedList() { + void concatToList_ValidCollection_ReturnsConcatenatedList() { Character[] charArray1 = { 'a', 'b' }; Character[] charArray2 = { 'c', 'd', 'e' }; List expected = Arrays.asList('a', 'b', 'c', 'd', 'e'); @@ -303,116 +305,786 @@ public class ArrayToolsTests { assertEquals(expected, result); } + // ================================ // #endregion + // ================================ + // ================================ // #region - repeat + // ================================ @Test - void repeat_CharArray_TimesZero_ReturnsEmptyArray() { - char[] input = { 'a', 'b', 'c' }; - char[] result = ArrayTools.repeat(input, 0); - assertArrayEquals(ArrayTools.EMPTY_CHAR_ARRAY, result); + void repeat_NullArray_ThrowsException() { + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_CHAR_ARRAY, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_BYTE_ARRAY, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_SHORT_ARRAY, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_INT_ARRAY, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_LONG_ARRAY, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_FLOAT_ARRAY, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_DOUBLE_ARRAY, 2)); + + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_CHAR_ARRAY, 2, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_BYTE_ARRAY, 2, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_SHORT_ARRAY, 2, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_INT_ARRAY, 2, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_LONG_ARRAY, 2, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_FLOAT_ARRAY, 2, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(NULL_DOUBLE_ARRAY, 2, 2)); } @Test - void repeat_CharArray_TimesOne_ReturnsSameArray() { - char[] input = { 'a', 'b', 'c' }; - char[] result = ArrayTools.repeat(input, 1); - assertArrayEquals(input, result); + void repeat_NegativeTimes_ThrowsException() { + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new char[]{ 'a' }, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new byte[]{ 1 }, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new short[]{ 1 }, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new int[]{ 1 }, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new long[]{ 1 }, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new float[]{ 1 }, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new double[]{ 1 }, -1)); + + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new char[]{ 'a' }, -1, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new byte[]{ 1 }, -1, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new short[]{ 1 }, -1, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new int[]{ 1 }, -1, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new long[]{ 1 }, -1, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new float[]{ 1 }, -1, 2)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new double[]{ 1 }, -1, 2)); } @Test - void repeat_CharArray_TimesTwo_ReturnsRepeatedArray() { - char[] input = { 'a', 'b', 'c' }; - char[] expected = { 'a', 'b', 'c', 'a', 'b', 'c' }; - char[] result = ArrayTools.repeat(input, 2); - assertArrayEquals(expected, result); + void repeat_NegativeMaxLength_ThrowsException() { + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new char[]{ 'a' }, 2, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new byte[]{ 1 }, 2, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new short[]{ 1 }, 2, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new int[]{ 1 }, 2, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new long[]{ 1 }, 2, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new float[]{ 1 }, 2, -1)); + assertThrows(IllegalArgumentException.class, () -> ArrayTools.repeat(new double[]{ 1 }, 2, -1)); } @Test - void repeat_CharArray_WithMaxLength_ReturnsTruncatedArray() { - char[] input = { 'a', 'b', 'c' }; - char[] expected = { 'a', 'b', 'c', 'a', 'b' }; - char[] result = ArrayTools.repeat(input, 2, 5); - assertArrayEquals(expected, result); + void repeat_ZeroTimes_ReturnsEmptyArray() { + assertEquals(0, ArrayTools.repeat(new char[]{ 'a' }, 0).length); + assertEquals(0, ArrayTools.repeat(new byte[]{ 1 }, 0).length); + assertEquals(0, ArrayTools.repeat(new short[]{ 1 }, 0).length); + assertEquals(0, ArrayTools.repeat(new int[]{ 1 }, 0).length); + assertEquals(0, ArrayTools.repeat(new long[]{ 1 }, 0).length); + assertEquals(0, ArrayTools.repeat(new float[]{ 1 }, 0).length); + assertEquals(0, ArrayTools.repeat(new double[]{ 1 }, 0).length); } @Test - void repeat_ByteArray_TimesZero_ReturnsEmptyArray() { - byte[] input = { 1, 2, 3 }; - byte[] result = ArrayTools.repeat(input, 0); - assertArrayEquals(ArrayTools.EMPTY_BYTE_ARRAY, result); + void repeat_NormalCase_RepeatsArray() { + assertArrayEquals(new char[] { 'a', 'b', 'a', 'b', 'a', 'b' }, ArrayTools.repeat(new char[] { 'a', 'b' }, 3)); + assertArrayEquals(new byte[] { 1, 2, 1, 2, 1, 2 }, ArrayTools.repeat(new byte[] { 1, 2 }, 3)); + assertArrayEquals(new short[] { 1, 2, 1, 2, 1, 2 }, ArrayTools.repeat(new short[] { 1, 2 }, 3)); + assertArrayEquals(new int[] { 1, 2, 1, 2, 1, 2 }, ArrayTools.repeat(new int[] { 1, 2 }, 3)); + assertArrayEquals(new long[] { 1L, 2L, 1L, 2L, 1L, 2L }, ArrayTools.repeat(new long[] { 1L, 2L }, 3)); + assertArrayEquals(new float[] { 1.1F, 2.2F, 1.1F, 2.2F, 1.1F, 2.2F }, ArrayTools.repeat(new float[] { 1.1F, 2.2F }, 3)); + assertArrayEquals(new double[] { 1.12, 2.23, 1.12, 2.23, 1.12, 2.23 }, ArrayTools.repeat(new double[] { 1.12, 2.23 }, 3)); } @Test - void repeat_ShortArray_TimesZero_ReturnsEmptyArray() { - short[] input = { 1, 2, 3 }; - short[] result = ArrayTools.repeat(input, 0); - assertArrayEquals(ArrayTools.EMPTY_SHORT_ARRAY, result); - } - - @Test - void repeat_IntArray_TimesZero_ReturnsEmptyArray() { - int[] input = { 1, 2, 3 }; - int[] result = ArrayTools.repeat(input, 0); - assertArrayEquals(ArrayTools.EMPTY_INT_ARRAY, result); - } - - @Test - void repeat_LongArray_TimesZero_ReturnsEmptyArray() { - long[] input = { 1, 2, 3 }; - long[] result = ArrayTools.repeat(input, 0); - assertArrayEquals(ArrayTools.EMPTY_LONG_ARRAY, result); - } - - @Test - void repeat_FloatArray_TimesZero_ReturnsEmptyArray() { - float[] input = { 1, 2, 3 }; - float[] result = ArrayTools.repeat(input, 0); - assertArrayEquals(ArrayTools.EMPTY_FLOAT_ARRAY, result); - } - - @Test - void repeat_DoubleArray_TimesZero_ReturnsEmptyArray() { - double[] input = { 1, 2, 3 }; - double[] result = ArrayTools.repeat(input, 0); - assertArrayEquals(ArrayTools.EMPTY_DOUBLE_ARRAY, result); - } - - @Test - void repeat_CharArray_ThrowsExceptionForNullArray() { - Executable executable = () -> ArrayTools.repeat((char[]) null, 2); - assertThrows(IllegalArgumentException.class, executable); - } - - @Test - void repeat_CharArray_ThrowsExceptionForNegativeTimes() { - char[] input = { 'a', 'b', 'c' }; - Executable executable = () -> ArrayTools.repeat(input, -1); - assertThrows(IllegalArgumentException.class, executable); - } - - @Test - void repeat_CharArray_ThrowsExceptionForNegativeMaxLength() { - char[] input = { 'a', 'b', 'c' }; - Executable executable = () -> ArrayTools.repeat(input, 2, -1); - assertThrows(IllegalArgumentException.class, executable); + void repeat_WithMaxLength_TruncatesResult() { + assertArrayEquals(new char[] { 'a', 'b', 'a', 'b', 'a' }, ArrayTools.repeat(new char[] { 'a', 'b' }, 3, 5)); + assertArrayEquals(new byte[] { 1, 2, 1, 2, 1 }, ArrayTools.repeat(new byte[] { 1, 2 }, 3, 5)); + assertArrayEquals(new short[] { 1, 2, 1, 2, 1 }, ArrayTools.repeat(new short[] { 1, 2 }, 3, 5)); + assertArrayEquals(new int[] { 1, 2, 1, 2, 1 }, ArrayTools.repeat(new int[] { 1, 2 }, 3, 5)); + assertArrayEquals(new long[] { 1L, 2L, 1L, 2L, 1L }, ArrayTools.repeat(new long[] { 1L, 2L }, 3, 5)); + assertArrayEquals(new float[] { 1.1F, 2.2F, 1.1F, 2.2F, 1.1F }, ArrayTools.repeat(new float[] { 1.1F, 2.2F }, 3, 5)); + assertArrayEquals(new double[] { 1.12, 2.23, 1.12, 2.23, 1.12 }, ArrayTools.repeat(new double[] { 1.12, 2.23 }, 3, 5)); } + // ================================ // #endregion + // ================================ - // TODO 【添加】 补充测试用例 - + // ================================ // #region - fill - // #endregion + // ================================ + @Test + void fill_WithValues() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, new char[] {'a', 'b', 'c'}); + assertArrayEquals(new char[] { 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a' }, charArray); + ArrayTools.fill(charArray, "abcd"); + assertArrayEquals(new char[] { 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b' }, charArray); + + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, new byte[] { 1, 2, 3 }); + assertArrayEquals(new byte[] { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1 }, byteArray); + + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, new short[] { 1, 2, 3 }); + assertArrayEquals(new short[] { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1 }, shortArray); + + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, new int[] { 1, 2, 3 }); + assertArrayEquals(new int[] { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1 }, intArray); + + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, new long[] { 1, 2, 3 }); + assertArrayEquals(new long[] { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1 }, longArray); + + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, new float[] { 1.1F, 2.2F, 3.3F }); + assertArrayEquals(new float[] { 1.1F, 2.2F, 3.3F, 1.1F, 2.2F, 3.3F, 1.1F, 2.2F, 3.3F, 1.1F }, floatArray); + + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, new double[] { 1.1, 2.2, 3.3 }); + assertArrayEquals(new double[] { 1.1, 2.2, 3.3, 1.1, 2.2, 3.3, 1.1, 2.2, 3.3, 1.1 }, doubleArray); + + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, new String[] { "aa", "bb", "cc" }); + assertArrayEquals(new String[] { "aa", "bb", "cc", "aa", "bb", "cc", "aa", "bb", "cc", "aa" }, stringArray); + } + + @Test + void fill_WithEmptyValues() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, EMPTY_CHAR_ARRAY); + assertArrayEquals(new char[10], charArray); + ArrayTools.fill(charArray, ""); + assertArrayEquals(new char[10], charArray); + + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, EMPTY_BYTE_ARRAY); + assertArrayEquals(new byte[10], byteArray); + + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, EMPTY_SHORT_ARRAY); + assertArrayEquals(new short[10], shortArray); + + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, EMPTY_INT_ARRAY); + assertArrayEquals(new int[10], intArray); + + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, EMPTY_LONG_ARRAY); + assertArrayEquals(new long[10], longArray); + + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, EMPTY_FLOAT_ARRAY); + assertArrayEquals(new float[10], floatArray); + + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, EMPTY_DOUBLE_ARRAY); + assertArrayEquals(new double[10], doubleArray); + + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, EMPTY_STRING_ARRAY); + assertArrayEquals(new String[10], stringArray); + } + + @Test + void fill_WithNullValues() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, NULL_CHAR_ARRAY); + assertArrayEquals(new char[10], charArray); + ArrayTools.fill(charArray, (String) null); + assertArrayEquals(new char[10], charArray); + + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, NULL_BYTE_ARRAY); + assertArrayEquals(new byte[10], byteArray); + + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, NULL_SHORT_ARRAY); + assertArrayEquals(new short[10], shortArray); + + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, NULL_INT_ARRAY); + assertArrayEquals(new int[10], intArray); + + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, NULL_LONG_ARRAY); + assertArrayEquals(new long[10], longArray); + + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, NULL_FLOAT_ARRAY); + assertArrayEquals(new float[10], floatArray); + + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, NULL_DOUBLE_ARRAY); + assertArrayEquals(new double[10], doubleArray); + + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, NULL_STRING_ARRAY); + assertArrayEquals(new String[10], stringArray); + } + + @Test + void fill_WithValuesInRange() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, 2, 8, new char[] { 'x', 'y' }); + assertArrayEquals(new char[] { '\0', '\0', 'x', 'y', 'x', 'y', 'x', 'y', '\0', '\0' }, charArray); + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, 2, 8, new byte[] { 1, 2 }); + assertArrayEquals(new byte[] { 0, 0, 1, 2, 1, 2, 1, 2, 0, 0 }, byteArray); + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, 2, 8, new short[] { 1, 2 }); + assertArrayEquals(new short[] { 0, 0, 1, 2, 1, 2, 1, 2, 0, 0 }, shortArray); + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, 2, 8, new int[] { 1, 2 }); + assertArrayEquals(new int[] { 0, 0, 1, 2, 1, 2, 1, 2, 0, 0 }, intArray); + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, 2, 8, new long[] { 1, 2 }); + assertArrayEquals(new long[] { 0, 0, 1, 2, 1, 2, 1, 2, 0, 0 }, longArray); + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, 2, 8, new float[] { 1.1F, 2.2F }); + assertArrayEquals(new float[] { 0F, 0F, 1.1F, 2.2F, 1.1F, 2.2F, 1.1F, 2.2F, 0F, 0F }, floatArray); + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, 2, 8, new double[] { 0.1, 0.2 }); + assertArrayEquals(new double[] { 0.0, 0.0, 0.1, 0.2, 0.1, 0.2, 0.1, 0.2, 0.0, 0.0 }, doubleArray); + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, 2, 8, new String[] { "Java", "C++" }); + assertArrayEquals(new String[] { null, null, "Java", "C++", "Java", "C++", "Java", "C++", null, null }, + stringArray); + } + + @Test + void fill_WithValues_OutOfRange() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, 10, 20, new char[] { 'x', 'y' }); + assertArrayEquals(new char[10], charArray); + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, 10, 20, new byte[] { 1, 2 }); + assertArrayEquals(new byte[10], byteArray); + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, 10, 20, new short[] { 1, 2 }); + assertArrayEquals(new short[10], shortArray); + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, 10, 20, new int[] { 1, 2 }); + assertArrayEquals(new int[10], intArray); + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, 10, 20, new long[] { 1, 2 }); + assertArrayEquals(new long[10], longArray); + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, 10, 20, new float[] { 1.1F, 2.2F }); + assertArrayEquals(new float[10], floatArray); + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, 10, 20, new double[] { 0.1, 0.2 }); + assertArrayEquals(new double[10], doubleArray); + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, 10, 20, new String[] { "Java", "C++" }); + assertArrayEquals(new String[10], stringArray); + } + + @Test + void fill_WithValues_NegativeRange() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, -5, 5, new char[] {'w'}); + assertArrayEquals(new char[]{'w', 'w', 'w', 'w', 'w', '\0', '\0', '\0', '\0', '\0'}, charArray); + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, -5, 5, new byte[] {108}); + assertArrayEquals(new byte[]{108, 108, 108, 108, 108, 0, 0, 0, 0, 0}, byteArray); + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, -5, 5, new short[] {108}); + assertArrayEquals(new short[]{108, 108, 108, 108, 108, 0, 0, 0, 0, 0}, shortArray); + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, -5, 5, new int[] {108}); + assertArrayEquals(new int[]{108, 108, 108, 108, 108, 0, 0, 0, 0, 0}, intArray); + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, -5, 5, new long[] {108L}); + assertArrayEquals(new long[]{108L, 108L, 108L, 108L, 108L, 0L, 0L, 0L, 0L, 0L}, longArray); + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, -5, 5, new float[] {108.01F}); + assertArrayEquals(new float[]{108.01F, 108.01F, 108.01F, 108.01F, 108.01F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F}, floatArray); + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, -5, 5, new double[] {108.01}); + assertArrayEquals(new double[]{108.01, 108.01, 108.01, 108.01, 108.01, 0.0, 0.0, 0.0, 0.0, 0.0}, doubleArray); + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, -5, 5, new String[] {"test"}); + assertArrayEquals(new String[]{"test", "test", "test", "test", "test", null, null, null, null, null}, stringArray); + } + + @Test + void fill_WithValues_ExactRange() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, 0, 10, new char[] {'w'}); + assertArrayEquals(new char[]{'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w'}, charArray); + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, 0, 10, new byte[] {108}); + assertArrayEquals(new byte[]{108, 108, 108, 108, 108, 108, 108, 108, 108, 108}, byteArray); + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, 0, 10, new short[] {108}); + assertArrayEquals(new short[]{108, 108, 108, 108, 108, 108, 108, 108, 108, 108}, shortArray); + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, 0, 10, new int[] {108}); + assertArrayEquals(new int[]{108, 108, 108, 108, 108, 108, 108, 108, 108, 108}, intArray); + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, 0, 10, new long[] {108L}); + assertArrayEquals(new long[]{108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L}, longArray); + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, 0, 10, new float[] {108.01F}); + assertArrayEquals(new float[]{108.01F, 108.01F, 108.01F, 108.01F, 108.01F, 108.01F, 108.01F, 108.01F, 108.01F, 108.01F}, floatArray); + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, 0, 10, new double[] {108.01}); + assertArrayEquals(new double[]{108.01, 108.01, 108.01, 108.01, 108.01, 108.01, 108.01, 108.01, 108.01, 108.01}, doubleArray); + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, 0, 10, new String[] {"test"}); + assertArrayEquals(new String[]{"test", "test", "test", "test", "test", "test", "test", "test", "test", "test"}, stringArray); + } + + @Test + void fill_WithValues_Overlap() { + // fill - char + char[] charArray = new char[10]; + ArrayTools.fill(charArray, 0, 5, new char[] {'a', 'b', 'c', 'd', 'e', 'f'}); + assertArrayEquals(new char[]{'a', 'b', 'c', 'd', 'e', '\0', '\0', '\0', '\0', '\0'}, charArray); + // fill - byte + byte[] byteArray = new byte[10]; + ArrayTools.fill(byteArray, 0, 5, new byte[] {1, 2, 3, 4, 5, 6}); + assertArrayEquals(new byte[]{1, 2, 3, 4, 5, 0, 0, 0, 0, 0}, byteArray); + // fill - short + short[] shortArray = new short[10]; + ArrayTools.fill(shortArray, 0, 5, new short[] {1, 2, 3, 4, 5, 6}); + assertArrayEquals(new short[]{1, 2, 3, 4, 5, 0, 0, 0, 0, 0}, shortArray); + // fill - int + int[] intArray = new int[10]; + ArrayTools.fill(intArray, 0, 5, new int[] {1, 2, 3, 4, 5, 6}); + assertArrayEquals(new int[]{1, 2, 3, 4, 5, 0, 0, 0, 0, 0}, intArray); + // fill - long + long[] longArray = new long[10]; + ArrayTools.fill(longArray, 0, 5, new long[] {1L, 2L, 3L, 4L, 5L, 6L}); + assertArrayEquals(new long[]{1L, 2L, 3L, 4L, 5L, 0L, 0L, 0L, 0L, 0L}, longArray); + // fill - float + float[] floatArray = new float[10]; + ArrayTools.fill(floatArray, 0, 5, new float[] {1.1F, 2.2F, 3.3F, 4.4F, 5.5F, 6.6F}); + assertArrayEquals(new float[]{1.1F, 2.2F, 3.3F, 4.4F, 5.5F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F}, floatArray); + // fill - double + double[] doubleArray = new double[10]; + ArrayTools.fill(doubleArray, 0, 5, new double[] {1.1, 2.2, 3.3, 4.4, 5.5, 6.6}); + assertArrayEquals(new double[]{1.1, 2.2, 3.3, 4.4, 5.5, 0.0, 0.0, 0.0, 0.0, 0.0}, doubleArray); + // fill - T + String[] stringArray = new String[10]; + ArrayTools.fill(stringArray, 0, 5, new String[] {"aaa", "bbb", "ccc", "ddd", "eee", "fff"}); + assertArrayEquals(new String[]{"aaa", "bbb", "ccc", "ddd", "eee", null, null, null, null, null}, stringArray); + } + + // ================================ + // #endregion + // ================================ + + // ================================ // #region - indexOf - // #endregion + // ================================ + @Test + void indexOfWithPredicate_NullPredicate_ThrowsNullPointerException() { + assertThrows(NullPointerException.class, () -> ArrayTools.indexOfWithPredicate(new String[] {}, null)); + } + + @Test + void indexOfWithPredicate_NullArray_ReturnsNotFoundIndex() { + Predicate predicate = s -> s.equals("test"); + int result = ArrayTools.indexOfWithPredicate(null, predicate); + assertEquals(ArrayTools.NOT_FOUND_INDEX, result); + } + + @Test + void indexOfWithPredicate_EmptyArray_ReturnsNotFoundIndex() { + Predicate predicate = s -> s.equals("test"); + int result = ArrayTools.indexOfWithPredicate(new String[] {}, predicate); + assertEquals(ArrayTools.NOT_FOUND_INDEX, result); + } + + @Test + void indexOfWithPredicate_ArrayContainsMatchingElement_ReturnsIndex() { + String[] array = { "apple", "banana", "cherry" }; + Predicate predicate = s -> s.equals("banana"); + int result = ArrayTools.indexOfWithPredicate(array, predicate); + assertEquals(1, result); + } + + @Test + void indexOfWithPredicate_ArrayDoesNotContainMatchingElement_ReturnsNotFoundIndex() { + String[] array = { "apple", "banana", "cherry" }; + Predicate predicate = s -> s.equals("orange"); + int result = ArrayTools.indexOfWithPredicate(array, predicate); + assertEquals(ArrayTools.NOT_FOUND_INDEX, result); + } + + @Test + void indexOf_NullArray_ReturnsNotFound() { + // T + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((String[]) null, "test")); + // char + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((char[]) null, 'a')); + // byte + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((byte[]) null, (byte) 1)); + // short + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((short[]) null, (short) 1)); + // int + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((int[]) null, 1)); + // long + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((long[]) null, 1L)); + // float + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((float[]) null, 1.23F)); + // double + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf((double[]) null, 1.23)); + } + + @Test + void indexOf_EmptyArray_ReturnsNotFound() { + // T + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new String[] {}, "test")); + // char + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new char[] {}, 'a')); + // byte + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new byte[] {}, (byte) 1)); + // short + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new short[] {}, (short) 1)); + // int + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new int[] {}, 1)); + // long + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new long[] {}, 1L)); + // float + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new float[] {}, 1.23F)); + // double + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new double[] {}, 1.23)); + } + + @Test + void indexOf_ObjectFound_ReturnsIndex() { + // T + assertEquals(1, ArrayTools.indexOf(new String[] { "a", "b", "c" }, "b")); + // char + assertEquals(1, ArrayTools.indexOf(new char[] { 'a', 'b', 'c' }, 'b')); + // byte + assertEquals(1, ArrayTools.indexOf(new byte[] { 1, 2, 3 }, (byte) 2)); + // short + assertEquals(1, ArrayTools.indexOf(new short[] { 1, 2, 3 }, (short) 2)); + // int + assertEquals(1, ArrayTools.indexOf(new int[] { 1, 2, 3 }, 2)); + // long + assertEquals(1, ArrayTools.indexOf(new long[] { 1000000L, 2000000L, 3000000L }, 2000000L)); + // float + assertEquals(1, ArrayTools.indexOf(new float[] { 1.11F, 2.22F, 3.33F }, 2.22F)); + // double + assertEquals(1, ArrayTools.indexOf(new double[] { 1.11, 2.22, 3.33 }, 2.22)); + } + + @Test + void indexOf_ObjectNotFound_ReturnsNotFound() { + // T + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new String[] { "a", "b", "c" }, "d")); + // char + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new char[] { 'a', 'b', 'c' }, 'd')); + // byte + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new byte[] { 1, 2, 3 }, (byte) 4)); + // short + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new short[] { 1, 2, 3 }, (short) 4)); + // int + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new int[] { 1, 2, 3 }, 4)); + // long + assertEquals(ArrayTools.NOT_FOUND_INDEX, + ArrayTools.indexOf(new long[] { 1000000L, 2000000L, 3000000L }, 4000000L)); + // float + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new float[] { 1.11F, 2.22F, 3.33F }, 4.44F)); + // double + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new double[] { 1.11, 2.22, 3.33 }, 4.44)); + } + + @Test + void indexOf_NullObject_ReturnsNotFound() { + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.indexOf(new String[] { "a", "b", "c" }, null)); + } + + @Test + void indexOf_ArrayContainsNull_ReturnsIndex() { + assertEquals(1, ArrayTools.indexOf(new String[] { "a", null, "c" }, null)); + } + + // ================================ + // #endregion + // ================================ + + // ================================ // #region - lastIndexOf - // #endregion + // ================================ - // #region - contains + @Test + void lastIndexOfWithPredicate_NullPredicate_ThrowsNullPointerException() { + assertThrows(NullPointerException.class, () -> ArrayTools.lastIndexOfWithPredicate(new String[] {}, null)); + } + + @Test + void lastIndexOfWithPredicate_NullArray_ReturnsNotFoundIndex() { + Predicate predicate = s -> s.equals("test"); + int result = ArrayTools.lastIndexOfWithPredicate(null, predicate); + assertEquals(ArrayTools.NOT_FOUND_INDEX, result); + } + + @Test + void lastIndexOfWithPredicate_EmptyArray_ReturnsNotFoundIndex() { + Predicate predicate = s -> s.equals("test"); + int result = ArrayTools.lastIndexOfWithPredicate(new String[] {}, predicate); + assertEquals(ArrayTools.NOT_FOUND_INDEX, result); + } + + @Test + void lastIndexOfWithPredicate_ArrayContainsMatchingElement_ReturnsIndex() { + String[] array = { "apple", "banana", "banana", "cherry" }; + Predicate predicate = s -> s.equals("banana"); + int result = ArrayTools.lastIndexOfWithPredicate(array, predicate); + assertEquals(2, result); + } + + @Test + void lastIndexOfWithPredicate_ArrayDoesNotContainMatchingElement_ReturnsNotFoundIndex() { + String[] array = { "apple", "banana", "cherry" }; + Predicate predicate = s -> s.equals("orange"); + int result = ArrayTools.lastIndexOfWithPredicate(array, predicate); + assertEquals(ArrayTools.NOT_FOUND_INDEX, result); + } + + @Test + void lastIndexOf_NullArray_ReturnsNotFound() { + // T + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((String[]) null, "test")); + // char + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((char[]) null, 'a')); + // byte + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((byte[]) null, (byte) 1)); + // short + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((short[]) null, (short) 1)); + // int + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((int[]) null, 1)); + // long + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((long[]) null, 1L)); + // float + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((float[]) null, 1.23F)); + // double + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf((double[]) null, 1.23)); + } + + @Test + void lastIndexOf_EmptyArray_ReturnsNotFound() { + // T + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new String[] {}, "test")); + // char + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new char[] {}, 'a')); + // byte + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new byte[] {}, (byte) 1)); + // short + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new short[] {}, (short) 1)); + // int + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new int[] {}, 1)); + // long + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new long[] {}, 1L)); + // float + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new float[] {}, 1.23F)); + // double + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new double[] {}, 1.23)); + } + + @Test + void lastIndexOf_ObjectFound_ReturnsIndex() { + // T + assertEquals(3, ArrayTools.lastIndexOf(new String[] { "a", "b", "c", "b", "c" }, "b")); + // char + assertEquals(3, ArrayTools.lastIndexOf(new char[] { 'a', 'b', 'c', 'b', 'c' }, 'b')); + // byte + assertEquals(3, ArrayTools.lastIndexOf(new byte[] { 1, 2, 3, 2, 3 }, (byte) 2)); + // short + assertEquals(3, ArrayTools.lastIndexOf(new short[] { 1, 2, 3, 2, 3 }, (short) 2)); + // int + assertEquals(3, ArrayTools.lastIndexOf(new int[] { 1, 2, 3, 2, 3 }, 2)); + // long + assertEquals(3, + ArrayTools.lastIndexOf(new long[] { 1000000L, 2000000L, 3000000L, 2000000L, 3000000L }, 2000000L)); + // float + assertEquals(3, ArrayTools.lastIndexOf(new float[] { 1.11F, 2.22F, 3.33F, 2.22F, 3.33F }, 2.22F)); + // double + assertEquals(3, ArrayTools.lastIndexOf(new double[] { 1.11, 2.22, 3.33, 2.22, 3.33 }, 2.22)); + } + + @Test + void lastIndexOf_ObjectNotFound_ReturnsNotFound() { + // T + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new String[] { "a", "b", "c" }, "d")); + // char + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new char[] { 'a', 'b', 'c' }, 'd')); + // byte + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new byte[] { 1, 2, 3 }, (byte) 4)); + // short + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new short[] { 1, 2, 3 }, (short) 4)); + // int + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new int[] { 1, 2, 3 }, 4)); + // long + assertEquals(ArrayTools.NOT_FOUND_INDEX, + ArrayTools.lastIndexOf(new long[] { 1000000L, 2000000L, 3000000L }, 4000000L)); + // float + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new float[] { 1.11F, 2.22F, 3.33F }, 4.44F)); + // double + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new double[] { 1.11, 2.22, 3.33 }, 4.44)); + } + + @Test + void lastIndexOf_NullObject_ReturnsNotFound() { + assertEquals(ArrayTools.NOT_FOUND_INDEX, ArrayTools.lastIndexOf(new String[] { "a", "b", "c" }, null)); + } + + @Test + void lastIndexOf_ArrayContainsNull_ReturnsIndex() { + assertEquals(3, ArrayTools.lastIndexOf(new String[] { "a", null, "c", null, "e" }, null)); + } + + // ================================ // #endregion + // ================================ + + // ================================ + // #region - contains + // ================================ + + @Test + void contains_NullArray_ReturnsFalse() { + assertFalse(ArrayTools.contains((String[]) null, "test")); + // char + assertFalse(ArrayTools.contains((char[]) null, 'a')); + // byte + assertFalse(ArrayTools.contains((byte[]) null, (byte) 1)); + // short + assertFalse(ArrayTools.contains((short[]) null, (short) 1)); + // int + assertFalse(ArrayTools.contains((int[]) null, 1)); + // long + assertFalse(ArrayTools.contains((long[]) null, 1L)); + // float + assertFalse(ArrayTools.contains((float[]) null, 1.2F)); + // double + assertFalse(ArrayTools.contains((double[]) null, 1.2)); + } + + @Test + void contains_EmptyArray_ReturnsFalse() { + assertFalse(ArrayTools.contains(new String[] {}, "test")); + // char + assertFalse(ArrayTools.contains(new char[] {}, 'a')); + // byte + assertFalse(ArrayTools.contains(new byte[] {}, (byte) 1)); + // short + assertFalse(ArrayTools.contains(new short[] {}, (short) 1)); + // int + assertFalse(ArrayTools.contains(new int[] {}, 1)); + // long + assertFalse(ArrayTools.contains(new long[] {}, 1L)); + // float + assertFalse(ArrayTools.contains(new float[] {}, 1.2F)); + // double + assertFalse(ArrayTools.contains(new double[] {}, 1.2)); + } + + @Test + void contains_ArrayContainsObject_ReturnsTrue() { + assertTrue(ArrayTools.contains(new String[] { "test", "example" }, "test")); + // char + assertTrue(ArrayTools.contains(new char[] { 'a', 'b', 'c' }, 'a')); + // byte + assertTrue(ArrayTools.contains(new byte[] { 1, 2, 3 }, (byte) 1)); + // short + assertTrue(ArrayTools.contains(new short[] { 1, 2, 3 }, (short) 1)); + // int + assertTrue(ArrayTools.contains(new int[] { 1, 2, 3 }, 1)); + // long + assertTrue(ArrayTools.contains(new long[] { 1000000L, 2000000L, 3000000L }, 1000000L)); + // float + assertTrue(ArrayTools.contains(new float[] { 1.11F, 2.22F, 3.33F }, 2.22F)); + // double + assertTrue(ArrayTools.contains(new double[] { 1.11, 2.22, 3.33 }, 2.22)); + } + + @Test + void contains_ArrayDoesNotContainObject_ReturnsFalse() { + // T + assertFalse(ArrayTools.contains(new String[] { "a", "b", "c" }, "d")); + // char + assertFalse(ArrayTools.contains(new char[] { 'a', 'b', 'c' }, 'd')); + // byte + assertFalse(ArrayTools.contains(new byte[] { 1, 2, 3 }, (byte) 4)); + // short + assertFalse(ArrayTools.contains(new short[] { 1, 2, 3 }, (short) 4)); + // int + assertFalse(ArrayTools.contains(new int[] { 1, 2, 3 }, 4)); + // long + assertFalse(ArrayTools.contains(new long[] { 1000000L, 2000000L, 3000000L }, 4000000L)); + // float + assertFalse(ArrayTools.contains(new float[] { 1.11F, 2.22F, 3.33F }, 4.44F)); + // double + assertFalse(ArrayTools.contains(new double[] { 1.11, 2.22, 3.33 }, 4.44)); + } + + @Test + void contains_ObjectIsNull_ReturnsFalse() { + assertFalse(ArrayTools.contains(new String[] { "test", "example" }, null)); + } + + @Test + void contains_ArrayContainsNull_ReturnsTrue() { + assertTrue(ArrayTools.contains(new String[] { "test", null }, null)); + } + + // ================================ + // #endregion + // ================================ }