aherbert commented on code in PR #877:
URL: https://github.com/apache/commons-lang/pull/877#discussion_r873216278
##########
src/test/java/org/apache/commons/lang3/ConversionTest.java:
##########
@@ -413,6 +415,29 @@ public void testBinaryBeMsb0ToHexDigit_2args() {
}
+ @Test
+ public void testBinaryToHexDigitReverse() {
+ final SplittableRandom rng = new SplittableRandom();
+ final boolean[] x = new boolean[8];
+ for (int i = 0; i < 100; i++) {
+ Conversion.longToBinary(rng.nextLong(), 0, x, 0, 8);
+ for (int j = 1; j <= 8; j++) {
+ final boolean[] a = Arrays.copyOf(x, j);
+ final boolean[] b = a.clone();
+ ArrayUtils.reverse(b);
+ for (int k = 0; k < j; k++) {
+ assertEquals(Conversion.binaryToHexDigit(a, k),
+ Conversion.binaryBeMsb0ToHexDigit(b, k));
+ }
+ }
+ }
+ }
+
+ @Test
+ public void binaryBeMsb0ToHexDigitPosOutsideArray() {
+ assertThrows(IndexOutOfBoundsException.class, () ->
Conversion.binaryBeMsb0ToHexDigit(new boolean[8], 99));
Review Comment:
Add a test for the index as negative or a match to the array length.
##########
src/main/java/org/apache/commons/lang3/Conversion.java:
##########
@@ -435,39 +435,40 @@ public static char binaryBeMsb0ToHexDigit(final boolean[]
src) {
* @return a hexadecimal digit representing the selected bits
* @throws IllegalArgumentException if {@code src} is empty
* @throws NullPointerException if {@code src} is {@code null}
+ * @throws IndexOutOfBoundsException if {@code srcPos} is outside the
array.
*/
public static char binaryBeMsb0ToHexDigit(boolean[] src, int srcPos) {
if (src.length == 0) {
Review Comment:
To leave only one branch check on the main path I would put this check
inside the other check:
```Java
if (Integer.compareUnsigned(srcPos, src.length) >= 0) {
// Throw the correct exception
if (src.length == 0) {
throw new IllegalArgumentException("Cannot convert an empty array.");
}
throw new IndexOutOfBoundsException(srcPos + " is not within array
length " + src.length);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]