Test the Conversion class (#1155)

* add tests for Conversion

* nHexs --> nHex
This commit is contained in:
Elliotte Rusty Harold 2024-01-10 14:38:56 +00:00 committed by GitHub
parent 02eac5e1a5
commit d234fced82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 3 deletions

View File

@ -777,7 +777,7 @@ public class Conversion {
}
/**
* Converts an array of Char into a byte using the default (little-endian, Lsb0) byte and
* Converts a hexadecimal string into a byte using the default (little-endian, Lsb0) byte and
* bit ordering.
*
* @param src the hexadecimal string to convert
@ -787,7 +787,7 @@ public class Conversion {
* @param dstPos the position of the lsb, in bits, in the result byte
* @param nHex the number of Chars to convert
* @return a byte containing the selected bits
* @throws IllegalArgumentException if {@code (nHexs-1)*4+dstPos >= 8}
* @throws IllegalArgumentException if {@code (nHex-1)*4+dstPos >= 8}
*/
public static byte hexToByte(final String src, final int srcPos, final byte dstInit, final int dstPos,
final int nHex) {
@ -795,7 +795,7 @@ public class Conversion {
return dstInit;
}
if ((nHex - 1) * 4 + dstPos >= 8) {
throw new IllegalArgumentException("(nHexs-1)*4+dstPos is greater or equal to than 8");
throw new IllegalArgumentException("(nHex-1)*4+dstPos is greater than or equal to 8");
}
byte out = dstInit;
for (int i = 0; i < nHex; i++) {

View File

@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.builder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.apache.commons.lang3.Conversion;
import org.junit.jupiter.api.Test;
class ConversionTest {
@Test
void testHexToByte() {
assertEquals((byte) 0, Conversion.hexToByte("00", 0, (byte) 0, 0, 0));
assertEquals((byte) 0, Conversion.hexToByte("00", 0, (byte) 0, 0, 2));
}
@Test
void testHexToByte_nullString() {
assertThrows(NullPointerException.class, () -> Conversion.hexToByte(null, 0, (byte) 0, 0, 2));
}
@Test
void testHexToByte_IllegalArgument() {
assertThrows(IllegalArgumentException.class, () -> Conversion.hexToByte("A0", 0, (byte) 0, 4, 2));
}
}