Skip to content

Commit b5a097c

Browse files
authored
Enhance docs, add more tests in EndianConverter (TheAlgorithms#5921)
1 parent 62c9309 commit b5a097c

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

src/main/java/com/thealgorithms/conversions/EndianConverter.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
11
package com.thealgorithms.conversions;
22

33
/**
4-
* Converts between big-endian and little-endian formats.
5-
* Big-endian is the most significant byte first, while little-endian is the least significant byte first.
6-
* Big-endian to little-endian: 0x12345678 -> 0x78563412
4+
* Utility class for converting integers between big-endian and little-endian formats.
5+
* <p>
6+
* Endianness defines how byte sequences represent multi-byte data types:
7+
* <ul>
8+
* <li><b>Big-endian</b>: The most significant byte (MSB) comes first.</li>
9+
* <li><b>Little-endian</b>: The least significant byte (LSB) comes first.</li>
10+
* </ul>
11+
* <p>
12+
* Example conversion:
13+
* <ul>
14+
* <li>Big-endian to little-endian: {@code 0x12345678} → {@code 0x78563412}</li>
15+
* <li>Little-endian to big-endian: {@code 0x78563412} → {@code 0x12345678}</li>
16+
* </ul>
717
*
8-
* Little-endian to big-endian: 0x12345678 -> 0x78563412
18+
* <p>Note: Both conversions in this utility are equivalent since reversing the bytes is symmetric.</p>
19+
*
20+
* <p>This class only supports 32-bit integers.</p>
921
*
1022
* @author Hardvan
1123
*/
1224
public final class EndianConverter {
1325
private EndianConverter() {
1426
}
1527

28+
/**
29+
* Converts a 32-bit integer from big-endian to little-endian.
30+
*
31+
* @param value the integer in big-endian format
32+
* @return the integer in little-endian format
33+
*/
1634
public static int bigToLittleEndian(int value) {
1735
return Integer.reverseBytes(value);
1836
}
1937

38+
/**
39+
* Converts a 32-bit integer from little-endian to big-endian.
40+
*
41+
* @param value the integer in little-endian format
42+
* @return the integer in big-endian format
43+
*/
2044
public static int littleToBigEndian(int value) {
2145
return Integer.reverseBytes(value);
2246
}

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class EndianConverterTest {
89

9-
@Test
10-
public void testBigToLittleEndian() {
11-
assertEquals(0x78563412, EndianConverter.bigToLittleEndian(0x12345678));
12-
assertEquals(0x00000000, EndianConverter.bigToLittleEndian(0x00000000));
13-
assertEquals(0x00000001, EndianConverter.bigToLittleEndian(0x01000000));
10+
@ParameterizedTest
11+
@CsvSource({
12+
"0x78563412, 0x12345678", "0x00000000, 0x00000000", "0x00000001, 0x01000000",
13+
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
14+
"0x0000007F, 0x7F000000" // Positive boundary case
15+
})
16+
public void
17+
testLittleToBigEndian(String inputHex, String expectedHex) {
18+
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
19+
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
20+
assertEquals(expected, EndianConverter.littleToBigEndian(input));
1421
}
1522

16-
@Test
17-
public void testLittleToBigEndian() {
18-
assertEquals(0x12345678, EndianConverter.littleToBigEndian(0x78563412));
19-
assertEquals(0x00000000, EndianConverter.littleToBigEndian(0x00000000));
20-
assertEquals(0x01000000, EndianConverter.littleToBigEndian(0x00000001));
23+
@ParameterizedTest
24+
@CsvSource({
25+
"0x12345678, 0x78563412", "0x00000000, 0x00000000", "0x01000000, 0x00000001",
26+
"0xFFFFFFFF, 0xFFFFFFFF", // -1 in two's complement
27+
"0x7F000000, 0x0000007F" // Positive boundary case
28+
})
29+
public void
30+
testBigToLittleEndian(String inputHex, String expectedHex) {
31+
int input = (int) Long.parseLong(inputHex.substring(2), 16); // Convert hex string to int
32+
int expected = (int) Long.parseLong(expectedHex.substring(2), 16); // Convert hex string to int
33+
assertEquals(expected, EndianConverter.bigToLittleEndian(input));
2134
}
2235
}

0 commit comments

Comments
 (0)