refactor: BinaryToOctal (#5338)

* refactor: BinaryToOctal

* checkstyle: fix formatting

* refactor: adding input correctness case, cover by tests. Renaming variable

---------

Co-authored-by: alxkm <alx@alx.com>
Co-authored-by: Bama Charan Chhandogi <b.c.chhandogi@gmail.com>
This commit is contained in:
Alex Klymenko
2024-08-17 21:06:45 +02:00
committed by GitHub
parent 25b6aebe45
commit e8985b3edb
2 changed files with 42 additions and 37 deletions

View File

@ -1,14 +1,24 @@
package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class BinaryToOctalTest {
@ParameterizedTest
@CsvSource({"0, 0", "1, 1", "10, 2", "111, 7", "1000, 10", "1111, 17", "110101, 65", "1010101, 125", "110110011, 663", "111111111, 777", "10010110, 226", "1011101, 135"})
void testConvertBinaryToOctal(int binary, String expectedOctal) {
assertEquals(expectedOctal, BinaryToOctal.convertBinaryToOctal(binary));
}
@Test
public void testBinaryToOctal() {
assertEquals("226", BinaryToOctal.convertBinaryToOctal(10010110));
assertEquals("135", BinaryToOctal.convertBinaryToOctal(1011101));
void testIncorrectInput() {
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(1234));
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(102));
assertThrows(IllegalArgumentException.class, () -> BinaryToOctal.convertBinaryToOctal(-1010));
}
}