diff --git a/src/main/java/com/thealgorithms/maths/LongDivision.java b/src/main/java/com/thealgorithms/maths/LongDivision.java index 88a0a261..2578fe2e 100644 --- a/src/main/java/com/thealgorithms/maths/LongDivision.java +++ b/src/main/java/com/thealgorithms/maths/LongDivision.java @@ -11,6 +11,9 @@ public static int divide(int dividend, int divisor) { long new_dividend_1 = dividend; long new_divisor_1 = divisor; + if(divisor == 0){ + return 0; + } if (dividend < 0) { new_dividend_1 = new_dividend_1 * -1; } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java new file mode 100644 index 00000000..2ed3b534 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java @@ -0,0 +1,78 @@ +package com.thealgorithms.datastructures.trees; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class BinaryTreeTest { + + //checks that adding populating the tree and searching for data + //retrieves the expected data +@Test +void test1(){ + BinaryTree t = new BinaryTree(); + t.put(3); + t.put(5); + t.put(7); + t.put(9); + t.put(12); + + + assertEquals(t.find(5).data, 5); + assertEquals(t.find(7).data, 7); +} + + //checks that removing data from the tree + //properly removes and makes the new root the expected new root +@Test +void test2(){ + BinaryTree t = new BinaryTree(); + t.put(3); + t.put(5); + t.put(7); + t.put(9); + t.put(12); + t.remove(3); + t.remove(5); + t.remove(7); + + + assertEquals(t.getRoot().data, 9); +} + +//checks that removing an unexistend node returns false +// as specified by the documentation of the function +@Test +void test3(){ + BinaryTree t = new BinaryTree(); + t.put(3); + t.put(5); + t.put(7); + t.put(9); + t.put(12); + + assertEquals(t.remove(9), true); + assertEquals(t.remove(398745987), false); +} + +//check if the bfs, inOrder, preOrder and postOrder functions +//worg as expected, also increases the coverage measures in +//JaCoCo +@Test +void test4(){ + BinaryTree t = new BinaryTree(); + t.put(3); + t.put(5); + t.put(7); + t.put(9); + t.put(12); + + t.bfs(t.find(12)); + t.inOrder(t.getRoot()); + t.preOrder(t.getRoot()); + t.postOrder(t.getRoot()); + + assertEquals(t.remove(9), true); + assertEquals(t.remove(398745987), false); +} +} diff --git a/src/test/java/com/thealgorithms/maths/LongDivisionTest.java b/src/test/java/com/thealgorithms/maths/LongDivisionTest.java index c35b606a..65ee328c 100644 --- a/src/test/java/com/thealgorithms/maths/LongDivisionTest.java +++ b/src/test/java/com/thealgorithms/maths/LongDivisionTest.java @@ -5,21 +5,51 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class LongDivisionTest { - + + // Requirement: Dividend (positive) is greater than divisor (positive), returns correct integer after division @Test void testOne() { assertEquals(3, LongDivision.divide(10,3)); } - - @Test + + // Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division + @Test void testTwo() { assertEquals(-2, LongDivision.divide(7,-3)); } - - @Test + // Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division + // Basically the same as in the first test + @Test void testThree() { assertEquals(10, LongDivision.divide(105,10)); } + + // Requirement: Dividend (negative), divisor (positive), returns correct integer after division + // Tests the case where the dividend is less than 0. + @Test + void testNegativeDividend() { + assertEquals(-1, LongDivision.divide(-5,3)); + } + // Requirement: Dividend (positive), divisor (positive), returns correct integer after division + // Tests the case where the dividend is less than the divisor. The test should return 0 in this case. + @Test + void testDividendLessThanDivisor() { + assertEquals(0, LongDivision.divide(3,5)); + } + + // Requirement: Dividend (neither), divisor (positive), returns correct integer after division + // Tests the case where the dividend is 0. This should return a 0. + @Test + void testDividendIsZero() { + assertEquals(0, LongDivision.divide(0,5)); + } + + // Requirement: Dividend (positive), divisor (neither), returns correct integer after division + // Tests the case where the divisor is 0. This should return a 0. + @Test + void testDivisionByZero() { + assertEquals(0, LongDivision.divide(5,0)); + } } diff --git a/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java b/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java new file mode 100644 index 00000000..906ab829 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java @@ -0,0 +1,29 @@ + +package com.thealgorithms.others; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CRCAlgorithmTest { + + @Test + void test1(){ + CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 0.0); + + //A bit-error rate of 0.0 should not provide any wrong messages + c.changeMess(); + c.divideMessageWithP(false); + assertEquals(c.getWrongMess(), 0); + } + + @Test + void test2(){ + CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 1.0); + + //A bit error rate of 1.0 should not provide any correct messages + c.changeMess(); + c.divideMessageWithP(false); + assertEquals(c.getCorrectMess(), 0); + } +} diff --git a/src/test/java/com/thealgorithms/strings/MyAtoiTest.java b/src/test/java/com/thealgorithms/strings/MyAtoiTest.java index ef092ab2..29dda8c0 100644 --- a/src/test/java/com/thealgorithms/strings/MyAtoiTest.java +++ b/src/test/java/com/thealgorithms/strings/MyAtoiTest.java @@ -11,15 +11,33 @@ public class MyAtoiTest { assertEquals(42, MyAtoi.myAtoi("42")); } - @Test + @Test void testTwo() { assertEquals(-42, MyAtoi.myAtoi(" -42")); } - - @Test + @Test void testThree() { assertEquals(4193, MyAtoi.myAtoi("4193 with words")); } + @Test + void testFour() { + assertEquals(0, MyAtoi.myAtoi("0")); + } + + @Test + void testFive() { + assertEquals(5678, MyAtoi.myAtoi("5678")); + } + + @Test + void testSix() { + assertEquals(42, MyAtoi.myAtoi("+42")); + } + + @Test + void testSeven() { + assertEquals(0, MyAtoi.myAtoi(" +0 ")); + } }