Skip to content

Commit

Permalink
Increase BaseUtil coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 2, 2023
1 parent 877d279 commit d4bb435
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/adventofcode/flashk/common/BaseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static String hexToBinary(String hex) {
}

/**
* Converts from hexadecimal to binary adding zero padding at the leading.
* Converts from hexadecimal to binary and adds zero padding at the leading.
* <p>
* Any hexadecimal number can be represented with 4 binary digits (<code>6 hex = 0110 bin</code>).
* </p>
Expand All @@ -53,12 +53,13 @@ public static String hexToBinary(String hex) {
* BaseUtil.hexToBinaryPadLeft("6") = "0110"
* </pre>
* @param hex the hexadecimal code
* @return the translated binary code
* @return the translated binary code, it will always have a multiple of 4 digits.
* @see #hexToBinary(String)
*/
public static String hexToBinaryPadLeft(String hex) {
String unpaddedBinary = BaseUtil.hexToBinary(hex);
int size = hex.length() * 4;
return String.format("%"+size+"s", unpaddedBinary).replace(" ", "0");
String formatSize = "%" + size + "s";
return String.format(formatSize, unpaddedBinary).replace(" ", "0");
}
}
62 changes: 62 additions & 0 deletions src/test/java/com/adventofcode/flashk/common/BaseUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.adventofcode.flashk.common;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class BaseUtilTest {

@Test
void binaryToDecIntegerTest() {
assertEquals(0, BaseUtil.binaryToDecInteger("0"));
assertEquals(1, BaseUtil.binaryToDecInteger("1"));
assertEquals(2, BaseUtil.binaryToDecInteger("10"));
assertEquals(5, BaseUtil.binaryToDecInteger("101"));
assertEquals(8, BaseUtil.binaryToDecInteger("1000"));
assertEquals(10, BaseUtil.binaryToDecInteger("1010"));
assertEquals(15, BaseUtil.binaryToDecInteger("1111"));
}

@Test
void binaryToDecTest() {
assertEquals(0, BaseUtil.binaryToDec("0"));
assertEquals(1, BaseUtil.binaryToDec("1"));
assertEquals(2, BaseUtil.binaryToDec("10"));
assertEquals(5, BaseUtil.binaryToDec("101"));
assertEquals(8, BaseUtil.binaryToDec("1000"));
assertEquals(10, BaseUtil.binaryToDec("1010"));
assertEquals(15, BaseUtil.binaryToDec("1111"));
}

@Test
void hexToBinaryTest() {
assertEquals("0", BaseUtil.hexToBinary("0"));
assertEquals("1", BaseUtil.hexToBinary("1"));
assertEquals("10", BaseUtil.hexToBinary("2"));
assertEquals("101", BaseUtil.hexToBinary("5"));
assertEquals("1000", BaseUtil.hexToBinary("8"));
assertEquals("1010", BaseUtil.hexToBinary("A"));
assertEquals("1011", BaseUtil.hexToBinary("B"));
assertEquals("1100", BaseUtil.hexToBinary("C"));
assertEquals("1101", BaseUtil.hexToBinary("D"));
assertEquals("1110", BaseUtil.hexToBinary("E"));
assertEquals("1111", BaseUtil.hexToBinary("F"));
assertEquals("100110110", BaseUtil.hexToBinary("136"));
}

@Test
void hexToBinaryPadLeft() {
assertEquals("0000", BaseUtil.hexToBinaryPadLeft("0"));
assertEquals("0001", BaseUtil.hexToBinaryPadLeft("1"));
assertEquals("0010", BaseUtil.hexToBinaryPadLeft("2"));
assertEquals("0101", BaseUtil.hexToBinaryPadLeft("5"));
assertEquals("1000", BaseUtil.hexToBinaryPadLeft("8"));
assertEquals("1010", BaseUtil.hexToBinaryPadLeft("A"));
assertEquals("1011", BaseUtil.hexToBinaryPadLeft("B"));
assertEquals("1100", BaseUtil.hexToBinaryPadLeft("C"));
assertEquals("1101", BaseUtil.hexToBinaryPadLeft("D"));
assertEquals("1110", BaseUtil.hexToBinaryPadLeft("E"));
assertEquals("1111", BaseUtil.hexToBinaryPadLeft("F"));
assertEquals("000100110110", BaseUtil.hexToBinaryPadLeft("136"));
}
}

0 comments on commit d4bb435

Please sign in to comment.