Skip to content

Commit 60d1377

Browse files
authored
Merge pull request #948 from tapankavasthi/dev/tavasthi-ktln-852
KTLN-852: Guide to the HexFormat API
2 parents 11cfa61 + 1bb0122 commit 60d1377

File tree

1 file changed

+103
-0
lines changed
  • core-kotlin-modules/core-kotlin-strings-5/src/test/kotlin/com/baeldung/hexformat

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.baeldung.hexformat
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.api.assertThrows
6+
7+
@OptIn(ExperimentalStdlibApi::class)
8+
class HexFormatUnitTest {
9+
10+
val prefixNumberFormat = HexFormat {
11+
number.prefix = "%"
12+
}
13+
14+
val prefixRemoveLeadingZerosNumberFormat = HexFormat {
15+
number.removeLeadingZeros = true
16+
}
17+
18+
val hexFormat = HexFormat {
19+
number {
20+
prefix = "0x"
21+
removeLeadingZeros = true
22+
}
23+
}
24+
25+
val macAddressFormat = HexFormat {
26+
upperCase = true
27+
bytes.bytesPerGroup = 1
28+
bytes.groupSeparator = ":"
29+
}
30+
31+
@Test
32+
fun `Return hexadecimal string when formatting byte value with HexFormat`() {
33+
val number: Byte = 63
34+
assertEquals("3f", number.toHexString())
35+
assertEquals("3f", number.toHexString(HexFormat.Default))
36+
assertEquals("%3f", number.toHexString(prefixNumberFormat))
37+
assertEquals("0x3f", number.toHexString(hexFormat))
38+
}
39+
40+
@Test
41+
fun `Return hexadecimal string when formatting short value with HexFormat`() {
42+
val number: Short = 63
43+
assertEquals("003f", number.toHexString())
44+
assertEquals("003f", number.toHexString(HexFormat.Default))
45+
assertEquals("%003f", number.toHexString(prefixNumberFormat))
46+
assertEquals("3f", number.toHexString(prefixRemoveLeadingZerosNumberFormat))
47+
assertEquals("0x3f", number.toHexString(hexFormat))
48+
}
49+
50+
@Test
51+
fun `Return hexadecimal string when formatting int value with HexFormat`() {
52+
val number: Int = 63
53+
assertEquals("0000003f", number.toHexString())
54+
assertEquals("0000003f", number.toHexString(HexFormat.Default))
55+
assertEquals("%0000003f", number.toHexString(prefixNumberFormat))
56+
assertEquals("3f", number.toHexString(prefixRemoveLeadingZerosNumberFormat))
57+
assertEquals("0x3f", number.toHexString(hexFormat))
58+
}
59+
60+
@Test
61+
fun `Return hexadecimal string when formatting long value with HexFormat`() {
62+
val number: Long = 63
63+
assertEquals("000000000000003f", number.toHexString())
64+
assertEquals("000000000000003f", number.toHexString(HexFormat.Default))
65+
assertEquals("%000000000000003f", number.toHexString(prefixNumberFormat))
66+
assertEquals("3f", number.toHexString(prefixRemoveLeadingZerosNumberFormat))
67+
assertEquals("0x3f", number.toHexString(hexFormat))
68+
}
69+
70+
@Test
71+
fun `Return number type when parsing hexadecimal string`() {
72+
assertEquals(63, "3f".hexToByte())
73+
assertThrows<IllegalArgumentException> { "%3f".hexToByte() }
74+
assertEquals(63, "%3f".hexToByte(prefixNumberFormat))
75+
76+
77+
assertEquals(63, "3f".hexToShort())
78+
assertThrows<IllegalArgumentException> { "%3f".hexToShort() }
79+
assertEquals(63, "%3f".hexToShort(prefixNumberFormat))
80+
81+
assertEquals(63, "3f".hexToInt())
82+
assertThrows<IllegalArgumentException> { "%3f".hexToInt() }
83+
assertEquals(63, "%3f".hexToInt(prefixNumberFormat))
84+
85+
assertEquals(63, "3f".hexToLong())
86+
assertThrows<IllegalArgumentException> { "%3f".hexToLong() }
87+
assertEquals(63, "%3f".hexToLong(prefixNumberFormat))
88+
}
89+
90+
@Test
91+
fun `Return mac address hex value when formatting 6 bytes with mac address HexFormat`() {
92+
val macAddressBytes = byteArrayOf(2, 66, -64, -117, -14, 94)
93+
val macAddressHexValue = macAddressBytes.toHexString(macAddressFormat)
94+
assertEquals("02:42:C0:8B:F2:5E", macAddressHexValue)
95+
}
96+
97+
@Test
98+
fun `Return mac address bytes when parsing mac address hex value with mac address HexFormat`() {
99+
val macAddressHexValue = "02:42:C0:8B:F2:5E"
100+
val macAddressBytes = macAddressHexValue.hexToByteArray(macAddressFormat)
101+
assertArrayEquals(byteArrayOf(2, 66, -64, -117, -14, 94), macAddressBytes)
102+
}
103+
}

0 commit comments

Comments
 (0)