summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/java/com/android/internal/util/ArrayUtils.java61
-rw-r--r--core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java78
-rw-r--r--test-runner/src/android/test/MoreAsserts.java27
3 files changed, 166 insertions, 0 deletions
diff --git a/core/java/com/android/internal/util/ArrayUtils.java b/core/java/com/android/internal/util/ArrayUtils.java
index a56fa36..d66ef83 100644
--- a/core/java/com/android/internal/util/ArrayUtils.java
+++ b/core/java/com/android/internal/util/ArrayUtils.java
@@ -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;
+ }
}
diff --git a/core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java b/core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java
index 5dc9ef8..433d4d2 100644
--- a/core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java
+++ b/core/tests/coretests/src/com/android/internal/util/ArrayUtilsTest.java
@@ -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));
+ }
+
}
diff --git a/test-runner/src/android/test/MoreAsserts.java b/test-runner/src/android/test/MoreAsserts.java
index fb0faba..3364895 100644
--- a/test-runner/src/android/test/MoreAsserts.java
+++ b/test-runner/src/android/test/MoreAsserts.java
@@ -128,6 +128,33 @@ public final class MoreAsserts {
}
/**
+ * @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
* specific element mismatch.