Increase test coverage (fixes #3895 #3896) (#3897)

This commit is contained in:
a-kbd 2023-03-05 20:03:08 +01:00 committed by GitHub
parent 87f9ebcb29
commit dd949e9b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 166 additions and 8 deletions

View File

@ -11,6 +11,9 @@ public static int divide(int dividend, int divisor) {
long new_dividend_1 = dividend; long new_dividend_1 = dividend;
long new_divisor_1 = divisor; long new_divisor_1 = divisor;
if(divisor == 0){
return 0;
}
if (dividend < 0) { if (dividend < 0) {
new_dividend_1 = new_dividend_1 * -1; new_dividend_1 = new_dividend_1 * -1;
} }

View File

@ -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);
}
}

View File

@ -6,20 +6,50 @@ import static org.junit.jupiter.api.Assertions.*;
public class LongDivisionTest { public class LongDivisionTest {
// Requirement: Dividend (positive) is greater than divisor (positive), returns correct integer after division
@Test @Test
void testOne() { void testOne() {
assertEquals(3, LongDivision.divide(10,3)); assertEquals(3, LongDivision.divide(10,3));
} }
@Test // Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division
@Test
void testTwo() { void testTwo() {
assertEquals(-2, LongDivision.divide(7,-3)); assertEquals(-2, LongDivision.divide(7,-3));
} }
// Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division
@Test // Basically the same as in the first test
@Test
void testThree() { void testThree() {
assertEquals(10, LongDivision.divide(105,10)); 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));
}
} }

View File

@ -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);
}
}

View File

@ -11,15 +11,33 @@ public class MyAtoiTest {
assertEquals(42, MyAtoi.myAtoi("42")); assertEquals(42, MyAtoi.myAtoi("42"));
} }
@Test @Test
void testTwo() { void testTwo() {
assertEquals(-42, MyAtoi.myAtoi(" -42")); assertEquals(-42, MyAtoi.myAtoi(" -42"));
} }
@Test
@Test
void testThree() { void testThree() {
assertEquals(4193, MyAtoi.myAtoi("4193 with words")); 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 "));
}
} }