Merge "Make a copy of libcore.util.ArraysUtils in framework" am: f35963cb77

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1531681

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Ia74bb76418cbed901535af0fafe1e2e03423806e
This commit is contained in:
Treehugger Robot
2020-12-17 10:29:32 +00:00
committed by Automerger Merge Worker
6 changed files with 111 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
package android.content.pm;
import libcore.util.ArrayUtils;
import com.android.internal.util.ArrayUtils;
import java.io.FilterInputStream;
import java.io.IOException;

View File

@@ -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;

View File

@@ -33,11 +33,12 @@ import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import com.android.internal.util.ArrayUtils;
import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.FastNative;
import dalvik.system.VMRuntime;
import libcore.util.ArrayUtils;
import libcore.util.SneakyThrow;
import java.io.ByteArrayInputStream;

View File

@@ -733,6 +733,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
*

View File

@@ -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
}
}
}

View File

@@ -25,9 +25,10 @@ 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.Streams;
import libcore.util.ArrayUtils;
import java.io.FileDescriptor;
import java.io.FilterOutputStream;