From d95013f69f1f1b18b387639314471da6633b6b83 Mon Sep 17 00:00:00 2001 From: Rajin Hossain Date: Thu, 13 Oct 2022 01:55:57 +1100 Subject: [PATCH 1/6] two indexOf methods added to include fromIndex parameter --- .../com/google/common/primitives/Bytes.java | 41 +++++++++++++++++++ .../com/google/common/primitives/Bytes.java | 41 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/android/guava/src/com/google/common/primitives/Bytes.java b/android/guava/src/com/google/common/primitives/Bytes.java index 11832742e5fa..851ae940bd4c 100644 --- a/android/guava/src/com/google/common/primitives/Bytes.java +++ b/android/guava/src/com/google/common/primitives/Bytes.java @@ -89,6 +89,19 @@ public static int indexOf(byte[] array, byte target) { return indexOf(array, target, 0, array.length); } + /** + * Returns the index of the first appearance of the value {@code target} in {@code array}. + * + * @param array an array of {@code byte} values, possibly empty + * @param target a primitive {@code byte} value + * @param fromIndex an index from which the searching occurs + * @return the least index {@code i} after {@code fromIndex} for which {@code array[i] == target}, or {@code -1} if no + * such index exists. + */ + public static int indexOf(byte[] array, byte target, int fromIndex) { + return indexOf(array, target, fromIndex, array.length); + } + // TODO(kevinb): consider making this public private static int indexOf(byte[] array, byte target, int start, int end) { for (int i = start; i < end; i++) { @@ -128,6 +141,34 @@ public static int indexOf(byte[] array, byte[] target) { return -1; } + /** + * Returns the start position of the first occurrence of the specified {@code target} within + * {@code array} from the index {@code fromIndex}, or {@code -1} if there is no such occurrence. + * + * @param array the array to search for the sequence {@code target} + * @param target the array to search for as a sub-sequence of {@code array} + * @param fromIndex the index from which to search from + */ + public static int indexOf(byte[] array, byte[] target, int fromIndex) { + checkNotNull(array, "array"); + checkNotNull(target, "target"); + if (target.length == 0) { + return 0; + } + + outer: + for (int i = fromIndex; i < array.length - target.length + 1; i++) { + for (int j = 0; j < target.length; j++) { + if (array[i + j] != target[j]) { + continue outer; + } + } + return i; + } + return -1; + } + + /** * Returns the index of the last appearance of the value {@code target} in {@code array}. * diff --git a/guava/src/com/google/common/primitives/Bytes.java b/guava/src/com/google/common/primitives/Bytes.java index 11832742e5fa..851ae940bd4c 100644 --- a/guava/src/com/google/common/primitives/Bytes.java +++ b/guava/src/com/google/common/primitives/Bytes.java @@ -89,6 +89,19 @@ public static int indexOf(byte[] array, byte target) { return indexOf(array, target, 0, array.length); } + /** + * Returns the index of the first appearance of the value {@code target} in {@code array}. + * + * @param array an array of {@code byte} values, possibly empty + * @param target a primitive {@code byte} value + * @param fromIndex an index from which the searching occurs + * @return the least index {@code i} after {@code fromIndex} for which {@code array[i] == target}, or {@code -1} if no + * such index exists. + */ + public static int indexOf(byte[] array, byte target, int fromIndex) { + return indexOf(array, target, fromIndex, array.length); + } + // TODO(kevinb): consider making this public private static int indexOf(byte[] array, byte target, int start, int end) { for (int i = start; i < end; i++) { @@ -128,6 +141,34 @@ public static int indexOf(byte[] array, byte[] target) { return -1; } + /** + * Returns the start position of the first occurrence of the specified {@code target} within + * {@code array} from the index {@code fromIndex}, or {@code -1} if there is no such occurrence. + * + * @param array the array to search for the sequence {@code target} + * @param target the array to search for as a sub-sequence of {@code array} + * @param fromIndex the index from which to search from + */ + public static int indexOf(byte[] array, byte[] target, int fromIndex) { + checkNotNull(array, "array"); + checkNotNull(target, "target"); + if (target.length == 0) { + return 0; + } + + outer: + for (int i = fromIndex; i < array.length - target.length + 1; i++) { + for (int j = 0; j < target.length; j++) { + if (array[i + j] != target[j]) { + continue outer; + } + } + return i; + } + return -1; + } + + /** * Returns the index of the last appearance of the value {@code target} in {@code array}. * From a8e8e37121a00efa49c4fca24a6d9de5027ebbd8 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 13 Oct 2022 16:40:53 +1100 Subject: [PATCH 2/6] Add one lastIndexOf method with include fromIndex parameter to Bytes.java --- .../com/google/common/primitives/Bytes.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/guava/src/com/google/common/primitives/Bytes.java b/guava/src/com/google/common/primitives/Bytes.java index 851ae940bd4c..80ab1c45f6f2 100644 --- a/guava/src/com/google/common/primitives/Bytes.java +++ b/guava/src/com/google/common/primitives/Bytes.java @@ -102,6 +102,30 @@ public static int indexOf(byte[] array, byte target, int fromIndex) { return indexOf(array, target, fromIndex, array.length); } + /** + * Return the index of the last appearance of the vale {@code target} in {@code array} + * + * @param array an array of {@code byte} values, possibly empty + * @param target a primitive {@code byte} value + * @param fromIndex an index from which the searching occurs + * @return the highest index {@code i} after {@code fromIndex} for which {@code array[i] == target}, or {@code -1} if no + * such index exists. + */ + //TODO: Ryan + public static int lastIndexOf(byte[] array, byte target, int fromIndex){ + // check if fromIndex exceed the range + if(fromIndex< array.length){ + return -1; + } + + for (int i = array.length-1; i > fromIndex; i--) { + if (array[i] == target) { + return i; + } + } + return -1; + } + // TODO(kevinb): consider making this public private static int indexOf(byte[] array, byte target, int start, int end) { for (int i = start; i < end; i++) { @@ -168,7 +192,11 @@ public static int indexOf(byte[] array, byte[] target, int fromIndex) { return -1; } + //TODO: Ryan + public static int lastIndexOf(byte[] array, byte[] target, int fromIndex){ + return 0; + } /** * Returns the index of the last appearance of the value {@code target} in {@code array}. * From 5468c50e4b50a52c1ec2ac3ed9d94b3a8df9ae24 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 13 Oct 2022 18:21:07 +1100 Subject: [PATCH 3/6] Add two lastIndexOf method with fromIndex parameter to Bytes.java --- .../com/google/common/primitives/Bytes.java | 61 ++++++++++++++++++ .../com/google/common/primitives/Bytes.java | 62 +++++++++++++++---- 2 files changed, 110 insertions(+), 13 deletions(-) diff --git a/android/guava/src/com/google/common/primitives/Bytes.java b/android/guava/src/com/google/common/primitives/Bytes.java index 851ae940bd4c..5f8539e9b8ab 100644 --- a/android/guava/src/com/google/common/primitives/Bytes.java +++ b/android/guava/src/com/google/common/primitives/Bytes.java @@ -102,6 +102,28 @@ public static int indexOf(byte[] array, byte target, int fromIndex) { return indexOf(array, target, fromIndex, array.length); } + /** + * Return the index of the last appearance of the value {@code target} in {@code array} + * from the index {@code fromIndex}, or {@code -1} if there is no such occurrence. + * + * @param array an array of {@code byte} values, possibly empty + * @param target a primitive {@code byte} value + * @param fromIndex an index from which the searching occurs + * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no + * such index exists. + */ + public static int lastIndexOf(byte[] array, byte target, int fromIndex){ + checkNotNull(array, "array"); + checkNotNull(target, "target"); + checkNotNull(fromIndex,"fromIndex"); + // check if fromIndex exceed the range + if(fromIndex > array.length-1 || fromIndex < 0){ + return -1; + } + + return lastIndexOf(array, target,fromIndex, array.length); + } + // TODO(kevinb): consider making this public private static int indexOf(byte[] array, byte target, int start, int end) { for (int i = start; i < end; i++) { @@ -168,6 +190,45 @@ public static int indexOf(byte[] array, byte[] target, int fromIndex) { return -1; } + /** + * Returns the first position of the last occurrence of the specified {@code + * target} within {@code array}, or {@code -1} if there is no such occurrence. + * + * @param array an array of {@code byte} values, possibly empty + * @param target the array to search for as a sub-sequence of {@code array} + * @param fromIndex an index from which the searching occurs + * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no + * such index exists. + */ + public static int lastIndexOf(byte[] array, byte[] target, int fromIndex){ + checkNotNull(array, "array"); + checkNotNull(target, "target"); + checkNotNull(fromIndex,"fromIndex"); + + if (target.length == 0) { + return array.length - 1; + } else if (target.length > array.length) { + return -1; + } + + int lastIndexOf = -1; + boolean differentValue; + + for (int i = array.length-target.length ; i >= fromIndex; i--) { + differentValue = false; + for (int j = 0; j < target.length; j++) { + if (array[i + j] != target[j]) { + differentValue = true; + break; + } + } + + if(!differentValue){ + lastIndexOf = i; + } + } + return lastIndexOf; + } /** * Returns the index of the last appearance of the value {@code target} in {@code array}. diff --git a/guava/src/com/google/common/primitives/Bytes.java b/guava/src/com/google/common/primitives/Bytes.java index 80ab1c45f6f2..069c7d6143c6 100644 --- a/guava/src/com/google/common/primitives/Bytes.java +++ b/guava/src/com/google/common/primitives/Bytes.java @@ -99,31 +99,32 @@ public static int indexOf(byte[] array, byte target) { * such index exists. */ public static int indexOf(byte[] array, byte target, int fromIndex) { + checkNotNull(array, "array"); + checkNotNull(target, "target"); + checkNotNull(fromIndex,"fromIndex"); return indexOf(array, target, fromIndex, array.length); } /** - * Return the index of the last appearance of the vale {@code target} in {@code array} + * Return the index of the last appearance of the value {@code target} in {@code array} + * from the index {@code fromIndex}, or {@code -1} if there is no such occurrence. * * @param array an array of {@code byte} values, possibly empty * @param target a primitive {@code byte} value * @param fromIndex an index from which the searching occurs - * @return the highest index {@code i} after {@code fromIndex} for which {@code array[i] == target}, or {@code -1} if no - * such index exists. + * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no + * such index exists. */ - //TODO: Ryan public static int lastIndexOf(byte[] array, byte target, int fromIndex){ + checkNotNull(array, "array"); + checkNotNull(target, "target"); + checkNotNull(fromIndex,"fromIndex"); // check if fromIndex exceed the range - if(fromIndex< array.length){ + if(fromIndex > array.length-1 || fromIndex < 0){ return -1; } - for (int i = array.length-1; i > fromIndex; i--) { - if (array[i] == target) { - return i; - } - } - return -1; + return lastIndexOf(array, target,fromIndex, array.length); } // TODO(kevinb): consider making this public @@ -192,11 +193,46 @@ public static int indexOf(byte[] array, byte[] target, int fromIndex) { return -1; } - //TODO: Ryan + /** + * Returns the first position of the last occurrence of the specified {@code + * target} within {@code array}, or {@code -1} if there is no such occurrence. + * + * @param array an array of {@code byte} values, possibly empty + * @param target the array to search for as a sub-sequence of {@code array} + * @param fromIndex an index from which the searching occurs + * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no + * such index exists. + */ public static int lastIndexOf(byte[] array, byte[] target, int fromIndex){ + checkNotNull(array, "array"); + checkNotNull(target, "target"); + checkNotNull(fromIndex,"fromIndex"); + + if (target.length == 0) { + return array.length - 1; + } else if (target.length > array.length) { + return -1; + } + + int lastIndexOf = -1; + boolean differentValue; - return 0; + for (int i = array.length-target.length ; i >= fromIndex; i--) { + differentValue = false; + for (int j = 0; j < target.length; j++) { + if (array[i + j] != target[j]) { + differentValue = true; + break; + } + } + + if(!differentValue){ + lastIndexOf = i; + } + } + return lastIndexOf; } + /** * Returns the index of the last appearance of the value {@code target} in {@code array}. * From 5d13f7e802b679bf3ff6ed98e0b874b633d5f3e0 Mon Sep 17 00:00:00 2001 From: Waylon Wei Date: Thu, 13 Oct 2022 20:16:00 +1100 Subject: [PATCH 4/6] done tests for testIndexOf_arrayTarget_fromIndex and testIndexOf_fromIndex --- .../google/common/primitives/BytesTest.java | 86 ++++++++++++++++++ .../google/common/primitives/BytesTest.java | 88 +++++++++++++++++++ 2 files changed, 174 insertions(+) diff --git a/android/guava-tests/test/com/google/common/primitives/BytesTest.java b/android/guava-tests/test/com/google/common/primitives/BytesTest.java index da06a34bb95d..2ae2ab950002 100644 --- a/android/guava-tests/test/com/google/common/primitives/BytesTest.java +++ b/android/guava-tests/test/com/google/common/primitives/BytesTest.java @@ -37,7 +37,13 @@ public class BytesTest extends TestCase { private static final byte[] EMPTY = {}; private static final byte[] ARRAY1 = {(byte) 1}; + private static final byte[] ARRAY2 = {(byte) 2}; + private static final byte[] ARRAY2 = {(byte) 3}; + private static final byte[] ARRAY4 = {(byte) 4}; + private static final byte[] ARRAY23 = {(byte) 2, (byte) 3}; + private static final byte[] ARRAY34 = {(byte) 3, (byte) 4}; private static final byte[] ARRAY234 = {(byte) 2, (byte) 3, (byte) 4}; + private static final byte[] ARRAY234234 = {(byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3, (byte) 4}; private static final byte[] VALUES = {Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE}; @@ -69,6 +75,30 @@ public void testIndexOf() { .isEqualTo(1); } + public void testIndexOf_fromIndex() { + assertThat(Bytes.indexOf(EMPTY, (byte) 1, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, (byte) 1, 10)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, (byte) 1, -10)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, (byte) 2, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, (byte) 1, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 1, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 2, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 2, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 4)).isEqualTo(-1); + + assertThat(Bytes.indexOf(ARRAY1, (byte) 1, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(new byte[] {(byte) -1}, (byte) -1, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, (byte) 2, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 0)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 0)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 1)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 2)).isEqualTo(2); + } + public void testIndexOf_arrayTarget() { assertThat(Bytes.indexOf(EMPTY, EMPTY)).isEqualTo(0); assertThat(Bytes.indexOf(ARRAY234, EMPTY)).isEqualTo(0); @@ -103,6 +133,62 @@ public void testIndexOf_arrayTarget() { .isEqualTo(-1); } + public void testIndexOf_arrayTarget_fromIndex() { + ssertThat(Bytes.indexOf(EMPTY, EMPTY, 0)).isEqualTo(0); + ssertThat(Bytes.indexOf(EMPTY, EMPTY, 1)).isEqualTo(-1); + ssertThat(Bytes.indexOf(EMPTY, EMPTY, -1)).isEqualTo(0); + + assertThat(Bytes.indexOf(EMPTY, ARRAY234, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, ARRAY234, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, ARRAY234, -1)).isEqualTo(-1); + + assertThat(Bytes.indexOf(ARRAY1, ARRAY1), 0).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY1, ARRAY1), 1).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, ARRAY1), 100).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, ARRAY1), -1).isEqualTo(0); + + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 2)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, -1)).isEqualTo(0); + + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, 0).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, 1).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, 3).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, -1).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY2, 0).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY2, 1).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY2, -1).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, 0).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, 1).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, 3).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, -1).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, 0).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, 2).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, 3).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, -1).isEqualTo(2); + + assertThat(Bytes.indexOf(ARRAY234, ARRAY23, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY23, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY23, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, 0)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, -1)).isEqualTo(1); + + assertThat(Bytes.indexOf(ARRAY234, ARRAY234, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY234, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY234, -1)).isEqualTo(0); + + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 1)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 2)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 3)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 4)).isEqualTo(-1); + } + public void testLastIndexOf() { assertThat(Bytes.lastIndexOf(EMPTY, (byte) 1)).isEqualTo(-1); assertThat(Bytes.lastIndexOf(ARRAY1, (byte) 2)).isEqualTo(-1); diff --git a/guava-tests/test/com/google/common/primitives/BytesTest.java b/guava-tests/test/com/google/common/primitives/BytesTest.java index da06a34bb95d..cf9e8c16aa56 100644 --- a/guava-tests/test/com/google/common/primitives/BytesTest.java +++ b/guava-tests/test/com/google/common/primitives/BytesTest.java @@ -37,7 +37,13 @@ public class BytesTest extends TestCase { private static final byte[] EMPTY = {}; private static final byte[] ARRAY1 = {(byte) 1}; + private static final byte[] ARRAY2 = {(byte) 2}; + private static final byte[] ARRAY3 = {(byte) 3}; + private static final byte[] ARRAY4 = {(byte) 4}; + private static final byte[] ARRAY23 = {(byte) 2, (byte) 3}; + private static final byte[] ARRAY34 = {(byte) 3, (byte) 4}; private static final byte[] ARRAY234 = {(byte) 2, (byte) 3, (byte) 4}; + private static final byte[] ARRAY234234 = {(byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3, (byte) 4}; private static final byte[] VALUES = {Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE}; @@ -68,6 +74,30 @@ public void testIndexOf() { assertThat(Bytes.indexOf(new byte[] {(byte) 2, (byte) 3, (byte) 2, (byte) 3}, (byte) 3)) .isEqualTo(1); } + + public void testIndexOf_fromIndex() { + assertThat(Bytes.indexOf(EMPTY, (byte) 1, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, (byte) 1, 10)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, (byte) 1, -10)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, (byte) 2, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, (byte) 1, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 1, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 2, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 2, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 4)).isEqualTo(-1); + + assertThat(Bytes.indexOf(ARRAY1, (byte) 1, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(new byte[] {(byte) -1}, (byte) -1, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, (byte) 2, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 0)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 0)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 1)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, (byte) 4, 2)).isEqualTo(2); + } public void testIndexOf_arrayTarget() { assertThat(Bytes.indexOf(EMPTY, EMPTY)).isEqualTo(0); @@ -102,6 +132,64 @@ public void testIndexOf_arrayTarget() { new byte[] {(byte) 2, (byte) 3, (byte) 4})) .isEqualTo(-1); } + + public void testIndexOf_arrayTarget_fromIndex() { + assertThat(Bytes.indexOf(EMPTY, EMPTY, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(EMPTY, EMPTY, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, EMPTY, -1)).isEqualTo(0); + + assertThat(Bytes.indexOf(EMPTY, ARRAY234, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, ARRAY234, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, ARRAY234, -1)).isEqualTo(-1); + + assertThat(Bytes.indexOf(ARRAY1, ARRAY1, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY1, ARRAY1, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, ARRAY1, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY1, ARRAY1, -1)).isEqualTo(0); + + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 2)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, -1)).isEqualTo(0); + + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY1, -1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY2, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY2, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY2, -1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, 0)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY3, -1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, 0)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, 2)).isEqualTo(2); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY4, -1)).isEqualTo(2); + + assertThat(Bytes.indexOf(ARRAY234, ARRAY23, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY23, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY23, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, 0)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY34, -1)).isEqualTo(1); + + assertThat(Bytes.indexOf(ARRAY234, ARRAY234, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, ARRAY234, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, ARRAY234, -1)).isEqualTo(0); + + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 0)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 1)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 2)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 3)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 4)).isEqualTo(-1); + + + } public void testLastIndexOf() { assertThat(Bytes.lastIndexOf(EMPTY, (byte) 1)).isEqualTo(-1); From 95c4a9bb8020c835c72158e3cab1abb58f6eb2e3 Mon Sep 17 00:00:00 2001 From: Shengpeng Gao Eric Date: Mon, 17 Oct 2022 12:50:40 +1100 Subject: [PATCH 5/6] Done tests for last_indexOf --- .../google/common/primitives/BytesTest.java | 22 +++++++++++++++++++ .../google/common/primitives/BytesTest.java | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/android/guava-tests/test/com/google/common/primitives/BytesTest.java b/android/guava-tests/test/com/google/common/primitives/BytesTest.java index 2ae2ab950002..1865d5303281 100644 --- a/android/guava-tests/test/com/google/common/primitives/BytesTest.java +++ b/android/guava-tests/test/com/google/common/primitives/BytesTest.java @@ -201,6 +201,28 @@ public void testLastIndexOf() { .isEqualTo(3); } + public void testLastIndexOf_fromIndex() { + // empty case + assertThat(Bytes.indexOf(EMPTY, EMPTY, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, ARRAY234, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, (byte) 1, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 1)).isEqualTo(1); + + // normal byte case + assertThat(Bytes.indexOf(ARRAY34, (byte) 4, 0)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY1, (byte) 1, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234234, (byte) 4, 1)).isEqualTo(5); + + // byte list case + assertThat(Bytes.indexOf(ARRAY234234, ARRAY1, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY23, 0)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 1)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 4)).isEqualTo(-1); + } + public void testConcat() { assertThat(Bytes.concat()).isEqualTo(EMPTY); assertThat(Bytes.concat(EMPTY)).isEqualTo(EMPTY); diff --git a/guava-tests/test/com/google/common/primitives/BytesTest.java b/guava-tests/test/com/google/common/primitives/BytesTest.java index cf9e8c16aa56..128c5e5f8b03 100644 --- a/guava-tests/test/com/google/common/primitives/BytesTest.java +++ b/guava-tests/test/com/google/common/primitives/BytesTest.java @@ -203,6 +203,28 @@ public void testLastIndexOf() { .isEqualTo(3); } + public void testLastIndexOf_fromIndex() { + // empty case + assertThat(Bytes.indexOf(EMPTY, EMPTY, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, ARRAY234, 1)).isEqualTo(-1); + assertThat(Bytes.indexOf(EMPTY, (byte) 1, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 3)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, -1)).isEqualTo(0); + assertThat(Bytes.indexOf(ARRAY234, EMPTY, 1)).isEqualTo(1); + + // normal byte case + assertThat(Bytes.indexOf(ARRAY34, (byte) 4, 0)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY1, (byte) 1, 2)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234, (byte) 3, 1)).isEqualTo(1); + assertThat(Bytes.indexOf(ARRAY234234, (byte) 4, 1)).isEqualTo(5); + + // byte list case + assertThat(Bytes.indexOf(ARRAY234234, ARRAY1, 0)).isEqualTo(-1); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY23, 0)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 1)).isEqualTo(3); + assertThat(Bytes.indexOf(ARRAY234234, ARRAY234, 4)).isEqualTo(-1); + } + public void testConcat() { assertThat(Bytes.concat()).isEqualTo(EMPTY); assertThat(Bytes.concat(EMPTY)).isEqualTo(EMPTY); From a0ee419fc53fed46073e58c4463e874aaa8f63f1 Mon Sep 17 00:00:00 2001 From: Shengpeng Gao Eric Date: Thu, 27 Oct 2022 18:47:53 +1100 Subject: [PATCH 6/6] Delete unnecessary null checks --- guava/src/com/google/common/primitives/Bytes.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/guava/src/com/google/common/primitives/Bytes.java b/guava/src/com/google/common/primitives/Bytes.java index 069c7d6143c6..b0df29e0dd5b 100644 --- a/guava/src/com/google/common/primitives/Bytes.java +++ b/guava/src/com/google/common/primitives/Bytes.java @@ -116,9 +116,6 @@ public static int indexOf(byte[] array, byte target, int fromIndex) { * such index exists. */ public static int lastIndexOf(byte[] array, byte target, int fromIndex){ - checkNotNull(array, "array"); - checkNotNull(target, "target"); - checkNotNull(fromIndex,"fromIndex"); // check if fromIndex exceed the range if(fromIndex > array.length-1 || fromIndex < 0){ return -1; @@ -204,9 +201,6 @@ public static int indexOf(byte[] array, byte[] target, int fromIndex) { * such index exists. */ public static int lastIndexOf(byte[] array, byte[] target, int fromIndex){ - checkNotNull(array, "array"); - checkNotNull(target, "target"); - checkNotNull(fromIndex,"fromIndex"); if (target.length == 0) { return array.length - 1;