1+ package prog .util ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import static org .junit .jupiter .api .Assertions .*;
5+
6+ /**
7+ * Comprehensive unit tests for the CommonUtil class.
8+ * Tests both the new method names and deprecated methods for backward compatibility.
9+ */
10+ public class CommonUtilTest {
11+
12+ @ Test
13+ void testByteToUnsignedIntConversion () {
14+ // Test edge cases
15+ assertEquals (0 , CommonUtil .byteToUnsignedInt ((byte ) 0 ), "Zero byte should convert to 0" );
16+ assertEquals (127 , CommonUtil .byteToUnsignedInt ((byte ) 127 ), "Max positive byte should convert to 127" );
17+
18+ // Test common ASCII values
19+ assertEquals (65 , CommonUtil .byteToUnsignedInt ((byte ) 65 ), "ASCII 'A' should convert to 65" );
20+ assertEquals (90 , CommonUtil .byteToUnsignedInt ((byte ) 90 ), "ASCII 'Z' should convert to 90" );
21+ assertEquals (48 , CommonUtil .byteToUnsignedInt ((byte ) 48 ), "ASCII '0' should convert to 48" );
22+
23+ // Test negative byte values (should convert to positive values 128-255)
24+ assertEquals (128 , CommonUtil .byteToUnsignedInt ((byte ) -128 ), "Min byte should convert to 128" );
25+ assertEquals (200 , CommonUtil .byteToUnsignedInt ((byte ) -56 ), "Negative byte -56 should convert to 200" );
26+ assertEquals (255 , CommonUtil .byteToUnsignedInt ((byte ) -1 ), "-1 byte should convert to 255" );
27+ }
28+
29+ @ Test
30+ void testBinaryStringToIntConversion () {
31+ // Test single bit values
32+ assertEquals (0 , CommonUtil .binaryStringToInt ("0" ), "Binary '0' should convert to 0" );
33+ assertEquals (1 , CommonUtil .binaryStringToInt ("1" ), "Binary '1' should convert to 1" );
34+
35+ // Test multi-bit values
36+ assertEquals (2 , CommonUtil .binaryStringToInt ("10" ), "Binary '10' should convert to 2" );
37+ assertEquals (3 , CommonUtil .binaryStringToInt ("11" ), "Binary '11' should convert to 3" );
38+ assertEquals (10 , CommonUtil .binaryStringToInt ("1010" ), "Binary '1010' should convert to 10" );
39+ assertEquals (15 , CommonUtil .binaryStringToInt ("1111" ), "Binary '1111' should convert to 15" );
40+
41+ // Test byte boundary values
42+ assertEquals (255 , CommonUtil .binaryStringToInt ("11111111" ), "8 bits all ones should convert to 255" );
43+ assertEquals (1023 , CommonUtil .binaryStringToInt ("1111111111" ), "10 bits all ones should convert to 1023" );
44+
45+ // Test with leading zeros
46+ assertEquals (10 , CommonUtil .binaryStringToInt ("00001010" ), "Binary with leading zeros should work correctly" );
47+ }
48+
49+ @ Test
50+ void testBinaryStringToIntWithInvalidInput () {
51+ // Test invalid characters
52+ assertThrows (IllegalArgumentException .class ,
53+ () -> CommonUtil .binaryStringToInt ("10201" ),
54+ "Should throw exception for non-binary character '2'" );
55+
56+ assertThrows (IllegalArgumentException .class ,
57+ () -> CommonUtil .binaryStringToInt ("abc" ),
58+ "Should throw exception for non-binary characters" );
59+ }
60+
61+ }
0 commit comments