Skip to content

Commit c2ce527

Browse files
committed
Fix
1 parent dbdfd96 commit c2ce527

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,41 @@
55
import org.junit.jupiter.params.ParameterizedTest;
66
import org.junit.jupiter.params.provider.CsvSource;
77

8+
/**
9+
* Unit tests for the {@link EndianConverter} class.
10+
*/
811
public class HexaDecimalToBinaryTest {
912

10-
private final HexaDecimalToBinary converter = new HexaDecimalToBinary();
11-
12-
@ParameterizedTest
13-
@CsvSource({"1, 00000001", "A1, 10100001", "EF, 11101111", "BA, 10111010", "7, 00000111", "F, 00001111", "7FFFFFFF, 1111111111111111111111111111111", "ABCDEF, 101010111100110111101111"})
14-
public void testHexaDecimalToBinary(String hexInput, String expectedBinary) {
15-
assertEquals(expectedBinary, converter.convert(hexInput));
16-
}
17-
13+
/**
14+
* Parameterized test to validate the conversion from little-endian to big-endian.
15+
* Hexadecimal values are passed as strings and converted to integers during the test.
16+
*/
1817
@ParameterizedTest
19-
@CsvSource({"0, 00000000", "2, 00000010", "3, 00000011"})
20-
public void testPaddingWithLeadingZeros(String hexInput, String expectedBinary) {
21-
assertEquals(expectedBinary, converter.convert(hexInput));
18+
@CsvSource({
19+
"0x78563412, 0x12345678", "0x00000000, 0x00000000", "0x00000001, 0x01000000",
20+
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
21+
"0x0000007F, 0x7F000000" // Positive boundary case
22+
})
23+
public void
24+
testLittleToBigEndian(String inputHex, String expectedHex) {
25+
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
26+
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
27+
assertEquals(expected, EndianConverter.littleToBigEndian(input));
2228
}
2329

30+
/**
31+
* Parameterized test to validate the conversion from big-endian to little-endian.
32+
*/
2433
@ParameterizedTest
25-
@CsvSource({"G, NumberFormatException", "ZZ, NumberFormatException"})
26-
public void testInvalidHexInput(String hexInput, String expectedException) {
27-
try {
28-
converter.convert(hexInput);
29-
} catch (NumberFormatException e) {
30-
assertEquals(expectedException, e.getClass().getSimpleName());
31-
}
34+
@CsvSource({
35+
"0x12345678, 0x78563412", "0x00000000, 0x00000000", "0x01000000, 0x00000001",
36+
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
37+
"0x7F000000, 0x0000007F" // Positive boundary case
38+
})
39+
public void
40+
testBigToLittleEndian(String inputHex, String expectedHex) {
41+
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
42+
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
43+
assertEquals(expected, EndianConverter.bigToLittleEndian(input));
3244
}
3345
}

0 commit comments

Comments
 (0)