Skip to content

Commit dd949e9

Browse files
authored
Increase test coverage (fixes #3895 #3896) (#3897)
1 parent 87f9ebc commit dd949e9

File tree

5 files changed

+166
-8
lines changed

5 files changed

+166
-8
lines changed

src/main/java/com/thealgorithms/maths/LongDivision.java

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public static int divide(int dividend, int divisor) {
1111
long new_dividend_1 = dividend;
1212
long new_divisor_1 = divisor;
1313

14+
if(divisor == 0){
15+
return 0;
16+
}
1417
if (dividend < 0) {
1518
new_dividend_1 = new_dividend_1 * -1;
1619
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class BinaryTreeTest {
8+
9+
//checks that adding populating the tree and searching for data
10+
//retrieves the expected data
11+
@Test
12+
void test1(){
13+
BinaryTree t = new BinaryTree();
14+
t.put(3);
15+
t.put(5);
16+
t.put(7);
17+
t.put(9);
18+
t.put(12);
19+
20+
21+
assertEquals(t.find(5).data, 5);
22+
assertEquals(t.find(7).data, 7);
23+
}
24+
25+
//checks that removing data from the tree
26+
//properly removes and makes the new root the expected new root
27+
@Test
28+
void test2(){
29+
BinaryTree t = new BinaryTree();
30+
t.put(3);
31+
t.put(5);
32+
t.put(7);
33+
t.put(9);
34+
t.put(12);
35+
t.remove(3);
36+
t.remove(5);
37+
t.remove(7);
38+
39+
40+
assertEquals(t.getRoot().data, 9);
41+
}
42+
43+
//checks that removing an unexistend node returns false
44+
// as specified by the documentation of the function
45+
@Test
46+
void test3(){
47+
BinaryTree t = new BinaryTree();
48+
t.put(3);
49+
t.put(5);
50+
t.put(7);
51+
t.put(9);
52+
t.put(12);
53+
54+
assertEquals(t.remove(9), true);
55+
assertEquals(t.remove(398745987), false);
56+
}
57+
58+
//check if the bfs, inOrder, preOrder and postOrder functions
59+
//worg as expected, also increases the coverage measures in
60+
//JaCoCo
61+
@Test
62+
void test4(){
63+
BinaryTree t = new BinaryTree();
64+
t.put(3);
65+
t.put(5);
66+
t.put(7);
67+
t.put(9);
68+
t.put(12);
69+
70+
t.bfs(t.find(12));
71+
t.inOrder(t.getRoot());
72+
t.preOrder(t.getRoot());
73+
t.postOrder(t.getRoot());
74+
75+
assertEquals(t.remove(9), true);
76+
assertEquals(t.remove(398745987), false);
77+
}
78+
}

src/test/java/com/thealgorithms/maths/LongDivisionTest.java

+35-5
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,51 @@
55
import static org.junit.jupiter.api.Assertions.*;
66

77
public class LongDivisionTest {
8-
8+
9+
// Requirement: Dividend (positive) is greater than divisor (positive), returns correct integer after division
910
@Test
1011
void testOne() {
1112
assertEquals(3, LongDivision.divide(10,3));
1213
}
13-
14-
@Test
14+
15+
// Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division
16+
@Test
1517
void testTwo() {
1618
assertEquals(-2, LongDivision.divide(7,-3));
1719
}
1820

19-
20-
@Test
21+
// Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division
22+
// Basically the same as in the first test
23+
@Test
2124
void testThree() {
2225
assertEquals(10, LongDivision.divide(105,10));
2326
}
27+
28+
// Requirement: Dividend (negative), divisor (positive), returns correct integer after division
29+
// Tests the case where the dividend is less than 0.
30+
@Test
31+
void testNegativeDividend() {
32+
assertEquals(-1, LongDivision.divide(-5,3));
33+
}
2434

35+
// Requirement: Dividend (positive), divisor (positive), returns correct integer after division
36+
// Tests the case where the dividend is less than the divisor. The test should return 0 in this case.
37+
@Test
38+
void testDividendLessThanDivisor() {
39+
assertEquals(0, LongDivision.divide(3,5));
40+
}
41+
42+
// Requirement: Dividend (neither), divisor (positive), returns correct integer after division
43+
// Tests the case where the dividend is 0. This should return a 0.
44+
@Test
45+
void testDividendIsZero() {
46+
assertEquals(0, LongDivision.divide(0,5));
47+
}
48+
49+
// Requirement: Dividend (positive), divisor (neither), returns correct integer after division
50+
// Tests the case where the divisor is 0. This should return a 0.
51+
@Test
52+
void testDivisionByZero() {
53+
assertEquals(0, LongDivision.divide(5,0));
54+
}
2555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
package com.thealgorithms.others;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class CRCAlgorithmTest {
9+
10+
@Test
11+
void test1(){
12+
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 0.0);
13+
14+
//A bit-error rate of 0.0 should not provide any wrong messages
15+
c.changeMess();
16+
c.divideMessageWithP(false);
17+
assertEquals(c.getWrongMess(), 0);
18+
}
19+
20+
@Test
21+
void test2(){
22+
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 1.0);
23+
24+
//A bit error rate of 1.0 should not provide any correct messages
25+
c.changeMess();
26+
c.divideMessageWithP(false);
27+
assertEquals(c.getCorrectMess(), 0);
28+
}
29+
}

src/test/java/com/thealgorithms/strings/MyAtoiTest.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,33 @@ void testOne() {
1111
assertEquals(42, MyAtoi.myAtoi("42"));
1212
}
1313

14-
@Test
14+
@Test
1515
void testTwo() {
1616
assertEquals(-42, MyAtoi.myAtoi(" -42"));
1717
}
1818

19-
20-
@Test
19+
@Test
2120
void testThree() {
2221
assertEquals(4193, MyAtoi.myAtoi("4193 with words"));
2322
}
2423

24+
@Test
25+
void testFour() {
26+
assertEquals(0, MyAtoi.myAtoi("0"));
27+
}
28+
29+
@Test
30+
void testFive() {
31+
assertEquals(5678, MyAtoi.myAtoi("5678"));
32+
}
33+
34+
@Test
35+
void testSix() {
36+
assertEquals(42, MyAtoi.myAtoi("+42"));
37+
}
38+
39+
@Test
40+
void testSeven() {
41+
assertEquals(0, MyAtoi.myAtoi(" +0 "));
42+
}
2543
}

0 commit comments

Comments
 (0)