diff options
-rw-r--r-- | luni/src/main/java/java/util/Arrays.java | 1114 | ||||
-rw-r--r-- | luni/src/test/java/org/apache/harmony/luni/tests/java/util/ArraysTest.java | 1669 |
2 files changed, 2586 insertions, 197 deletions
diff --git a/luni/src/main/java/java/util/Arrays.java b/luni/src/main/java/java/util/Arrays.java index c49a298..44f7616 100644 --- a/luni/src/main/java/java/util/Arrays.java +++ b/luni/src/main/java/java/util/Arrays.java @@ -172,83 +172,142 @@ public class Arrays { } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code byte} array to search. - * @param value - * the {@code byte} element to find. + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. */ public static int binarySearch(byte[] array, byte value) { - int lo = 0; - int hi = array.length - 1; - - while (lo <= hi) { - int mid = (lo + hi) >>> 1; - byte midVal = array[mid]; - - if (midVal < value) - lo = mid + 1; - else if (midVal > value) - hi = mid - 1; - else - return mid; // value found - } - return ~lo; // value not present + return binarySearch(array, 0, array.length, value); } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code char} array to search. - * @param value - * the {@code char} element to find. + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(byte[] array, int startIndex, int endIndex, byte value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; + + while (lo <= hi) { + int mid = (lo + hi) >>> 1; + byte midVal = array[mid]; + + if (midVal < value) { + lo = mid + 1; + } else if (midVal > value) { + hi = mid - 1; + } else { + return mid; // value found + } + } + return ~lo; // value not present + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. */ public static int binarySearch(char[] array, char value) { - int lo = 0; - int hi = array.length - 1; - - while (lo <= hi) { - int mid = (lo + hi) >>> 1; - char midVal = array[mid]; - - if (midVal < value) - lo = mid + 1; - else if (midVal > value) - hi = mid - 1; - else - return mid; // value found - } - return ~lo; // value not present + return binarySearch(array, 0, array.length, value); } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code double} array to search. - * @param value - * the {@code double} element to find. + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(char[] array, int startIndex, int endIndex, char value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; + + while (lo <= hi) { + int mid = (lo + hi) >>> 1; + char midVal = array[mid]; + + if (midVal < value) { + lo = mid + 1; + } else if (midVal > value) { + hi = mid - 1; + } else { + return mid; // value found + } + } + return ~lo; // value not present + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. */ public static int binarySearch(double[] array, double value) { - int lo = 0; - int hi = array.length - 1; + return binarySearch(array, 0, array.length, value); + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(double[] array, int startIndex, int endIndex, double value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; while (lo <= hi) { int mid = (lo + hi) >>> 1; @@ -264,33 +323,53 @@ public class Arrays { long midValBits = Double.doubleToLongBits(midVal); long valueBits = Double.doubleToLongBits(value); - if (midValBits < valueBits) + if (midValBits < valueBits) { lo = mid + 1; // (-0.0, 0.0) or (not NaN, NaN); midVal < val - else if (midValBits > valueBits) + } else if (midValBits > valueBits) { hi = mid - 1; // (0.0, -0.0) or (NaN, not NaN); midVal > val - else + } else { return mid; // bit patterns are equal; value found + } } } return ~lo; // value not present } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code float} array to search. - * @param value - * the {@code float} element to find. + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. */ public static int binarySearch(float[] array, float value) { - int lo = 0; - int hi = array.length - 1; + return binarySearch(array, 0, array.length, value); + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(float[] array, int startIndex, int endIndex, float value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; while (lo <= hi) { int mid = (lo + hi) >>> 1; @@ -306,184 +385,302 @@ public class Arrays { int midValBits = Float.floatToIntBits(midVal); int valueBits = Float.floatToIntBits(value); - if (midValBits < valueBits) + if (midValBits < valueBits) { lo = mid + 1; // (-0.0, 0.0) or (not NaN, NaN); midVal < val - else if (midValBits > valueBits) + } else if (midValBits > valueBits) { hi = mid - 1; // (0.0, -0.0) or (NaN, not NaN); midVal > val - else + } else { return mid; // bit patterns are equal; value found + } } } return ~lo; // value not present } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code int} array to search. - * @param value - * the {@code int} element to find. + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. */ - public static int binarySearch(int[] array, int value) { - int lo = 0; - int hi = array.length - 1; + public static int binarySearch(int[] array, int value) { + return binarySearch(array, 0, array.length, value); + } - while (lo <= hi) { - int mid = (lo + hi) >>> 1; - int midVal = array[mid]; + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(int[] array, int startIndex, int endIndex, int value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; - if (midVal < value) - lo = mid + 1; - else if (midVal > value) - hi = mid - 1; - else - return mid; // value found - } - return ~lo; // value not present - } + while (lo <= hi) { + int mid = (lo + hi) >>> 1; + int midVal = array[mid]; + + if (midVal < value) { + lo = mid + 1; + } else if (midVal > value) { + hi = mid - 1; + } else { + return mid; // value found + } + } + return ~lo; // value not present + } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code long} array to search. - * @param value - * the {@code long} element to find. + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. */ public static int binarySearch(long[] array, long value) { - int lo = 0; - int hi = array.length - 1; - - while (lo <= hi) { - int mid = (lo + hi) >>> 1; - long midVal = array[mid]; - - if (midVal < value) - lo = mid + 1; - else if (midVal > value) - hi = mid - 1; - else - return mid; // value found + return binarySearch(array, 0, array.length, value); + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(long[] array, int startIndex, int endIndex, long value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; + + while (lo <= hi) { + int mid = (lo + hi) >>> 1; + long midVal = array[mid]; + + if (midVal < value) { + lo = mid + 1; + } else if (midVal > value) { + hi = mid - 1; + } else { + return mid; // value found + } } return ~lo; // value not present } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code Object} array to search. - * @param value - * the {@code Object} element to find. + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. * @throws ClassCastException - * if an element in the array or the search element does not - * implement {@code Comparable}, or cannot be compared to each other. + * if an element in the array or the search element does not + * implement {@code Comparable}, or cannot be compared to each other. */ public static int binarySearch(Object[] array, Object value) { - int lo = 0; - int hi = array.length - 1; - - while (lo <= hi) { - int mid = (lo + hi) >>> 1; - @SuppressWarnings("unchecked") - int midValCmp = ((Comparable) array[mid]).compareTo(value); - - if (midValCmp < 0) - lo = mid + 1; - else if (midValCmp > 0) - hi = mid - 1; - else - return mid; // value found - } - return ~lo; // value not present + return binarySearch(array, 0, array.length, value); } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array using the {@code Comparator} to compare elements. - * Searching in an unsorted array has an undefined result. It's also - * undefined which element is found if there are multiple occurrences of the - * same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted array to search - * @param value - * the element to find - * @param comparator - * the {@code Comparator} sued to compare the elements. + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. * @throws ClassCastException - * if an element in the array cannot be compared to the search element - * using the {@code Comparator}. + * if an element in the array or the search element does not + * implement {@code Comparable}, or cannot be compared to each other. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(Object[] array, int startIndex, int endIndex, Object value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; + + while (lo <= hi) { + int mid = (lo + hi) >>> 1; + @SuppressWarnings("unchecked") + int midValCmp = ((Comparable) array[mid]).compareTo(value); + + if (midValCmp < 0) { + lo = mid + 1; + } else if (midValCmp > 0) { + hi = mid - 1; + } else { + return mid; // value found + } + } + return ~lo; // value not present + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * using {@comparator} to compare elements. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param value the element to find. + * @param comparator the {@code Comparator} used to compare the elements. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws ClassCastException + * if an element in the array or the search element does not + * implement {@code Comparable}, or cannot be compared to each other. */ - public static <T> int binarySearch(T[] array, T value, + public static <T> int binarySearch(T[] array, T value, Comparator<? super T> comparator) { + return binarySearch(array, 0, array.length, value, comparator); + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive), + * using {@comparator} to compare elements. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @param comparator the {@code Comparator} used to compare the elements. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws ClassCastException + * if an element in the array or the search element does not + * implement {@code Comparable}, or cannot be compared to each other. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static <T> int binarySearch(T[] array, int startIndex, int endIndex, T value, Comparator<? super T> comparator) { - if (comparator == null) - return binarySearch(array, value); + if (comparator == null) { + return binarySearch(array, startIndex, endIndex, value); + } - int lo = 0; - int hi = array.length - 1; + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; while (lo <= hi) { int mid = (lo + hi) >>> 1; int midValCmp = comparator.compare(array[mid], value); - if (midValCmp < 0) + if (midValCmp < 0) { lo = mid + 1; - else if (midValCmp > 0) + } else if (midValCmp > 0) { hi = mid - 1; - else + } else { return mid; // value found + } } return ~lo; // value not present } /** - * Performs a binary search for the specified element in the specified - * ascending sorted array. Searching in an unsorted array has an undefined - * result. It's also undefined which element is found if there are multiple - * occurrences of the same element. + * Performs a binary search for {@code value} in the ascending sorted array {@code array}. + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. * - * @param array - * the sorted {@code short} array to search. - * @param value - * the {@code short} element to find. + * @param array the sorted array to search. + * @param value the element to find. * @return the non-negative index of the element, or a negative index which * is {@code -index - 1} where the element would be inserted. */ public static int binarySearch(short[] array, short value) { - int lo = 0; - int hi = array.length - 1; - - while (lo <= hi) { - int mid = (lo + hi) >>> 1; - short midVal = array[mid]; - - if (midVal < value) - lo = mid + 1; - else if (midVal > value) - hi = mid - 1; - else - return mid; // value found - } - return ~lo; // value not present + return binarySearch(array, 0, array.length, value); + } + + /** + * Performs a binary search for {@code value} in the ascending sorted array {@code array}, + * in the range specified by fromIndex (inclusive) and toIndex (exclusive). + * Searching in an unsorted array has an undefined result. It's also undefined which element + * is found if there are multiple occurrences of the same element. + * + * @param array the sorted array to search. + * @param startIndex the inclusive start index. + * @param endIndex the exclusive start index. + * @param value the element to find. + * @return the non-negative index of the element, or a negative index which + * is {@code -index - 1} where the element would be inserted. + * @throws IllegalArgumentException if {@code startIndex > endIndex} + * @throws ArrayIndexOutOfBoundsException if {@code startIndex < 0 || endIndex > array.length} + * @since 1.6 + * @hide + */ + public static int binarySearch(short[] array, int startIndex, int endIndex, short value) { + checkBinarySearchBounds(startIndex, endIndex, array.length); + int lo = startIndex; + int hi = endIndex - 1; + + while (lo <= hi) { + int mid = (lo + hi) >>> 1; + short midVal = array[mid]; + + if (midVal < value) { + lo = mid + 1; + } else if (midVal > value) { + hi = mid - 1; + } else { + return mid; // value found + } + } + return ~lo; // value not present + } + + private static void checkBinarySearchBounds(int startIndex, int endIndex, int length) { + if (startIndex > endIndex) { + throw new IllegalArgumentException(); + } + if (startIndex < 0 || endIndex > length) { + throw new ArrayIndexOutOfBoundsException(); + } } /** @@ -517,7 +714,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(byte[] array, int start, int end, byte value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -554,7 +751,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(short[] array, int start, int end, short value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -591,7 +788,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(char[] array, int start, int end, char value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -628,7 +825,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(int[] array, int start, int end, int value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -665,7 +862,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(long[] array, int start, int end, long value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -702,7 +899,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(float[] array, int start, int end, float value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -739,7 +936,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(double[] array, int start, int end, double value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -776,7 +973,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(boolean[] array, int start, int end, boolean value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -813,7 +1010,7 @@ public class Arrays { * if {@code start < 0} or {@code end > array.length}. */ public static void fill(Object[] array, int start, int end, Object value) { - checkBounds(array.length, start, end); + checkFillBounds(array.length, start, end); for (int i = start; i < end; i++) { array[i] = value; } @@ -1549,7 +1746,7 @@ public class Arrays { DualPivotQuicksort.sort(array, start, end); } - private static void checkBounds(int arrLength, int start, int end) { + private static void checkFillBounds(int arrLength, int start, int end) { if (start > end) { // K0033=Start index ({0}) is greater than end index ({1}) throw new IllegalArgumentException(Msg.getString("K0033", //$NON-NLS-1$ @@ -2314,4 +2511,529 @@ public class Arrays { } return false; } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code false}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static boolean[] copyOf(boolean[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code (byte) 0}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static byte[] copyOf(byte[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code '\\u0000'}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static char[] copyOf(char[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code 0.0d}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static double[] copyOf(double[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code 0.0f}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static float[] copyOf(float[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code 0}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static int[] copyOf(int[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code 0L}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static long[] copyOf(long[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code (short) 0}. + * + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static short[] copyOf(short[] original, int newLength) { + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code null}. + * + * @param <T> array element type + * @param original the original array + * @param newLength the length of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static <T> T[] copyOf(T[] original, int newLength) { + if (original == null) { + throw new NullPointerException(); + } + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength); + } + + /** + * Copies {@code newLength} elements from {@code original} into a new array. + * If {@code newLength} is greater than {@code original.length}, the result is padded + * with the value {@code null}. + * + * @param <T> result array element type + * @param <U> original array element type + * @param original the original array + * @param newLength the length of the new array + * @param newType the class of the new array + * @return the new array + * @throws NegativeArraySizeException if {@code newLength < 0} + * @throws NullPointerException if {@code original == null} + * @throws ArrayStoreException if a value in {@code original} is incompatible with T + * @since 1.6 + * @hide + */ + public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { + // We use the null pointer check in copyOfRange for exception priority compatibility. + if (newLength < 0) { + throw new NegativeArraySizeException(); + } + return copyOfRange(original, 0, newLength, newType); + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code false}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static boolean[] copyOfRange(boolean[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + boolean[] result = new boolean[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code (byte) 0}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static byte[] copyOfRange(byte[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + byte[] result = new byte[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code '\\u0000'}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static char[] copyOfRange(char[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + char[] result = new char[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code 0.0d}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static double[] copyOfRange(double[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + double[] result = new double[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code 0.0f}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static float[] copyOfRange(float[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + float[] result = new float[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code 0}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static int[] copyOfRange(int[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + int[] result = new int[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code 0L}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static long[] copyOfRange(long[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + long[] result = new long[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code (short) 0}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + public static short[] copyOfRange(short[] original, int start, int end) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + short[] result = new short[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code null}. + * + * @param <T> the element type + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @since 1.6 + * @hide + */ + @SuppressWarnings("unchecked") + public static <T> T[] copyOfRange(T[] original, int start, int end) { + int originalLength = original.length; // For exception priority compatibility. + if (start > end) { + throw new IllegalArgumentException(); + } + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + T[] result = (T[]) Array.newInstance(original.getClass().getComponentType(), resultLength); + System.arraycopy(original, start, result, 0, copyLength); + return result; + } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to + * end (exclusive). The original order of elements is preserved. + * If {@code end} is greater than {@code original.length}, the result is padded + * with the value {@code null}. + * + * @param <T> result array element type + * @param <U> original array element type + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @throws ArrayStoreException if a value in {@code original} is incompatible with T + * @since 1.6 + * @hide + */ + @SuppressWarnings("unchecked") + public static <T, U> T[] copyOfRange(U[] original, int start, int end, Class<? extends T[]> newType) { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + T[] result = (T[]) Array.newInstance(newType.getComponentType(), resultLength); + System.arraycopy(original, start, result, 0, copyLength); + return result; + } } diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/util/ArraysTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/util/ArraysTest.java index 54f51f0..dafaaea 100644 --- a/luni/src/test/java/org/apache/harmony/luni/tests/java/util/ArraysTest.java +++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/util/ArraysTest.java @@ -19,6 +19,7 @@ package org.apache.harmony.luni.tests.java.util; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Comparator; +import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -1832,9 +1833,1674 @@ public class ArraysTest extends junit.framework.TestCase { } } - /** + /** + * @tests java.util.Arrays#binarySearch(byte[],int,int, byte) + */ + public void test_binarySearch$BIIB() { + for (byte counter = 0; counter < arraySize; counter++) { + assertTrue( + "Binary search on byte[] answered incorrect position", + Arrays.binarySearch(byteArray, counter, arraySize, counter) == counter); + } + assertEquals( + "Binary search succeeded for value not present in array 1", -1, + Arrays.binarySearch(byteArray, 0, arraySize, (byte) -1)); + assertTrue( + "Binary search succeeded for value not present in array 2", + Arrays.binarySearch(byteArray, (byte) arraySize) == -(arraySize + 1)); + for (byte counter = 0; counter < arraySize; counter++) { + byteArray[counter] -= 50; + } + for (byte counter = 0; counter < arraySize; counter++) { + assertTrue( + "Binary search on byte[] involving negative numbers answered incorrect position", + Arrays.binarySearch(byteArray, counter, arraySize, + (byte) (counter - 50)) == counter); + } + try { + Arrays.binarySearch((byte[])null, 2, 1, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((byte[])null, -1, 0, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((byte[])null, -1, -2, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(byteArray, 2, 1, (byte) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays.binarySearch(byteArray, 0, 0, (byte) arraySize)); + try { + Arrays.binarySearch(byteArray, -1, -2, (byte) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(byteArray, arraySize + 2, arraySize + 1, + (byte) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(byteArray, -1, 0, (byte) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(byteArray, 0, arraySize + 1, (byte) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(char[], char) + */ + public void test_binarySearch$CIIC() { + for (char counter = 0; counter < arraySize; counter++) { + assertTrue("Binary search on char[] answered incorrect position", + Arrays.binarySearch(charArray, counter, arraySize, + (char) (counter + 1)) == counter); + } + assertEquals( + "Binary search succeeded for value not present in array 1", -1, + Arrays.binarySearch(charArray, 0, arraySize, '\u0000')); + assertTrue("Binary search succeeded for value not present in array 2", + Arrays.binarySearch(charArray, 0, arraySize, + (char) (arraySize + 1)) == -(arraySize + 1)); + try { + Arrays.binarySearch(charArray, 2, 1, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch((char[])null, 2, 1, (char) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((char[])null, -1, 0, (char) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((char[])null, -1, -2, (char) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + assertEquals(-1, Arrays.binarySearch(charArray, 0, 0, (char) arraySize)); + try { + Arrays.binarySearch(charArray, -1, -2, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(charArray, arraySize + 2, arraySize + 1, + (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(charArray, -1, 0, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(charArray, 0, arraySize + 1, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(double[], double) + */ + public void test_binarySearch$DIID() { + for (int counter = 0; counter < arraySize; counter++) { + assertTrue("Binary search on double[] answered incorrect position", + Arrays.binarySearch(doubleArray, counter, arraySize, + (double) counter) == (double) counter); + } + assertEquals( + "Binary search succeeded for value not present in array 1", -1, + Arrays.binarySearch(doubleArray, 0, arraySize, (double) -1)); + assertTrue("Binary search succeeded for value not present in array 2", + Arrays.binarySearch(doubleArray, 0, arraySize, + (double) arraySize) == -(arraySize + 1)); + for (int counter = 0; counter < arraySize; counter++) { + doubleArray[counter] -= (double) 50; + } + for (int counter = 0; counter < arraySize; counter++) { + assertTrue( + "Binary search on double[] involving negative numbers answered incorrect position", + Arrays.binarySearch(doubleArray, counter, arraySize,(double) (counter - 50)) == (double) counter); + } + double[] specials = new double[] { Double.NEGATIVE_INFINITY, + -Double.MAX_VALUE, -2d, -Double.MIN_VALUE, -0d, 0d, + Double.MIN_VALUE, 2d, Double.MAX_VALUE, + Double.POSITIVE_INFINITY, Double.NaN }; + for (int i = 0; i < specials.length; i++) { + int result = Arrays.binarySearch(specials, i, specials.length,specials[i]); + assertTrue(specials[i] + " invalid: " + result, result == i); + } + assertEquals("-1d", -4, Arrays.binarySearch(specials,0,specials.length, -1d)); + assertEquals("1d", -8, Arrays.binarySearch(specials,0,specials.length, 1d)); + try { + Arrays.binarySearch((double[])null, 2, 1, (double) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((double[])null, -1, 0, (double) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((double[])null, -1, -2, (double) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(doubleArray, 2, 1, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays.binarySearch(doubleArray, 0, 0, (char) arraySize)); + try { + Arrays.binarySearch(doubleArray, -1, -2, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(doubleArray, arraySize + 2, arraySize + 1, + (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(doubleArray, -1, 0, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(doubleArray, 0, arraySize + 1, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(float[], float) + */ + public void test_binarySearch$FIIF() { + for (int counter = 0; counter < arraySize; counter++) { + assertTrue("Binary search on float[] answered incorrect position", + Arrays.binarySearch(floatArray, counter, arraySize, + (float) counter) == (float) counter); + } + assertEquals( + "Binary search succeeded for value not present in array 1", -1, + Arrays.binarySearch(floatArray, 0, arraySize, (float) -1)); + assertTrue("Binary search succeeded for value not present in array 2", + Arrays + .binarySearch(floatArray, 0, arraySize, + (float) arraySize) == -(arraySize + 1)); + for (int counter = 0; counter < arraySize; counter++) { + floatArray[counter] -= (float) 50; + } + for (int counter = 0; counter < arraySize; counter++) { + assertTrue( + "Binary search on float[] involving negative numbers answered incorrect position", + Arrays.binarySearch(floatArray, 0, arraySize, + (float) counter - 50) == (float) counter); + } + float[] specials = new float[] { Float.NEGATIVE_INFINITY, + -Float.MAX_VALUE, -2f, -Float.MIN_VALUE, -0f, 0f, + Float.MIN_VALUE, 2f, Float.MAX_VALUE, Float.POSITIVE_INFINITY, + Float.NaN }; + for (int i = 0; i < specials.length; i++) { + int result = Arrays.binarySearch(specials,i,specials.length,specials[i]); + assertTrue(specials[i] + " invalid: " + result, result == i); + } + assertEquals("-1f", -4, Arrays.binarySearch(specials,0,specials.length, -1f)); + assertEquals("1f", -8, Arrays.binarySearch(specials,0,specials.length, 1f)); + try { + Arrays.binarySearch((float[])null, 2, 1, (float) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((float[])null, -1, 0, (float) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((float[])null, -1, -2, (float) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(floatArray, 2, 1, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays.binarySearch(floatArray, 0, 0, + (char) arraySize)); + try { + Arrays.binarySearch(floatArray, -1, -2, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(floatArray, arraySize + 2, arraySize + 1, + (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(floatArray, -1, 0, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays + .binarySearch(floatArray, 0, arraySize + 1, + (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(int[], int) + */ + public void test_binarySearch$IIII() { + for (int counter = 0; counter < arraySize; counter++) { + assertTrue( + "Binary search on int[] answered incorrect position", + Arrays.binarySearch(intArray, counter, arraySize, counter) == counter); + } + assertEquals( + "Binary search succeeded for value not present in array 1", -1, + Arrays.binarySearch(intArray,0, arraySize, -1)); + assertTrue("Binary search succeeded for value not present in array 2", + Arrays.binarySearch(intArray,0, arraySize, arraySize) == -(arraySize + 1)); + for (int counter = 0; counter < arraySize; counter++) { + intArray[counter] -= 50; + } + for (int counter = 0; counter < arraySize; counter++) { + assertTrue( + "Binary search on int[] involving negative numbers answered incorrect position", + Arrays.binarySearch(intArray,0, arraySize, counter - 50) == counter); + } + try { + Arrays.binarySearch((int[])null, 2, 1, (int) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((int[])null, -1, 0, (int) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((int[])null, -1, -2, (int) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(intArray, 2, 1, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays + .binarySearch(intArray, 0, 0, (char) arraySize)); + try { + Arrays.binarySearch(intArray, -1, -2, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(intArray, arraySize + 2, arraySize + 1, + (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(intArray, -1, 0, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(intArray, 0, arraySize + 1, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(long[], long) + */ + public void test_binarySearch$JIIJ() { + for (long counter = 0; counter < arraySize; counter++){ + assertTrue("Binary search on long[] answered incorrect position", + Arrays.binarySearch(longArray,0,arraySize, counter) == counter); + } + assertEquals("Binary search succeeded for value not present in array 1", + -1, Arrays.binarySearch(longArray,0,arraySize, (long) -1)); + assertTrue( + "Binary search succeeded for value not present in array 2", + Arrays.binarySearch(longArray,0,arraySize, (long) arraySize) == -(arraySize + 1)); + for (long counter = 0; counter < arraySize; counter++){ + longArray[(int) counter] -= (long) 50; + } + for (long counter = 0; counter < arraySize; counter++){ + assertTrue( + "Binary search on long[] involving negative numbers answered incorrect position", + Arrays.binarySearch(longArray,0,arraySize, counter - (long) 50) == counter); + } + try { + Arrays.binarySearch((long[])null, 2, 1, (long) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((long[])null, -1, 0, (long) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((long[])null, -1, -2, (long) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(longArray, 2, 1, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays + .binarySearch(longArray, 0, 0, (char) arraySize)); + try { + Arrays.binarySearch(longArray, -1, -2, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(longArray, arraySize + 2, arraySize + 1, + (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(longArray, -1, 0, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(longArray, 0, arraySize + 1, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(java.lang.Object[], + * java.lang.Object) + */ + public void test_binarySearch$Ljava_lang_ObjectIILjava_lang_Object() { + assertEquals( + "Binary search succeeded for non-comparable value in empty array", + -1, Arrays.binarySearch(new Object[] {},0,0, new Object())); + assertEquals( + "Binary search succeeded for comparable value in empty array", + -1, Arrays.binarySearch(new Object[] {},0,0, new Integer(-1))); + for (int counter = 0; counter < arraySize; counter++){ + assertTrue( + "Binary search on Object[] answered incorrect position", + Arrays.binarySearch(objectArray,counter,arraySize, objArray[counter]) == counter); + } + assertEquals("Binary search succeeded for value not present in array 1", + -1, Arrays.binarySearch(objectArray,0,arraySize, new Integer(-1))); + assertTrue( + "Binary search succeeded for value not present in array 2", + Arrays.binarySearch(objectArray,0,arraySize, new Integer(arraySize)) == -(arraySize + 1)); + try { + Arrays.binarySearch((Object[])null, 2, 1, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((Object[])null, -1, 0, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((Object[])null, -1, -2, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, 2, 1, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays + .binarySearch(objectArray, 0, 0, (char) arraySize)); + try { + Arrays.binarySearch(objectArray, -1, -2, (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, arraySize + 2, arraySize + 1, + (char) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, -1, 0, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, 0, arraySize + 1, (char) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(java.lang.Object[], + * java.lang.Object, java.util.Comparator) + */ + public void test_binarySearch$Ljava_lang_ObjectIILjava_lang_ObjectLjava_util_Comparator() { + Comparator comp = new ReversedIntegerComparator(); + for (int counter = 0; counter < arraySize; counter++) { + objectArray[counter] = objArray[arraySize - counter - 1]; + } + assertTrue("Binary search succeeded for value not present in array 1", + Arrays.binarySearch(objectArray, 0, arraySize, new Integer(-1), + comp) == -(arraySize + 1)); + assertEquals( + "Binary search succeeded for value not present in array 2", -1, + Arrays.binarySearch(objectArray, 0, arraySize, new Integer( + arraySize), comp)); + for (int counter = 0; counter < arraySize; counter++) { + assertTrue( + "Binary search on Object[] with custom comparator answered incorrect position", + Arrays.binarySearch(objectArray, objArray[counter], comp) == arraySize + - counter - 1); + } + try { + Arrays.binarySearch((Object[])null, 2, 1, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((Object[])null, -1, 0, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((Object[])null, -1, -2, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, 2, 1, (char) arraySize, comp); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays.binarySearch(objectArray, 0, 0, + (char) arraySize, comp)); + try { + Arrays.binarySearch(objectArray, -1, -2, (char) arraySize, comp); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, arraySize + 2, arraySize + 1, + (char) arraySize, comp); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, -1, 0, (char) arraySize, comp); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, 0, arraySize + 1, + (char) arraySize, comp); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(objectArray, 0, arraySize , + new LinkedList(), comp); + fail("should throw ClassCastException"); + } catch (ClassCastException e) { + // expected + } + } + + /** + * @tests java.util.Arrays#binarySearch(short[], short) + */ + public void test_binarySearch$SIIS() { + for (short counter = 0; counter < arraySize; counter++){ + assertTrue("Binary search on short[] answered incorrect position", + Arrays.binarySearch(shortArray,counter,arraySize, counter) == counter); + } + assertEquals("Binary search succeeded for value not present in array 1", + -1, Arrays.binarySearch(shortArray,0,arraySize, (short) -1)); + assertTrue( + "Binary search succeeded for value not present in array 2", + Arrays.binarySearch(shortArray,0,arraySize, (short) arraySize) == -(arraySize + 1)); + for (short counter = 0; counter < arraySize; counter++){ + shortArray[counter] -= 50; + } + for (short counter = 0; counter < arraySize; counter++){ + assertTrue( + "Binary search on short[] involving negative numbers answered incorrect position", + Arrays.binarySearch(shortArray,counter,arraySize, (short) (counter - 50)) == counter); + } + try { + Arrays.binarySearch((String[])null, 2, 1, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((String[])null, -1, 0, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch((String[])null, -1, -2, (byte) arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.binarySearch(shortArray, 2, 1, (short) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(-1, Arrays + .binarySearch(shortArray, 0, 0, (short) arraySize)); + try { + Arrays.binarySearch(shortArray, -1, -2, (short) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(shortArray, arraySize + 2, arraySize + 1, + (short) arraySize); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.binarySearch(shortArray, -1, 0, (short) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.binarySearch(shortArray, 0, arraySize + 1, (short) arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + String[] array = {"a" , "b" , "c"}; + assertEquals(-2,Arrays.binarySearch(array, 1, 2, "a", null)); + } + + /** + * @tests {@link java.util.Arrays#copyOf(byte[],int) + */ + public void test_copyOf_$BI() throws Exception { + byte[] result = Arrays.copyOf(byteArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOf(byteArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i, result[i]); + } + try { + Arrays.copyOf((byte[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(byteArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((byte[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(short[],int) + */ + public void test_copyOf_$SI() throws Exception { + short[] result = Arrays.copyOf(shortArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOf(shortArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i, result[i]); + } + try { + Arrays.copyOf((short[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(shortArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((short[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(int[],int) + */ + public void test_copyOf_$II() throws Exception { + int[] result = Arrays.copyOf(intArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOf(intArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i, result[i]); + } + try { + Arrays.copyOf((int[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(intArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((int[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(boolean[],int) + */ + public void test_copyOf_$ZI() throws Exception { + boolean[] result = Arrays.copyOf(booleanArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(booleanArray[i], result[i]); + } + for (; i < result.length; i++) { + assertEquals(false, result[i]); + } + result = Arrays.copyOf(booleanArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(booleanArray[i], result[i]); + } + try { + Arrays.copyOf((boolean[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(booleanArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((boolean[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(char[],int) + */ + public void test_copyOf_$CI() throws Exception { + char[] result = Arrays.copyOf(charArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i+1, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOf(charArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i+1, result[i]); + } + try { + Arrays.copyOf((char[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(charArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((char[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(float[],int) + */ + public void test_copyOf_$FI() throws Exception { + float[] result = Arrays.copyOf(floatArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(floatArray[i], result[i]); + } + for (; i < result.length; i++) { + assertEquals(0.0f, result[i]); + } + result = Arrays.copyOf(floatArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(floatArray[i], result[i]); + } + try { + Arrays.copyOf((float[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(floatArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((float[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(double[],int) + */ + public void test_copyOf_$DI() throws Exception { + double[] result = Arrays.copyOf(doubleArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(doubleArray[i], result[i]); + } + for (; i < result.length; i++) { + assertEquals(0.0, result[i]); + } + result = Arrays.copyOf(doubleArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(doubleArray[i], result[i]); + } + try { + Arrays.copyOf((double[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(doubleArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((double[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(long[],int) + */ + public void test_copyOf_$JI() throws Exception { + long[] result = Arrays.copyOf(longArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(longArray[i], result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOf(longArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(longArray[i], result[i]); + } + try { + Arrays.copyOf((long[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(longArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((long[])null, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + } + + /** + * @tests {@link java.util.Arrays#copyOf(T[],int) + */ + public void test_copyOf_$TI() throws Exception { + Object[] result = Arrays.copyOf(objArray, arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(objArray[i], result[i]); + } + for (; i < result.length; i++) { + assertNull(result[i]); + } + result = Arrays.copyOf(objArray, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(objArray[i], result[i]); + } + try { + Arrays.copyOf((String[])null, arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(objArray, -1); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((String[])null, -1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + Date[] component = new Date[0]; + Object object[] = new Date[0]; + + object = Arrays.copyOf(component,2); + assertNotNull(object); + component = Arrays.copyOf(component,2); + assertNotNull(component); + assertEquals(2, component.length); + } + + /** + * @tests {@link java.util.Arrays#copyOf(T[],int,Class<? extends Object[]>)) + */ + public void test_copyOf_$TILClass() throws Exception { + Object[] result = Arrays.copyOf(objArray, arraySize*2,Object[].class); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(objArray[i], result[i]); + } + for (; i < result.length; i++) { + assertNull(result[i]); + } + result = Arrays.copyOf(objArray, arraySize/2,Object[].class); + i = 0; + for (; i < result.length; i++) { + assertEquals(objArray[i], result[i]); + } + result = Arrays.copyOf(objArray, arraySize/2,Integer[].class); + i = 0; + for (; i < result.length; i++) { + assertEquals(objArray[i], result[i]); + } + try { + Arrays.copyOf((Object[])null, arraySize,LinkedList[].class); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(objArray, arraySize,LinkedList[].class); + fail("should throw ArrayStoreException "); + } catch (ArrayStoreException e) { + // expected + } + try { + Arrays.copyOf((Object[])null, arraySize,Object[].class); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOf(objArray, -1,Object[].class); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((Object[])null, -1,Object[].class); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((Object[])null, -1,LinkedList[].class); + fail("should throw NegativeArraySizeException"); + } catch (NegativeArraySizeException e) { + // expected + } + try { + Arrays.copyOf((Object[])null, 0,LinkedList[].class); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + assertEquals(0,Arrays.copyOf(objArray, 0,LinkedList[].class).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(byte[],int,int) + */ + public void test_copyOfRange_$BII() throws Exception { + byte[] result = Arrays.copyOfRange(byteArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOfRange(byteArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i, result[i]); + } + result = Arrays.copyOfRange(byteArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((byte[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((byte[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((byte[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(byteArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(byteArray, 0, -1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(byteArray.length + 1, Arrays.copyOfRange(byteArray, 0, + byteArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(short[],int,int) + */ + public void test_copyOfRange_$SII() throws Exception { + short[] result = Arrays.copyOfRange(shortArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOfRange(shortArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i, result[i]); + } + result = Arrays.copyOfRange(shortArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((short[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((short[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((short[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(shortArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(shortArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(shortArray.length + 1, Arrays.copyOfRange(shortArray, 0, + shortArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(int[],int,int) + */ + public void test_copyOfRange_$III() throws Exception { + int[] result = Arrays.copyOfRange(intArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOfRange(intArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i, result[i]); + } + result = Arrays.copyOfRange(intArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((int[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((int[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((int[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(intArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(intArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(intArray.length + 1, Arrays.copyOfRange(intArray, 0, + intArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(long[],int,int) + */ + public void test_copyOfRange_$JII() throws Exception { + long[] result = Arrays.copyOfRange(longArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOfRange(longArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i, result[i]); + } + result = Arrays.copyOfRange(longArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((long[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((long[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((long[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(longArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(longArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(longArray.length + 1, Arrays.copyOfRange(longArray, 0, + longArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(char[],int,int) + */ + public void test_copyOfRange_$CII() throws Exception { + char[] result = Arrays.copyOfRange(charArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(i+1, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0, result[i]); + } + result = Arrays.copyOfRange(charArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(i+1, result[i]); + } + result = Arrays.copyOfRange(charArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((char[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((char[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((char[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(charArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(charArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(charArray.length + 1, Arrays.copyOfRange(charArray, 0, + charArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(float[],int,int) + */ + public void test_copyOfRange_$FII() throws Exception { + float[] result = Arrays.copyOfRange(floatArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals((float)i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0.0f, result[i]); + } + result = Arrays.copyOfRange(floatArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals((float)i, result[i]); + } + result = Arrays.copyOfRange(floatArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((float[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((float[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((float[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(floatArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(floatArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(floatArray.length + 1, Arrays.copyOfRange(floatArray, 0, + floatArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(double[],int,int) + */ + public void test_copyOfRange_$DII() throws Exception { + double[] result = Arrays.copyOfRange(doubleArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals((double)i, result[i]); + } + for (; i < result.length; i++) { + assertEquals(0.0, result[i]); + } + result = Arrays.copyOfRange(doubleArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals((double)i, result[i]); + } + result = Arrays.copyOfRange(doubleArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((double[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((double[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((double[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(doubleArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(doubleArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(doubleArray.length + 1, Arrays.copyOfRange(doubleArray, 0, + doubleArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(boolean[],int,int) + */ + public void test_copyOfRange_$ZII() throws Exception { + boolean[] result = Arrays.copyOfRange(booleanArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(booleanArray[i], result[i]); + } + for (; i < result.length; i++) { + assertEquals(false, result[i]); + } + result = Arrays.copyOfRange(booleanArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(booleanArray[i], result[i]); + } + result = Arrays.copyOfRange(booleanArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((boolean[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((boolean[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((boolean[])null, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(booleanArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(booleanArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(booleanArray.length + 1, Arrays.copyOfRange(booleanArray, 0, + booleanArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(Object[],int,int) + */ + public void test_copyOfRange_$TII() throws Exception { + Object[] result = Arrays.copyOfRange(objArray, 0,arraySize*2); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(objArray[i], result[i]); + } + for (; i < result.length; i++) { + assertEquals(null, result[i]); + } + result = Arrays.copyOfRange(objArray,0, arraySize/2); + i = 0; + for (; i < result.length; i++) { + assertEquals(objArray[i], result[i]); + } + result = Arrays.copyOfRange(objArray,0, 0); + assertEquals(0, result.length); + try { + Arrays.copyOfRange((Object[])null, 0,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((Object[])null, -1,arraySize); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((Object[])null, 0,-1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange((Object[])objArray, -1,arraySize); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange((Object[])objArray, 0,-1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + assertEquals(objArray.length + 1, Arrays.copyOfRange(objArray, 0, + objArray.length + 1).length); + } + + /** + * @tests {@link java.util.Arrays#copyOfRange(Object[], int, int, Class) + */ + public void test_copyOfRange_$TIILClass() throws Exception { + Object[] result = Arrays.copyOfRange(objArray, 0,arraySize*2,Integer[].class); + int i = 0; + for (; i < arraySize; i++) { + assertEquals(objArray[i], result[i]); + } + for (; i < result.length; i++) { + assertEquals(null, result[i]); + } + result = Arrays.copyOfRange(objArray,0, arraySize/2,Integer[].class); + i = 0; + for (; i < result.length; i++) { + assertEquals(objArray[i], result[i]); + } + result = Arrays.copyOfRange(objArray,0, 0,Integer[].class); + assertEquals(0, result.length); + try { + Arrays.copyOfRange(null, 0,arraySize,Integer[].class); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange(null, -1,arraySize,Integer[].class); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + Arrays.copyOfRange(null, 0,-1,Integer[].class); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(objArray, -1,arraySize,Integer[].class); + fail("should throw ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + try { + Arrays.copyOfRange(objArray, 0,-1,Integer[].class); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(objArray, 0,-1,LinkedList[].class); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + try { + Arrays.copyOfRange(objArray, 0,1,LinkedList[].class); + fail("should throw ArrayStoreException"); + } catch (ArrayStoreException e) { + // expected + } + try { + Arrays.copyOfRange(null, 0,1,LinkedList[].class); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + try { + assertEquals(objArray.length + 1, Arrays.copyOfRange(objArray, 0, + objArray.length + 1, LinkedList[].class).length); + fail("should throw ArrayStoreException"); + } catch (ArrayStoreException e) { + // expected + } + assertEquals(0, + Arrays.copyOfRange(objArray, 0, 0, LinkedList[].class).length); + } + + /** * @tests java.util.Arrays#swap(int, int, Object[]) */ + /* BEGIN android-removed: tests private implementation detail we don't share. public void test_swap_I_I_$Ljava_lang_Object() throws Exception { Method m = Arrays.class.getDeclaredMethod("swap", int.class, int.class, Object[].class); m.setAccessible(true); @@ -1843,6 +3509,7 @@ public class ArraysTest extends junit.framework.TestCase { assertEquals("should be equal to 1",1, arr[0].intValue()); assertEquals("should be equal to 0",0, arr[1].intValue()); } + */ /** * Tears down the fixture, for example, close a network connection. This |