This commit is contained in:
Looly 2024-04-27 23:18:58 +08:00
parent cabfb74aa5
commit 284bb06416
2 changed files with 56 additions and 1 deletions

View File

@ -1152,7 +1152,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
// endregion
// region ----- indexOf and lastIndexOf and contains
// region ----- indexOf and lastIndexOf
/**
* 返回数组中指定元素所在位置未找到返回{@link #INDEX_NOT_FOUND}

View File

@ -163,12 +163,60 @@ public class ArrayUtilTest {
public void containsAllTest() {
final Integer[] a = {1, 2, 3, 4, 3, 6};
boolean contains = ArrayUtil.containsAll(a, 4, 2, 6);
// 提供的可变参数中元素顺序不需要一致
Assertions.assertTrue(contains);
contains = ArrayUtil.containsAll(a, 1, 2, 3, 5);
Assertions.assertFalse(contains);
}
@Test
void containsIgnoreCaseTest() {
final String[] keys = {"a", "B", "c"};
final boolean b = ArrayUtil.containsIgnoreCase(keys, "b");
Assertions.assertTrue(b);
}
@Test
public void testContainsIgnoreCaseWithEmptyArray() {
final CharSequence[] array = new CharSequence[0];
final CharSequence value = "test";
final boolean result = ArrayUtil.containsIgnoreCase(array, value);
Assertions.assertFalse(result, "Expected the result to be false for an empty array.");
}
@Test
public void testContainsIgnoreCaseWithNullValue() {
final CharSequence[] array = {"Hello", "World"};
final CharSequence value = null;
final boolean result = ArrayUtil.containsIgnoreCase(array, value);
Assertions.assertFalse(result, "Expected the result to be false when the value is null.");
}
@Test
public void testContainsIgnoreCaseWithExistingValue() {
final CharSequence[] array = {"Hello", "World"};
final CharSequence value = "world";
final boolean result = ArrayUtil.containsIgnoreCase(array, value);
Assertions.assertTrue(result, "Expected the result to be true when the value exists in the array.");
}
@Test
public void testContainsIgnoreCaseWithNonExistingValue() {
final CharSequence[] array = {"Hello", "World"};
final CharSequence value = "Java";
final boolean result = ArrayUtil.containsIgnoreCase(array, value);
Assertions.assertFalse(result, "Expected the result to be false when the value does not exist in the array.");
}
@Test
public void testContainsIgnoreCaseWithCaseSensitiveValue() {
final CharSequence[] array = {"Hello", "World"};
final CharSequence value = "HELLO";
final boolean result = ArrayUtil.containsIgnoreCase(array, value);
Assertions.assertTrue(result, "Expected the result to be true when the value exists in the array with different case sensitivity.");
}
@Test
public void zipTest() {
final String[] keys = {"a", "b", "c"};
@ -525,6 +573,13 @@ public class ArrayUtilTest {
Assertions.assertEquals("c", o);
}
@Test
public void getByPredicateTest() {
final String[] a = {"a", "b", "c"};
final Object o = ArrayUtil.get(a, "b"::equals);
Assertions.assertEquals("b", o);
}
@Test
public void replaceTest() {
final String[] a = {"1", "2", "3", "4"};