|
5 | 5 | import org.junit.jupiter.params.ParameterizedTest; |
6 | 6 | import org.junit.jupiter.params.provider.CsvSource; |
7 | 7 |
|
| 8 | +/** |
| 9 | + * Unit tests for the {@link EndianConverter} class. |
| 10 | + */ |
8 | 11 | public class HexaDecimalToBinaryTest { |
9 | 12 |
|
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 | + */ |
18 | 17 | @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)); |
22 | 28 | } |
23 | 29 |
|
| 30 | + /** |
| 31 | + * Parameterized test to validate the conversion from big-endian to little-endian. |
| 32 | + */ |
24 | 33 | @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)); |
32 | 44 | } |
33 | 45 | } |
0 commit comments