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

This commit is contained in:
Victor Chang
2020-12-17 02:25:00 +00:00
committed by Android (Google) Code Review
6 changed files with 110 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

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

View File

@@ -744,6 +744,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;