|
1 | 1 | package com.thealgorithms.bitmanipulation; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
5 | 6 | import org.junit.jupiter.api.Test; |
6 | 7 |
|
7 | | -/** |
8 | | - * Unit tests for the BcdConversion class. |
9 | | - */ |
10 | 8 | public class BcdConversionTest { |
11 | 9 |
|
12 | | - /** |
13 | | - * Test the bcdToBinary method with a BCD number. |
14 | | - */ |
15 | 10 | @Test |
16 | 11 | public void testBcdToBinary() { |
17 | 12 | int binary = BcdConversion.bcdToBinary(0x1234); |
18 | 13 | assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234 |
19 | 14 | } |
20 | 15 |
|
21 | | - /** |
22 | | - * Test the binaryToBcd method with a binary number. |
23 | | - */ |
24 | 16 | @Test |
25 | 17 | public void testBinaryToBcd() { |
26 | 18 | int bcd = BcdConversion.binaryToBcd(1234); |
27 | 19 | assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234 |
28 | 20 | } |
29 | 21 |
|
30 | | - /** |
31 | | - * Test the bcdToBinary method with zero. |
32 | | - */ |
33 | 22 | @Test |
34 | 23 | public void testBcdToBinaryZero() { |
35 | 24 | int binary = BcdConversion.bcdToBinary(0x0); |
36 | 25 | assertEquals(0, binary); // BCD 0x0 should convert to binary 0 |
37 | 26 | } |
38 | 27 |
|
39 | | - /** |
40 | | - * Test the binaryToBcd method with zero. |
41 | | - */ |
42 | 28 | @Test |
43 | 29 | public void testBinaryToBcdZero() { |
44 | 30 | int bcd = BcdConversion.binaryToBcd(0); |
45 | 31 | assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0 |
46 | 32 | } |
47 | 33 |
|
48 | | - /** |
49 | | - * Test the bcdToBinary method with a single digit BCD number. |
50 | | - */ |
51 | 34 | @Test |
52 | 35 | public void testBcdToBinarySingleDigit() { |
53 | 36 | int binary = BcdConversion.bcdToBinary(0x7); |
54 | 37 | assertEquals(7, binary); // BCD 0x7 should convert to binary 7 |
55 | 38 | } |
56 | 39 |
|
57 | | - /** |
58 | | - * Test the binaryToBcd method with a single digit binary number. |
59 | | - */ |
60 | 40 | @Test |
61 | 41 | public void testBinaryToBcdSingleDigit() { |
62 | 42 | int bcd = BcdConversion.binaryToBcd(7); |
63 | 43 | assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7 |
64 | 44 | } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testBcdToBinaryMaxValue() { |
| 48 | + int binary = BcdConversion.bcdToBinary(0x9999); |
| 49 | + assertEquals(9999, binary); // BCD 0x9999 should convert to binary 9999 |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testBinaryToBcdMaxValue() { |
| 54 | + int bcd = BcdConversion.binaryToBcd(9999); |
| 55 | + assertEquals(0x9999, bcd); // Binary 9999 should convert to BCD 0x9999 |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testBcdToBinaryInvalidHighDigit() { |
| 60 | + // Testing invalid BCD input where one of the digits is > 9 |
| 61 | + assertThrows(IllegalArgumentException.class, () -> { |
| 62 | + BcdConversion.bcdToBinary(0x123A); // Invalid BCD, 'A' is not a valid digit |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testBinaryToBcdInvalidValue() { |
| 68 | + // Testing conversion for numbers greater than 9999, which cannot be represented in BCD |
| 69 | + assertThrows(IllegalArgumentException.class, () -> { |
| 70 | + BcdConversion.binaryToBcd(10000); // 10000 is too large for BCD representation |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testBcdToBinaryLeadingZeroes() { |
| 76 | + int binary = BcdConversion.bcdToBinary(0x0234); |
| 77 | + assertEquals(234, binary); // BCD 0x0234 should convert to binary 234, ignoring leading zero |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testBinaryToBcdLeadingZeroes() { |
| 82 | + int bcd = BcdConversion.binaryToBcd(234); |
| 83 | + assertEquals(0x0234, bcd); // Binary 234 should convert to BCD 0x0234 |
| 84 | + } |
65 | 85 | } |
0 commit comments