mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Refactor BCD Conversion docs and add more tests (#5762)
This commit is contained in:
@ -1,65 +1,85 @@
|
||||
package com.thealgorithms.bitmanipulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the BcdConversion class.
|
||||
*/
|
||||
public class BcdConversionTest {
|
||||
|
||||
/**
|
||||
* Test the bcdToBinary method with a BCD number.
|
||||
*/
|
||||
@Test
|
||||
public void testBcdToBinary() {
|
||||
int binary = BcdConversion.bcdToBinary(0x1234);
|
||||
assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234
|
||||
public void testBcdToDecimal() {
|
||||
int decimal = BcdConversion.bcdToDecimal(0x1234);
|
||||
assertEquals(1234, decimal); // BCD 0x1234 should convert to decimal 1234
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the binaryToBcd method with a binary number.
|
||||
*/
|
||||
@Test
|
||||
public void testBinaryToBcd() {
|
||||
int bcd = BcdConversion.binaryToBcd(1234);
|
||||
assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234
|
||||
public void testDecimalToBcd() {
|
||||
int bcd = BcdConversion.decimalToBcd(1234);
|
||||
assertEquals(0x1234, bcd); // Decimal 1234 should convert to BCD 0x1234
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the bcdToBinary method with zero.
|
||||
*/
|
||||
@Test
|
||||
public void testBcdToBinaryZero() {
|
||||
int binary = BcdConversion.bcdToBinary(0x0);
|
||||
assertEquals(0, binary); // BCD 0x0 should convert to binary 0
|
||||
public void testBcdToDecimalZero() {
|
||||
int decimal = BcdConversion.bcdToDecimal(0x0);
|
||||
assertEquals(0, decimal); // BCD 0x0 should convert to decimal 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the binaryToBcd method with zero.
|
||||
*/
|
||||
@Test
|
||||
public void testBinaryToBcdZero() {
|
||||
int bcd = BcdConversion.binaryToBcd(0);
|
||||
assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0
|
||||
public void testDecimalToBcdZero() {
|
||||
int bcd = BcdConversion.decimalToBcd(0);
|
||||
assertEquals(0x0, bcd); // Decimal 0 should convert to BCD 0x0
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the bcdToBinary method with a single digit BCD number.
|
||||
*/
|
||||
@Test
|
||||
public void testBcdToBinarySingleDigit() {
|
||||
int binary = BcdConversion.bcdToBinary(0x7);
|
||||
assertEquals(7, binary); // BCD 0x7 should convert to binary 7
|
||||
public void testBcdToDecimalSingleDigit() {
|
||||
int decimal = BcdConversion.bcdToDecimal(0x7);
|
||||
assertEquals(7, decimal); // BCD 0x7 should convert to decimal 7
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the binaryToBcd method with a single digit binary number.
|
||||
*/
|
||||
@Test
|
||||
public void testBinaryToBcdSingleDigit() {
|
||||
int bcd = BcdConversion.binaryToBcd(7);
|
||||
assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7
|
||||
public void testDecimalToBcdSingleDigit() {
|
||||
int bcd = BcdConversion.decimalToBcd(7);
|
||||
assertEquals(0x7, bcd); // Decimal 7 should convert to BCD 0x7
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBcdToDecimalMaxValue() {
|
||||
int decimal = BcdConversion.bcdToDecimal(0x9999);
|
||||
assertEquals(9999, decimal); // BCD 0x9999 should convert to decimal 9999
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecimalToBcdMaxValue() {
|
||||
int bcd = BcdConversion.decimalToBcd(9999);
|
||||
assertEquals(0x9999, bcd); // Decimal 9999 should convert to BCD 0x9999
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBcdToDecimalInvalidHighDigit() {
|
||||
// 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
|
||||
public void testDecimalToBcdInvalidValue() {
|
||||
// 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
|
||||
public void testBcdToDecimalLeadingZeroes() {
|
||||
int decimal = BcdConversion.bcdToDecimal(0x0234);
|
||||
assertEquals(234, decimal); // BCD 0x0234 should convert to decimal 234, ignoring leading zero
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecimalToBcdLeadingZeroes() {
|
||||
int bcd = BcdConversion.decimalToBcd(234);
|
||||
assertEquals(0x0234, bcd); // Decimal 234 should convert to BCD 0x0234
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user