Skip to content

Commit a663e66

Browse files
authored
Add tests, remove main in SquareRootBinarySearch (#5676)
1 parent 3828577 commit a663e66

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@
10161016
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
10171017
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
10181018
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
1019+
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
10191020
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
10201021
* sorts
10211022
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)

src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.searches;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Given an integer x, find the square root of x. If x is not a perfect square,
75
* then return floor(√x).
@@ -18,20 +16,6 @@ public final class SquareRootBinarySearch {
1816
private SquareRootBinarySearch() {
1917
}
2018

21-
/**
22-
* This is the driver method.
23-
*
24-
* @param args Command line arguments
25-
*/
26-
public static void main(String[] args) {
27-
Scanner sc = new Scanner(System.in);
28-
System.out.print("Enter a number you want to calculate square root of : ");
29-
int num = sc.nextInt();
30-
long ans = squareRoot(num);
31-
System.out.println("The square root is : " + ans);
32-
sc.close();
33-
}
34-
3519
/**
3620
* This function calculates the floor of square root of a number. We use
3721
* Binary Search algorithm to calculate the square root in a more optimised
@@ -40,7 +24,7 @@ public static void main(String[] args) {
4024
* @param num Number
4125
* @return answer
4226
*/
43-
private static long squareRoot(long num) {
27+
static long squareRoot(long num) {
4428
if (num == 0 || num == 1) {
4529
return num;
4630
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SquareRootBinarySearchTest {
8+
9+
@Test
10+
void testPerfectSquare() {
11+
long input = 16;
12+
long expected = 4;
13+
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 16 should be 4");
14+
}
15+
16+
@Test
17+
void testNonPerfectSquare() {
18+
long input = 15;
19+
long expected = 3;
20+
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 15 should be 3");
21+
}
22+
23+
@Test
24+
void testZero() {
25+
long input = 0;
26+
long expected = 0;
27+
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 0 should be 0");
28+
}
29+
30+
@Test
31+
void testOne() {
32+
long input = 1;
33+
long expected = 1;
34+
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 1 should be 1");
35+
}
36+
37+
@Test
38+
void testLargeNumberPerfectSquare() {
39+
long input = 1000000;
40+
long expected = 1000;
41+
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 1000000 should be 1000");
42+
}
43+
44+
@Test
45+
void testLargeNumberNonPerfectSquare() {
46+
long input = 999999;
47+
long expected = 999;
48+
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 999999 should be 999");
49+
}
50+
51+
@Test
52+
void testNegativeInput() {
53+
long input = -4;
54+
long expected = 0; // Assuming the implementation should return 0 for negative input
55+
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of negative number should return 0");
56+
}
57+
}

0 commit comments

Comments
 (0)