Consolidate ArrayUtilsTest into one file

Commit 35402eb8ae ("Move tests for com.android.internal.util out of
coretests") intentionally moved ArrayUtilsTest from coretests into
utiltests, but commit 082614c6a5 ("Cache per-Activity Resources
objects") immediately added the file back with some new test cases only.
This appears to have been a rebase error, as these commits went in at
about the same time.  The result is that there are two ArrayUtilsTest
files, which is confusing.  Fix this by moving the coretests test cases
into utiltests.

Test: atest com.android.internal.util.ArrayUtilsTest
Change-Id: If410c6e15b7169843aae204ee7ae7301e6a64401
This commit is contained in:
Eric Biggers
2022-08-03 01:32:11 +00:00
parent 4fdb5d00fa
commit 1434a42676
2 changed files with 184 additions and 206 deletions

View File

@@ -1,206 +0,0 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.internal.util;
import androidx.test.filters.SmallTest;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class ArrayUtilsTest extends TestCase {
@SmallTest
public void testUnstableRemoveIf() throws Exception {
java.util.function.Predicate<Object> isNull = new java.util.function.Predicate<Object>() {
@Override
public boolean test(Object o) {
return o == null;
}
};
final Object a = new Object();
final Object b = new Object();
final Object c = new Object();
ArrayList<Object> collection = null;
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
collection = new ArrayList<>();
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
collection = new ArrayList<>(Collections.singletonList(a));
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Collections.singletonList(null));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(0, collection.size());
collection = new ArrayList<>(Arrays.asList(a, b));
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(a, null));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, a));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, null));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(0, collection.size());
collection = new ArrayList<>(Arrays.asList(a, b, c));
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(3, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
assertTrue(collection.contains(c));
collection = new ArrayList<>(Arrays.asList(a, b, null));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(a, null, b));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(null, a, b));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(a, null, null));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, null, a));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, a, null));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, null, null));
assertEquals(3, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(0, collection.size());
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenRangeInsideArray() {
ArrayUtils.throwsIfOutOfBounds(10, 2, 6);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenRangeIsWholeArray() {
ArrayUtils.throwsIfOutOfBounds(10, 0, 10);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtStart() {
ArrayUtils.throwsIfOutOfBounds(10, 0, 0);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtEnd() {
ArrayUtils.throwsIfOutOfBounds(10, 10, 0);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenEmptyArray() {
ArrayUtils.throwsIfOutOfBounds(0, 0, 0);
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenRangeStartNegative() {
try {
ArrayUtils.throwsIfOutOfBounds(10, -1, 5);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenCountNegative() {
try {
ArrayUtils.throwsIfOutOfBounds(10, 5, -1);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenRangeStartTooHigh() {
try {
ArrayUtils.throwsIfOutOfBounds(10, 11, 0);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenRangeEndTooHigh() {
try {
ArrayUtils.throwsIfOutOfBounds(10, 5, 6);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenLengthNegative() {
try {
ArrayUtils.throwsIfOutOfBounds(-1, 0, 0);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenOverflowRangeEndTooHigh() {
try {
ArrayUtils.throwsIfOutOfBounds(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
}

View File

@@ -18,6 +18,12 @@ package com.android.internal.util;
import static org.junit.Assert.assertArrayEquals;
import androidx.test.filters.SmallTest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import junit.framework.TestCase;
/**
@@ -257,4 +263,182 @@ public class ArrayUtilsTest extends TestCase {
assertArrayEquals(expectation, ArrayUtils.concat(array1, array2, array3));
}
@SmallTest
public void testUnstableRemoveIf() throws Exception {
java.util.function.Predicate<Object> isNull = new java.util.function.Predicate<Object>() {
@Override
public boolean test(Object o) {
return o == null;
}
};
final Object a = new Object();
final Object b = new Object();
final Object c = new Object();
ArrayList<Object> collection = null;
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
collection = new ArrayList<>();
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
collection = new ArrayList<>(Collections.singletonList(a));
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Collections.singletonList(null));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(0, collection.size());
collection = new ArrayList<>(Arrays.asList(a, b));
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(a, null));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, a));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, null));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(0, collection.size());
collection = new ArrayList<>(Arrays.asList(a, b, c));
assertEquals(0, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(3, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
assertTrue(collection.contains(c));
collection = new ArrayList<>(Arrays.asList(a, b, null));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(a, null, b));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(null, a, b));
assertEquals(1, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(2, collection.size());
assertTrue(collection.contains(a));
assertTrue(collection.contains(b));
collection = new ArrayList<>(Arrays.asList(a, null, null));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, null, a));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, a, null));
assertEquals(2, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(1, collection.size());
assertTrue(collection.contains(a));
collection = new ArrayList<>(Arrays.asList(null, null, null));
assertEquals(3, ArrayUtils.unstableRemoveIf(collection, isNull));
assertEquals(0, collection.size());
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenRangeInsideArray() {
ArrayUtils.throwsIfOutOfBounds(10, 2, 6);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenRangeIsWholeArray() {
ArrayUtils.throwsIfOutOfBounds(10, 0, 10);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtStart() {
ArrayUtils.throwsIfOutOfBounds(10, 0, 0);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtEnd() {
ArrayUtils.throwsIfOutOfBounds(10, 10, 0);
}
@SmallTest
public void testThrowsIfOutOfBounds_passesWhenEmptyArray() {
ArrayUtils.throwsIfOutOfBounds(0, 0, 0);
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenRangeStartNegative() {
try {
ArrayUtils.throwsIfOutOfBounds(10, -1, 5);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenCountNegative() {
try {
ArrayUtils.throwsIfOutOfBounds(10, 5, -1);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenRangeStartTooHigh() {
try {
ArrayUtils.throwsIfOutOfBounds(10, 11, 0);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenRangeEndTooHigh() {
try {
ArrayUtils.throwsIfOutOfBounds(10, 5, 6);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenLengthNegative() {
try {
ArrayUtils.throwsIfOutOfBounds(-1, 0, 0);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
@SmallTest
public void testThrowsIfOutOfBounds_failsWhenOverflowRangeEndTooHigh() {
try {
ArrayUtils.throwsIfOutOfBounds(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
// expected
}
}
}