Merge "Add ArrayUtils methods and tests for consumption by KeySet code."

This commit is contained in:
dcashman
2014-06-02 17:34:08 +00:00
committed by Android (Google) Code Review
3 changed files with 166 additions and 0 deletions

View File

@@ -169,6 +169,15 @@ public class ArrayUtils
return false;
}
public static boolean contains(long[] array, long value) {
for (long element : array) {
if (element == value) {
return true;
}
}
return false;
}
public static long total(long[] array) {
long total = 0;
for (long value : array) {
@@ -229,6 +238,14 @@ public class ArrayUtils
return array;
}
/**
* Appends a new value to a copy of the array and returns the copy. If
* the value is already present, the original array is returned
* @param cur The original array, or null to represent an empty array.
* @param val The value to add.
* @return A new array that contains all of the values of the original array
* with the new value added, or the original array.
*/
public static int[] appendInt(int[] cur, int val) {
if (cur == null) {
return new int[] { val };
@@ -264,4 +281,48 @@ public class ArrayUtils
}
return cur;
}
/**
* Appends a new value to a copy of the array and returns the copy. If
* the value is already present, the original array is returned
* @param cur The original array, or null to represent an empty array.
* @param val The value to add.
* @return A new array that contains all of the values of the original array
* with the new value added, or the original array.
*/
public static long[] appendLong(long[] cur, long val) {
if (cur == null) {
return new long[] { val };
}
final int N = cur.length;
for (int i = 0; i < N; i++) {
if (cur[i] == val) {
return cur;
}
}
long[] ret = new long[N + 1];
System.arraycopy(cur, 0, ret, 0, N);
ret[N] = val;
return ret;
}
public static long[] removeLong(long[] cur, long val) {
if (cur == null) {
return null;
}
final int N = cur.length;
for (int i = 0; i < N; i++) {
if (cur[i] == val) {
long[] ret = new long[N - 1];
if (i > 0) {
System.arraycopy(cur, 0, ret, 0, i);
}
if (i < (N - 1)) {
System.arraycopy(cur, i + 1, ret, i, N - i - 1);
}
return ret;
}
}
return cur;
}
}

View File

@@ -16,6 +16,9 @@
package com.android.internal.util;
import android.test.MoreAsserts;
import java.util.Arrays;
import junit.framework.TestCase;
/**
@@ -77,4 +80,79 @@ public class ArrayUtilsTest extends TestCase {
assertFalse(ArrayUtils.containsAll(new Object[] { }, new Object[] { null }));
assertFalse(ArrayUtils.containsAll(new Object[] { A }, new Object[] { null }));
}
public void testContainsInt() throws Exception {
assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 1));
assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 2));
assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 3));
assertFalse(ArrayUtils.contains(new int[] { 1, 2, 3 }, 0));
assertFalse(ArrayUtils.contains(new int[] { 1, 2, 3 }, 4));
assertFalse(ArrayUtils.contains(new int[] { }, 2));
}
public void testAppendInt() throws Exception {
MoreAsserts.assertEquals(new int[] { 1 },
ArrayUtils.appendInt(null, 1));
MoreAsserts.assertEquals(new int[] { 1 },
ArrayUtils.appendInt(new int[] { }, 1));
MoreAsserts.assertEquals(new int[] { 1, 2 },
ArrayUtils.appendInt(new int[] { 1 }, 2));
MoreAsserts.assertEquals(new int[] { 1, 2 },
ArrayUtils.appendInt(new int[] { 1, 2 }, 1));
}
public void testRemoveInt() throws Exception {
assertNull(ArrayUtils.removeInt(null, 1));
MoreAsserts.assertEquals(new int[] { },
ArrayUtils.removeInt(new int[] { }, 1));
MoreAsserts.assertEquals(new int[] { 1, 2, 3, },
ArrayUtils.removeInt(new int[] { 1, 2, 3}, 4));
MoreAsserts.assertEquals(new int[] { 2, 3, },
ArrayUtils.removeInt(new int[] { 1, 2, 3}, 1));
MoreAsserts.assertEquals(new int[] { 1, 3, },
ArrayUtils.removeInt(new int[] { 1, 2, 3}, 2));
MoreAsserts.assertEquals(new int[] { 1, 2, },
ArrayUtils.removeInt(new int[] { 1, 2, 3}, 3));
MoreAsserts.assertEquals(new int[] { 2, 3, 1 },
ArrayUtils.removeInt(new int[] { 1, 2, 3, 1 }, 1));
}
public void testContainsLong() throws Exception {
assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 1));
assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 2));
assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 3));
assertFalse(ArrayUtils.contains(new long[] { 1, 2, 3 }, 0));
assertFalse(ArrayUtils.contains(new long[] { 1, 2, 3 }, 4));
assertFalse(ArrayUtils.contains(new long[] { }, 2));
}
public void testAppendLong() throws Exception {
MoreAsserts.assertEquals(new long[] { 1 },
ArrayUtils.appendLong(null, 1));
MoreAsserts.assertEquals(new long[] { 1 },
ArrayUtils.appendLong(new long[] { }, 1));
MoreAsserts.assertEquals(new long[] { 1, 2 },
ArrayUtils.appendLong(new long[] { 1 }, 2));
MoreAsserts.assertEquals(new long[] { 1, 2 },
ArrayUtils.appendLong(new long[] { 1, 2 }, 1));
}
public void testRemoveLong() throws Exception {
assertNull(ArrayUtils.removeLong(null, 1));
MoreAsserts.assertEquals(new long[] { },
ArrayUtils.removeLong(new long[] { }, 1));
MoreAsserts.assertEquals(new long[] { 1, 2, 3, },
ArrayUtils.removeLong(new long[] { 1, 2, 3}, 4));
MoreAsserts.assertEquals(new long[] { 2, 3, },
ArrayUtils.removeLong(new long[] { 1, 2, 3}, 1));
MoreAsserts.assertEquals(new long[] { 1, 3, },
ArrayUtils.removeLong(new long[] { 1, 2, 3}, 2));
MoreAsserts.assertEquals(new long[] { 1, 2, },
ArrayUtils.removeLong(new long[] { 1, 2, 3}, 3));
MoreAsserts.assertEquals(new long[] { 2, 3, 1 },
ArrayUtils.removeLong(new long[] { 1, 2, 3, 1 }, 1));
}
}

View File

@@ -127,6 +127,33 @@ public final class MoreAsserts {
assertEquals(null, expected, actual);
}
/**
* @hide Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates first
* specific element mismatch.
*/
public static void assertEquals(
String message, long[] expected, long[] actual) {
if (expected.length != actual.length) {
failWrongLength(message, expected.length, actual.length);
}
for (int i = 0; i < expected.length; i++) {
if (expected[i] != actual[i]) {
failWrongElement(message, i, expected[i], actual[i]);
}
}
}
/**
* @hide Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates first
* specific element mismatch.
*/
public static void assertEquals(long[] expected, long[] actual) {
assertEquals(null, expected, actual);
}
/**
* Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates first