From 2c9edc95b88cb76c256b8fb01a8f9912717d708b Mon Sep 17 00:00:00 2001 From: Nandini Anagondi <91892829+naanagon@users.noreply.github.com> Date: Thu, 27 Oct 2022 18:04:01 +0530 Subject: [PATCH] Add test case for HexaDecimalToBinary (#3683) --- .../conversions/HexaDecimalToBinary.java | 6 +++--- .../conversions/HexaDecimalToBinaryTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java diff --git a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java index fde14206..5372c95f 100644 --- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java @@ -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)); } } } diff --git a/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java b/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java new file mode 100644 index 00000000..57379323 --- /dev/null +++ b/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java @@ -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")); + } +}