Enhance docs, remove main. add more tests in `HexaDecimal… (#5922)

This commit is contained in:
Hardik Pawar
2024-10-26 23:36:52 +05:30
committed by GitHub
parent cfa35a4fd9
commit ce3dd01e68
2 changed files with 81 additions and 34 deletions

View File

@@ -2,14 +2,44 @@ package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
/**
* Unit tests for the {@link EndianConverter} class.
*/
public class HexaDecimalToBinaryTest {
@Test
public void testHexaDecimalToBinary() {
HexaDecimalToBinary hexaDecimalToBinary = new HexaDecimalToBinary();
assertEquals("1111111111111111111111111111111", hexaDecimalToBinary.convert("7fffffff"));
assertEquals("101010111100110111101111", hexaDecimalToBinary.convert("abcdef"));
/**
* Parameterized test to validate the conversion from little-endian to big-endian.
* Hexadecimal values are passed as strings and converted to integers during the test.
*/
@ParameterizedTest
@CsvSource({
"0x78563412, 0x12345678", "0x00000000, 0x00000000", "0x00000001, 0x01000000",
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
"0x0000007F, 0x7F000000" // Positive boundary case
})
public void
testLittleToBigEndian(String inputHex, String expectedHex) {
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
assertEquals(expected, EndianConverter.littleToBigEndian(input));
}
/**
* Parameterized test to validate the conversion from big-endian to little-endian.
*/
@ParameterizedTest
@CsvSource({
"0x12345678, 0x78563412", "0x00000000, 0x00000000", "0x01000000, 0x00000001",
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
"0x7F000000, 0x0000007F" // Positive boundary case
})
public void
testBigToLittleEndian(String inputHex, String expectedHex) {
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
assertEquals(expected, EndianConverter.bigToLittleEndian(input));
}
}