Add test case for HexaDecimalToBinary (#3683)

This commit is contained in:
Nandini Anagondi 2022-10-27 18:04:01 +05:30 committed by GitHub
parent 838916d8e9
commit 2c9edc95b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -5,13 +5,13 @@ public class HexaDecimalToBinary {
private final int LONG_BITS = 8;
public void convert(String numHex) {
public String convert(String numHex) {
// String a HexaDecimal:
int conHex = Integer.parseInt(numHex, 16);
// Hex a Binary:
String binary = Integer.toBinaryString(conHex);
// Output:
System.out.println(numHex + " = " + completeDigits(binary));
return completeDigits(binary);
}
public String completeDigits(String binNum) {
@ -39,7 +39,7 @@ public class HexaDecimalToBinary {
HexaDecimalToBinary objConvert = new HexaDecimalToBinary();
for (String num : hexNums) {
objConvert.convert(num);
System.out.println(num + " = " + objConvert.convert(num));
}
}
}

View File

@ -0,0 +1,14 @@
package com.thealgorithms.conversions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class HexaDecimalToBinaryTest {
@Test
public void testHexaDecimalToBinary(){
HexaDecimalToBinary hexaDecimalToBinary = new HexaDecimalToBinary();
assertEquals("1111111111111111111111111111111", hexaDecimalToBinary.convert("7fffffff"));
assertEquals("101010111100110111101111", hexaDecimalToBinary.convert("abcdef"));
}
}