Consolidate HexTool methods and add more tests

This commit is contained in:
P0nk
2022-02-14 19:05:50 +01:00
parent 1daddbf302
commit 084e7b22fa
8 changed files with 61 additions and 41 deletions

View File

@@ -11,7 +11,14 @@ class HexToolTest {
void bytesToHexString() {
byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
String expectedHexString = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 7F FF";
assertEquals(expectedHexString, HexTool.toString(bytes));
assertEquals(expectedHexString, HexTool.toHexString(bytes));
}
@Test
void bytesToCompactHexString() {
byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
String expectedHexString = "0102030405060708090A0B0C0D0E0F10117FFF";
assertEquals(expectedHexString, HexTool.toCompactHexString(bytes));
}
@Test
@@ -29,16 +36,23 @@ class HexToolTest {
}
@Test
void upperCaseHexToBytesAndBack() {
String hex = "A1B2C3";
byte[] bytes = HexTool.hexToBytes(hex);
assertEquals(hex, HexTool.bytesToHex(bytes));
void lowerCaseHexStringToBytes() {
String lowerCaseHexString = "01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 7f ff";
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
assertArrayEquals(expectedBytes, HexTool.toBytes(lowerCaseHexString));
}
@Test
void mixedCaseHexToBytesAndBack() {
String hex = "aB5DaA";
byte[] bytes = HexTool.hexToBytes(hex);
assertEquals(hex.toUpperCase(), HexTool.bytesToHex(bytes));
void lowerCaseCompactHexStringToBytes() {
String hexString = "0102030405060708090a0b0c0d0e0f10117fff";
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
assertArrayEquals(expectedBytes, HexTool.toBytes(hexString));
}
@Test
void toStringFromAscii() {
byte[] asciiBytes = new byte[]{1, 10, 20, 30, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 31};
String expectedString = "....0123456789..";
assertEquals(expectedString, HexTool.toStringFromAscii(asciiBytes));
}
}