Enhance docs, add more tests in EndianConverter (#5921)

This commit is contained in:
Hardik Pawar
2024-10-27 00:05:36 +05:30
committed by GitHub
parent 62c9309a31
commit b5a097c56a
2 changed files with 52 additions and 15 deletions

View File

@@ -1,11 +1,23 @@
package com.thealgorithms.conversions;
/**
* Converts between big-endian and little-endian formats.
* Big-endian is the most significant byte first, while little-endian is the least significant byte first.
* Big-endian to little-endian: 0x12345678 -> 0x78563412
* Utility class for converting integers between big-endian and little-endian formats.
* <p>
* Endianness defines how byte sequences represent multi-byte data types:
* <ul>
* <li><b>Big-endian</b>: The most significant byte (MSB) comes first.</li>
* <li><b>Little-endian</b>: The least significant byte (LSB) comes first.</li>
* </ul>
* <p>
* Example conversion:
* <ul>
* <li>Big-endian to little-endian: {@code 0x12345678} → {@code 0x78563412}</li>
* <li>Little-endian to big-endian: {@code 0x78563412} → {@code 0x12345678}</li>
* </ul>
*
* Little-endian to big-endian: 0x12345678 -> 0x78563412
* <p>Note: Both conversions in this utility are equivalent since reversing the bytes is symmetric.</p>
*
* <p>This class only supports 32-bit integers.</p>
*
* @author Hardvan
*/
@@ -13,10 +25,22 @@ public final class EndianConverter {
private EndianConverter() {
}
/**
* Converts a 32-bit integer from big-endian to little-endian.
*
* @param value the integer in big-endian format
* @return the integer in little-endian format
*/
public static int bigToLittleEndian(int value) {
return Integer.reverseBytes(value);
}
/**
* Converts a 32-bit integer from little-endian to big-endian.
*
* @param value the integer in little-endian format
* @return the integer in big-endian format
*/
public static int littleToBigEndian(int value) {
return Integer.reverseBytes(value);
}

View File

@@ -2,21 +2,34 @@ 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;
public class EndianConverterTest {
@Test
public void testBigToLittleEndian() {
assertEquals(0x78563412, EndianConverter.bigToLittleEndian(0x12345678));
assertEquals(0x00000000, EndianConverter.bigToLittleEndian(0x00000000));
assertEquals(0x00000001, EndianConverter.bigToLittleEndian(0x01000000));
@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));
}
@Test
public void testLittleToBigEndian() {
assertEquals(0x12345678, EndianConverter.littleToBigEndian(0x78563412));
assertEquals(0x00000000, EndianConverter.littleToBigEndian(0x00000000));
assertEquals(0x01000000, EndianConverter.littleToBigEndian(0x00000001));
@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));
}
}