|
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 |
| - public void testBcdToBinary() { |
17 |
| - int binary = BcdConversion.bcdToBinary(0x1234); |
18 |
| - assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234 |
| 11 | + public void testBcdToDecimal() { |
| 12 | + int decimal = BcdConversion.bcdToDecimal(0x1234); |
| 13 | + assertEquals(1234, decimal); // BCD 0x1234 should convert to decimal 1234 |
19 | 14 | }
|
20 | 15 |
|
21 |
| - /** |
22 |
| - * Test the binaryToBcd method with a binary number. |
23 |
| - */ |
24 | 16 | @Test
|
25 |
| - public void testBinaryToBcd() { |
26 |
| - int bcd = BcdConversion.binaryToBcd(1234); |
27 |
| - assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234 |
| 17 | + public void testDecimalToBcd() { |
| 18 | + int bcd = BcdConversion.decimalToBcd(1234); |
| 19 | + assertEquals(0x1234, bcd); // Decimal 1234 should convert to BCD 0x1234 |
28 | 20 | }
|
29 | 21 |
|
30 |
| - /** |
31 |
| - * Test the bcdToBinary method with zero. |
32 |
| - */ |
33 | 22 | @Test
|
34 |
| - public void testBcdToBinaryZero() { |
35 |
| - int binary = BcdConversion.bcdToBinary(0x0); |
36 |
| - assertEquals(0, binary); // BCD 0x0 should convert to binary 0 |
| 23 | + public void testBcdToDecimalZero() { |
| 24 | + int decimal = BcdConversion.bcdToDecimal(0x0); |
| 25 | + assertEquals(0, decimal); // BCD 0x0 should convert to decimal 0 |
37 | 26 | }
|
38 | 27 |
|
39 |
| - /** |
40 |
| - * Test the binaryToBcd method with zero. |
41 |
| - */ |
42 | 28 | @Test
|
43 |
| - public void testBinaryToBcdZero() { |
44 |
| - int bcd = BcdConversion.binaryToBcd(0); |
45 |
| - assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0 |
| 29 | + public void testDecimalToBcdZero() { |
| 30 | + int bcd = BcdConversion.decimalToBcd(0); |
| 31 | + assertEquals(0x0, bcd); // Decimal 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 |
| - public void testBcdToBinarySingleDigit() { |
53 |
| - int binary = BcdConversion.bcdToBinary(0x7); |
54 |
| - assertEquals(7, binary); // BCD 0x7 should convert to binary 7 |
| 35 | + public void testBcdToDecimalSingleDigit() { |
| 36 | + int decimal = BcdConversion.bcdToDecimal(0x7); |
| 37 | + assertEquals(7, decimal); // BCD 0x7 should convert to decimal 7 |
55 | 38 | }
|
56 | 39 |
|
57 |
| - /** |
58 |
| - * Test the binaryToBcd method with a single digit binary number. |
59 |
| - */ |
60 | 40 | @Test
|
61 |
| - public void testBinaryToBcdSingleDigit() { |
62 |
| - int bcd = BcdConversion.binaryToBcd(7); |
63 |
| - assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7 |
| 41 | + public void testDecimalToBcdSingleDigit() { |
| 42 | + int bcd = BcdConversion.decimalToBcd(7); |
| 43 | + assertEquals(0x7, bcd); // Decimal 7 should convert to BCD 0x7 |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testBcdToDecimalMaxValue() { |
| 48 | + int decimal = BcdConversion.bcdToDecimal(0x9999); |
| 49 | + assertEquals(9999, decimal); // BCD 0x9999 should convert to decimal 9999 |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testDecimalToBcdMaxValue() { |
| 54 | + int bcd = BcdConversion.decimalToBcd(9999); |
| 55 | + assertEquals(0x9999, bcd); // Decimal 9999 should convert to BCD 0x9999 |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testBcdToDecimalInvalidHighDigit() { |
| 60 | + // Testing invalid BCD input where one of the digits is > 9 |
| 61 | + assertThrows(IllegalArgumentException.class, () -> { |
| 62 | + BcdConversion.bcdToDecimal(0x123A); // Invalid BCD, 'A' is not a valid digit |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testDecimalToBcdInvalidValue() { |
| 68 | + // Testing conversion for numbers greater than 9999, which cannot be represented in BCD |
| 69 | + assertThrows(IllegalArgumentException.class, () -> { |
| 70 | + BcdConversion.decimalToBcd(10000); // 10000 is too large for BCD representation |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testBcdToDecimalLeadingZeroes() { |
| 76 | + int decimal = BcdConversion.bcdToDecimal(0x0234); |
| 77 | + assertEquals(234, decimal); // BCD 0x0234 should convert to decimal 234, ignoring leading zero |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testDecimalToBcdLeadingZeroes() { |
| 82 | + int bcd = BcdConversion.decimalToBcd(234); |
| 83 | + assertEquals(0x0234, bcd); // Decimal 234 should convert to BCD 0x0234 |
64 | 84 | }
|
65 | 85 | }
|
0 commit comments