Skip to content

Add tests, remove main in SquareRootBinarySearch #5676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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
Expand All @@ -40,7 +24,7 @@ public static void main(String[] args) {
* @param num Number
* @return answer
*/
private static long squareRoot(long num) {
static long squareRoot(long num) {
if (num == 0 || num == 1) {
return num;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}