ArrayTools:修复 concat 方法的参数 arrays 中存在 null,从而报空指针的问题;补充单元测试

1.x.x
ZhouXY108 2024-12-04 01:14:20 +08:00
parent cca3c8b1ee
commit 65f0bb062c
2 changed files with 485 additions and 60 deletions

View File

@ -23,10 +23,21 @@ import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* ArrayTools
*
* <p>
*
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.1.0
*/
public class ArrayTools {
// #region - empty arrays
@ -267,14 +278,18 @@ public class ArrayTools {
* @param arrays {@code null}
* @return
*/
public static char[] concatCharArray(@Nullable Collection<char[]> arrays) {
public static char[] concatCharArray(@Nullable final Collection<char[]> arrays) {
if (arrays == null || arrays.isEmpty()) {
return new char[0];
}
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final Collection<char[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final char[] result = new char[length];
int i = 0;
for (char[] arr : arrays) {
for (char[] arr : arraysToConcat) {
System.arraycopy(arr, 0, result, i, arr.length);
i += arr.length;
}
@ -287,14 +302,18 @@ public class ArrayTools {
* @param arrays {@code null}
* @return
*/
public static byte[] concatByteArray(@Nullable Collection<byte[]> arrays) {
public static byte[] concatByteArray(@Nullable final Collection<byte[]> arrays) {
if (arrays == null || arrays.isEmpty()) {
return new byte[0];
}
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final Collection<byte[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final byte[] result = new byte[length];
int i = 0;
for (byte[] arr : arrays) {
for (byte[] arr : arraysToConcat) {
System.arraycopy(arr, 0, result, i, arr.length);
i += arr.length;
}
@ -307,14 +326,18 @@ public class ArrayTools {
* @param arrays {@code null}
* @return
*/
public static short[] concatShortArray(@Nullable Collection<short[]> arrays) {
public static short[] concatShortArray(@Nullable final Collection<short[]> arrays) {
if (arrays == null || arrays.isEmpty()) {
return new short[0];
}
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final Collection<short[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final short[] result = new short[length];
int i = 0;
for (short[] arr : arrays) {
for (short[] arr : arraysToConcat) {
System.arraycopy(arr, 0, result, i, arr.length);
i += arr.length;
}
@ -327,14 +350,18 @@ public class ArrayTools {
* @param arrays {@code null}
* @return
*/
public static int[] concatIntArray(@Nullable Collection<int[]> arrays) {
public static int[] concatIntArray(@Nullable final Collection<int[]> arrays) {
if (arrays == null || arrays.isEmpty()) {
return new int[0];
}
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final Collection<int[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final int[] result = new int[length];
int i = 0;
for (int[] arr : arrays) {
for (int[] arr : arraysToConcat) {
System.arraycopy(arr, 0, result, i, arr.length);
i += arr.length;
}
@ -347,14 +374,18 @@ public class ArrayTools {
* @param arrays {@code null}
* @return
*/
public static long[] concatLongArray(@Nullable Collection<long[]> arrays) {
public static long[] concatLongArray(@Nullable final Collection<long[]> arrays) {
if (arrays == null || arrays.isEmpty()) {
return new long[0];
}
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final Collection<long[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final long[] result = new long[length];
int i = 0;
for (long[] arr : arrays) {
for (long[] arr : arraysToConcat) {
System.arraycopy(arr, 0, result, i, arr.length);
i += arr.length;
}
@ -367,14 +398,18 @@ public class ArrayTools {
* @param arrays {@code null}
* @return
*/
public static float[] concatFloatArray(@Nullable Collection<float[]> arrays) {
public static float[] concatFloatArray(@Nullable final Collection<float[]> arrays) {
if (arrays == null || arrays.isEmpty()) {
return new float[0];
}
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final Collection<float[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final float[] result = new float[length];
int i = 0;
for (float[] arr : arrays) {
for (float[] arr : arraysToConcat) {
System.arraycopy(arr, 0, result, i, arr.length);
i += arr.length;
}
@ -387,14 +422,18 @@ public class ArrayTools {
* @param arrays {@code null}
* @return
*/
public static double[] concatDoubleArray(@Nullable Collection<double[]> arrays) {
public static double[] concatDoubleArray(@Nullable final Collection<double[]> arrays) {
if (arrays == null || arrays.isEmpty()) {
return new double[0];
}
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final Collection<double[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final double[] result = new double[length];
int i = 0;
for (double[] arr : arrays) {
for (double[] arr : arraysToConcat) {
System.arraycopy(arr, 0, result, i, arr.length);
i += arr.length;
}
@ -408,17 +447,21 @@ public class ArrayTools {
* @param <T>
* @return
*/
public static <T> List<T> concatToList(@Nullable Collection<T[]> arrays) {
public static <T> List<T> concatToList(@Nullable final Collection<T[]> arrays) {
// 如果输入的集合是否为空,则直接返回一个空列表
if (arrays == null || arrays.isEmpty()) {
return Collections.emptyList();
}
final Collection<T[]> arraysToConcat = arrays
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
// 计算所有数组的总长度,用于初始化列表的容量
final int length = arrays.stream().mapToInt(a -> a.length).sum();
final int length = arraysToConcat.stream().mapToInt(a -> a.length).sum();
final List<T> result = new ArrayList<>(length);
for (T[] arr : arrays) {
for (T[] arr : arraysToConcat) {
Collections.addAll(result, arr);
}
@ -825,7 +868,7 @@ public class ArrayTools {
// #region - indexOf
public static <T> int indexOf(T[] arr, Predicate<? super T> predicate) { // TODO 单元测试
public static <T> int indexOf(T[] arr, Predicate<? super T> predicate) {
AssertTools.checkNotNull(predicate);
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
@ -838,11 +881,11 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int indexOf(T[] arr, T obj) { // TODO 单元测试
public static <T> int indexOf(T[] arr, T obj) {
return indexOf(arr, item -> Objects.equals(item, obj));
}
public static int indexOf(char[] arr, char value) { // TODO 单元测试
public static int indexOf(char[] arr, char value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -854,7 +897,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static int indexOf(byte[] arr, byte value) { // TODO 单元测试
public static int indexOf(byte[] arr, byte value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -866,7 +909,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static int indexOf(short[] arr, short value) { // TODO 单元测试
public static int indexOf(short[] arr, short value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -878,7 +921,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static int indexOf(int[] arr, int value) { // TODO 单元测试
public static int indexOf(int[] arr, int value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -890,7 +933,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static int indexOf(long[] arr, long value) { // TODO 单元测试
public static int indexOf(long[] arr, long value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -902,7 +945,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static int indexOf(float[] arr, float value) { // TODO 单元测试
public static int indexOf(float[] arr, float value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -914,7 +957,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static int indexOf(double[] arr, double value) { // TODO 单元测试
public static int indexOf(double[] arr, double value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -930,7 +973,7 @@ public class ArrayTools {
// #region - lastIndexOf
public static <T> int lastIndexOf(T[] arr, @Nonnull Predicate<? super T> predicate) { // TODO 单元测试
public static <T> int lastIndexOf(T[] arr, @Nonnull Predicate<? super T> predicate) {
AssertTools.checkNotNull(predicate);
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
@ -943,11 +986,11 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int lastIndexOf(T[] arr, T obj) { // TODO 单元测试
public static <T> int lastIndexOf(T[] arr, T obj) {
return lastIndexOf(arr, item -> Objects.equals(item, obj));
}
public static <T> int lastIndexOf(char[] arr, char value) { // TODO 单元测试
public static int lastIndexOf(char[] arr, char value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -959,7 +1002,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int lastIndexOf(byte[] arr, byte value) { // TODO 单元测试
public static int lastIndexOf(byte[] arr, byte value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -971,7 +1014,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int lastIndexOf(short[] arr, short value) { // TODO 单元测试
public static int lastIndexOf(short[] arr, short value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -983,7 +1026,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int lastIndexOf(int[] arr, int value) { // TODO 单元测试
public static int lastIndexOf(int[] arr, int value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -995,7 +1038,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int lastIndexOf(long[] arr, long value) { // TODO 单元测试
public static int lastIndexOf(long[] arr, long value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -1007,7 +1050,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int lastIndexOf(float[] arr, float value) { // TODO 单元测试
public static int lastIndexOf(float[] arr, float value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -1019,7 +1062,7 @@ public class ArrayTools {
return NOT_FOUND_INDEX;
}
public static <T> int lastIndexOf(double[] arr, double value) { // TODO 单元测试
public static int lastIndexOf(double[] arr, double value) {
if (isNullOrEmpty(arr)) {
return NOT_FOUND_INDEX;
}
@ -1035,39 +1078,39 @@ public class ArrayTools {
// #region - contains
public static <T> boolean contains(T[] arr, T obj) { // TODO 单元测试
public static <T> boolean contains(T[] arr, T obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean contains(char[] arr, char obj) { // TODO 单元测试
public static boolean contains(char[] arr, char obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean contains(byte[] arr, byte obj) { // TODO 单元测试
public static boolean contains(byte[] arr, byte obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean contains(short[] arr, short obj) { // TODO 单元测试
public static boolean contains(short[] arr, short obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean contains(int[] arr, int obj) { // TODO 单元测试
public static boolean contains(int[] arr, int obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean contains(long[] arr, long obj) { // TODO 单元测试
public static boolean contains(long[] arr, long obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean contains(float[] arr, float obj) { // TODO 单元测试
public static boolean contains(float[] arr, float obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean contains(double[] arr, double obj) { // TODO 单元测试
public static boolean contains(double[] arr, double obj) {
return indexOf(arr, obj) > NOT_FOUND_INDEX;
}
public static boolean containsValue(BigDecimal[] arr, BigDecimal obj) { // TODO 单元测试
public static boolean containsValue(BigDecimal[] arr, BigDecimal obj) {
return indexOf(arr, item -> BigDecimals.equalsValue(item, obj)) > NOT_FOUND_INDEX;
}

View File

@ -16,21 +16,403 @@
package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@SuppressWarnings("all")
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 【优化】 检查、完善测试用例
//#region null or empty
@Test
void testIsAllNotNull() {
assertTrue(ArrayTools.isAllElementsNotNull(new Object[] { 1L, 2, 3.0, "Java" }));
assertFalse(ArrayTools.isAllElementsNotNull(new Object[] { 1L, 2, 3.0, "Java", null }));
assertFalse(ArrayTools.isAllElementsNotNull(new Object[] { null, 1L, 2, 3.0, "Java" }));
assertTrue(ArrayTools.isAllElementsNotNull(new Object[] {}));
assertThrows(IllegalArgumentException.class,
() -> ArrayTools.isAllElementsNotNull(null));
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)));
}
@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[] {})));
}
@Test
void isNullOrEmpty_NonEmptyArray_ReturnsFalse() {
assertAll(
() -> assertFalse(ArrayTools.isNullOrEmpty(new String[] { "a" })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new Integer[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new char[] { 'a' })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new byte[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new short[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new int[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new long[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new float[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new double[] { 1 })));
}
@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)));
}
@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[] {})));
}
@Test
void isNotEmpty_NonEmptyArray_ReturnsTrue() {
assertAll(
() -> assertTrue(ArrayTools.isNotEmpty(new String[] { "a" })),
() -> assertTrue(ArrayTools.isNotEmpty(new Integer[] { 1 })),
() -> assertTrue(ArrayTools.isNotEmpty(new char[] { 'a' })),
() -> assertTrue(ArrayTools.isNotEmpty(new byte[] { 1 })),
() -> assertTrue(ArrayTools.isNotEmpty(new short[] { 1 })),
() -> assertTrue(ArrayTools.isNotEmpty(new int[] { 1 })),
() -> assertTrue(ArrayTools.isNotEmpty(new long[] { 1 })),
() -> assertTrue(ArrayTools.isNotEmpty(new float[] { 1 })),
() -> assertTrue(ArrayTools.isNotEmpty(new double[] { 1 })));
}
@Test
void isAllElementsNotNull_NullArray_ThrowsException() {
assertThrows(IllegalArgumentException.class, () -> ArrayTools.isAllElementsNotNull((String[]) null));
assertThrows(IllegalArgumentException.class, () -> ArrayTools.isAllElementsNotNull((Integer[]) null));
}
@Test
void isAllElementsNotNull_EmptyArray_ReturnsTrue() {
assertTrue(ArrayTools.isAllElementsNotNull(new String[] {}));
assertTrue(ArrayTools.isAllElementsNotNull(new Integer[] {}));
}
@Test
void isAllElementsNotNull_ArrayWithNullElement_ReturnsFalse() {
assertFalse(ArrayTools.isAllElementsNotNull(new String[] { "a", null }));
assertFalse(ArrayTools.isAllElementsNotNull(new Integer[] { 1, null }));
}
@Test
void isAllElementsNotNull_ArrayWithoutNullElements_ReturnsTrue() {
assertTrue(ArrayTools.isAllElementsNotNull(new String[] { "a", "b" }));
assertTrue(ArrayTools.isAllElementsNotNull(new Integer[] { 1, 2 }));
}
// #endregion
// #region - concat
private static List<char[]> charArrays;
private static List<byte[]> byteArrays;
private static List<short[]> shortArrays;
private static List<int[]> intArrays;
private static List<long[]> longArrays;
private static List<float[]> floatArrays;
private static List<double[]> doubleArrays;
@BeforeAll
public static void setUp() {
charArrays = new ArrayList<>();
charArrays.add(new char[] { 'a', 'b' });
charArrays.add(new char[] { 'c', 'd', 'e' });
charArrays.add(null);
byteArrays = new ArrayList<>();
byteArrays.add(new byte[] { 1, 2 });
byteArrays.add(new byte[] { 3, 4, 5 });
byteArrays.add(null);
shortArrays = new ArrayList<>();
shortArrays.add(new short[] { 10, 20 });
shortArrays.add(new short[] { 30, 40, 50 });
shortArrays.add(null);
intArrays = new ArrayList<>();
intArrays.add(new int[] { 100, 200 });
intArrays.add(new int[] { 300, 400, 500 });
intArrays.add(null);
longArrays = new ArrayList<>();
longArrays.add(new long[] { 1000L, 2000L });
longArrays.add(new long[] { 3000L, 4000L, 5000L });
longArrays.add(null);
floatArrays = new ArrayList<>();
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(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));
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(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(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(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(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(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(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);
}
@Test
public void testConcatToList_NullOrEmptyCollection_ReturnsEmptyList() {
assertTrue(ArrayTools.concatToList(null).isEmpty());
assertTrue(ArrayTools.concatToList(Collections.emptyList()).isEmpty());
}
@Test
public void testConcatToList_ValidCollection_ReturnsConcatenatedList() {
Character[] charArray1 = { 'a', 'b' };
Character[] charArray2 = { 'c', 'd', 'e' };
List<Character> expected = Arrays.asList('a', 'b', 'c', 'd', 'e');
List<Character> result = ArrayTools.concatToList(Arrays.asList(charArray1, charArray2));
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);
}
@Test
void repeat_CharArray_TimesOne_ReturnsSameArray() {
char[] input = { 'a', 'b', 'c' };
char[] result = ArrayTools.repeat(input, 1);
assertArrayEquals(input, result);
}
@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);
}
@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);
}
@Test
void repeat_ByteArray_TimesZero_ReturnsEmptyArray() {
byte[] input = { 1, 2, 3 };
byte[] result = ArrayTools.repeat(input, 0);
assertArrayEquals(ArrayTools.EMPTY_BYTE_ARRAY, result);
}
@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);
}
// #endregion
// TODO 【添加】 补充测试用例
// #region - fill
// #endregion
// #region - indexOf
// #endregion
// #region - lastIndexOf
// #endregion
// #region - contains
// #endregion
}