From 1434a426760d7e2a26f23cb36f3162d4f4e690f8 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Wed, 3 Aug 2022 01:32:11 +0000 Subject: [PATCH] Consolidate ArrayUtilsTest into one file Commit 35402eb8ae46 ("Move tests for com.android.internal.util out of coretests") intentionally moved ArrayUtilsTest from coretests into utiltests, but commit 082614c6a57a ("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 --- .../android/internal/util/ArrayUtilsTest.java | 206 ------------------ .../android/internal/util/ArrayUtilsTest.java | 184 ++++++++++++++++ 2 files changed, 184 insertions(+), 206 deletions(-) delete mode 100644 core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java diff --git a/core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java b/core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java deleted file mode 100644 index e8793a9f6097e..0000000000000 --- a/core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java +++ /dev/null @@ -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 isNull = new java.util.function.Predicate() { - @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 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 - } - } -} diff --git a/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java index c66a743d8f530..72f3af640b673 100644 --- a/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java @@ -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 isNull = new java.util.function.Predicate() { + @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 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 + } + } }