Skip to content

refactor: Enhance docs, remove main. add more tests in `HexaDecimal… #5922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Unit tests for the {@link EndianConverter} class.
*/
public class HexaDecimalToBinaryTest {

private final HexaDecimalToBinary converter = new HexaDecimalToBinary();

@ParameterizedTest
@CsvSource({"1, 00000001", "A1, 10100001", "EF, 11101111", "BA, 10111010", "7, 00000111", "F, 00001111", "7FFFFFFF, 1111111111111111111111111111111", "ABCDEF, 101010111100110111101111"})
public void testHexaDecimalToBinary(String hexInput, String expectedBinary) {
assertEquals(expectedBinary, converter.convert(hexInput));
}

/**
* 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({"0, 00000000", "2, 00000010", "3, 00000011"})
public void testPaddingWithLeadingZeros(String hexInput, String expectedBinary) {
assertEquals(expectedBinary, converter.convert(hexInput));
@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({"G, NumberFormatException", "ZZ, NumberFormatException"})
public void testInvalidHexInput(String hexInput, String expectedException) {
try {
converter.convert(hexInput);
} catch (NumberFormatException e) {
assertEquals(expectedException, e.getClass().getSimpleName());
}
@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));
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy