- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathBcdConversionTest.java
85 lines (70 loc) · 2.74 KB
/
BcdConversionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
packagecom.thealgorithms.bitmanipulation;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
importorg.junit.jupiter.api.Test;
publicclassBcdConversionTest {
@Test
publicvoidtestBcdToDecimal() {
intdecimal = BcdConversion.bcdToDecimal(0x1234);
assertEquals(1234, decimal); // BCD 0x1234 should convert to decimal 1234
}
@Test
publicvoidtestDecimalToBcd() {
intbcd = BcdConversion.decimalToBcd(1234);
assertEquals(0x1234, bcd); // Decimal 1234 should convert to BCD 0x1234
}
@Test
publicvoidtestBcdToDecimalZero() {
intdecimal = BcdConversion.bcdToDecimal(0x0);
assertEquals(0, decimal); // BCD 0x0 should convert to decimal 0
}
@Test
publicvoidtestDecimalToBcdZero() {
intbcd = BcdConversion.decimalToBcd(0);
assertEquals(0x0, bcd); // Decimal 0 should convert to BCD 0x0
}
@Test
publicvoidtestBcdToDecimalSingleDigit() {
intdecimal = BcdConversion.bcdToDecimal(0x7);
assertEquals(7, decimal); // BCD 0x7 should convert to decimal 7
}
@Test
publicvoidtestDecimalToBcdSingleDigit() {
intbcd = BcdConversion.decimalToBcd(7);
assertEquals(0x7, bcd); // Decimal 7 should convert to BCD 0x7
}
@Test
publicvoidtestBcdToDecimalMaxValue() {
intdecimal = BcdConversion.bcdToDecimal(0x9999);
assertEquals(9999, decimal); // BCD 0x9999 should convert to decimal 9999
}
@Test
publicvoidtestDecimalToBcdMaxValue() {
intbcd = BcdConversion.decimalToBcd(9999);
assertEquals(0x9999, bcd); // Decimal 9999 should convert to BCD 0x9999
}
@Test
publicvoidtestBcdToDecimalInvalidHighDigit() {
// Testing invalid BCD input where one of the digits is > 9
assertThrows(IllegalArgumentException.class, () -> {
BcdConversion.bcdToDecimal(0x123A); // Invalid BCD, 'A' is not a valid digit
});
}
@Test
publicvoidtestDecimalToBcdInvalidValue() {
// Testing conversion for numbers greater than 9999, which cannot be represented in BCD
assertThrows(IllegalArgumentException.class, () -> {
BcdConversion.decimalToBcd(10000); // 10000 is too large for BCD representation
});
}
@Test
publicvoidtestBcdToDecimalLeadingZeroes() {
intdecimal = BcdConversion.bcdToDecimal(0x0234);
assertEquals(234, decimal); // BCD 0x0234 should convert to decimal 234, ignoring leading zero
}
@Test
publicvoidtestDecimalToBcdLeadingZeroes() {
intbcd = BcdConversion.decimalToBcd(234);
assertEquals(0x0234, bcd); // Decimal 234 should convert to BCD 0x0234
}
}