Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 198ced86 authored by Victor Chang's avatar Victor Chang
Browse files

Make a copy of libcore.util.ArraysUtils in framework

It helps remove it from the @CorePlatformApi

Bug: 154796679
Test: ArrayUtilsTest
Change-Id: I0c8f194a74a16b2cc46f9eea4571d5fb674fbc28
parent aedc4ae8
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
package android.content.pm;

import libcore.util.ArrayUtils;
import com.android.internal.util.ArrayUtils;

import java.io.FilterInputStream;
import java.io.IOException;
+2 −1
Original line number Diff line number Diff line
@@ -22,11 +22,12 @@ import android.system.ErrnoException;
import android.system.Os;
import android.util.Log;

import com.android.internal.util.ArrayUtils;

import libcore.io.IoBridge;
import libcore.io.IoUtils;
import libcore.io.Memory;
import libcore.io.Streams;
import libcore.util.ArrayUtils;

import java.io.FileDescriptor;
import java.io.IOException;
+1 −1
Original line number Diff line number Diff line
@@ -34,11 +34,11 @@ import android.util.SparseBooleanArray;
import android.util.SparseIntArray;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.ArrayUtils;

import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.FastNative;

import libcore.util.ArrayUtils;
import libcore.util.SneakyThrow;

import java.io.ByteArrayInputStream;
+19 −0
Original line number Diff line number Diff line
@@ -737,6 +737,25 @@ public class ArrayUtils {
        }
    }

    /**
     * Throws {@link ArrayIndexOutOfBoundsException} if the range is out of bounds.
     * @param len length of the array. Must be non-negative
     * @param offset start index of the range. Must be non-negative
     * @param count length of the range. Must be non-negative
     * @throws ArrayIndexOutOfBoundsException if the range from {@code offset} with length
     * {@code count} is out of bounds of the array
     */
    public static void throwsIfOutOfBounds(int len, int offset, int count) {
        if (len < 0) {
            throw new ArrayIndexOutOfBoundsException("Negative length: " + len);
        }

        if ((offset | count) < 0 || offset > len - count) {
            throw new ArrayIndexOutOfBoundsException(
                    "length=" + len + "; regionStart=" + offset + "; regionLength=" + count);
        }
    }

    /**
     * Returns an array with values from {@code val} minus {@code null} values
     *
+85 −0
Original line number Diff line number Diff line
@@ -118,4 +118,89 @@ public class ArrayUtilsTest extends TestCase {
        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
        }
    }
}
Loading