Skip to content

Commit 9823ea1

Browse files
Add unit tests for SquareRootWithBabylonianMethod
Adds JUnit 5 unit tests for `SquareRootWithBabylonianMethod`, which previously had no test coverage. Tests cover perfect squares, non-perfect squares, and a range of values from 1 to 1000, comparing the result against Math.sqrt with a small delta. #HSFDPMUW
1 parent 12aab70 commit 9823ea1

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class SquareRootWithBabylonianMethodTest {
8+
9+
private static final float DELTA = 1e-3f;
10+
11+
@Test
12+
void testPerfectSquares() {
13+
assertEquals(1.0f, SquareRootWithBabylonianMethod.squareRoot(1.0f), DELTA);
14+
assertEquals(2.0f, SquareRootWithBabylonianMethod.squareRoot(4.0f), DELTA);
15+
assertEquals(3.0f, SquareRootWithBabylonianMethod.squareRoot(9.0f), DELTA);
16+
assertEquals(5.0f, SquareRootWithBabylonianMethod.squareRoot(25.0f), DELTA);
17+
assertEquals(12.0f, SquareRootWithBabylonianMethod.squareRoot(144.0f), DELTA);
18+
}
19+
20+
@Test
21+
void testNonPerfectSquares() {
22+
assertEquals((float) Math.sqrt(2.0), SquareRootWithBabylonianMethod.squareRoot(2.0f), DELTA);
23+
assertEquals((float) Math.sqrt(50.0), SquareRootWithBabylonianMethod.squareRoot(50.0f), DELTA);
24+
assertEquals((float) Math.sqrt(99.0), SquareRootWithBabylonianMethod.squareRoot(99.0f), DELTA);
25+
}
26+
27+
@Test
28+
void testMatchesMathSqrtForRangeOfValues() {
29+
for (float num = 1.0f; num <= 1000.0f; num += 1.0f) {
30+
assertEquals((float) Math.sqrt(num), SquareRootWithBabylonianMethod.squareRoot(num), DELTA);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)