mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Add tests, remove main
in SquareRootBinarySearch
(#5676)
This commit is contained in:
@ -1016,6 +1016,7 @@
|
||||
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
|
||||
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
|
||||
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
|
||||
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
|
||||
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
|
||||
* sorts
|
||||
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Given an integer x, find the square root of x. If x is not a perfect square,
|
||||
* then return floor(√x).
|
||||
@ -18,20 +16,6 @@ public final class SquareRootBinarySearch {
|
||||
private SquareRootBinarySearch() {
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the driver method.
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Enter a number you want to calculate square root of : ");
|
||||
int num = sc.nextInt();
|
||||
long ans = squareRoot(num);
|
||||
System.out.println("The square root is : " + ans);
|
||||
sc.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* This function calculates the floor of square root of a number. We use
|
||||
* Binary Search algorithm to calculate the square root in a more optimised
|
||||
@ -40,7 +24,7 @@ public final class SquareRootBinarySearch {
|
||||
* @param num Number
|
||||
* @return answer
|
||||
*/
|
||||
private static long squareRoot(long num) {
|
||||
static long squareRoot(long num) {
|
||||
if (num == 0 || num == 1) {
|
||||
return num;
|
||||
}
|
||||
|
@ -0,0 +1,57 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SquareRootBinarySearchTest {
|
||||
|
||||
@Test
|
||||
void testPerfectSquare() {
|
||||
long input = 16;
|
||||
long expected = 4;
|
||||
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 16 should be 4");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNonPerfectSquare() {
|
||||
long input = 15;
|
||||
long expected = 3;
|
||||
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 15 should be 3");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testZero() {
|
||||
long input = 0;
|
||||
long expected = 0;
|
||||
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 0 should be 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOne() {
|
||||
long input = 1;
|
||||
long expected = 1;
|
||||
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 1 should be 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLargeNumberPerfectSquare() {
|
||||
long input = 1000000;
|
||||
long expected = 1000;
|
||||
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 1000000 should be 1000");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLargeNumberNonPerfectSquare() {
|
||||
long input = 999999;
|
||||
long expected = 999;
|
||||
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of 999999 should be 999");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNegativeInput() {
|
||||
long input = -4;
|
||||
long expected = 0; // Assuming the implementation should return 0 for negative input
|
||||
assertEquals(expected, SquareRootBinarySearch.squareRoot(input), "Square root of negative number should return 0");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user