Cleanup PerfectSquare
and its tests (#4992)
This commit is contained in:
parent
092ac5795b
commit
1ea95ffa92
@ -3,14 +3,8 @@ package com.thealgorithms.maths;
|
||||
/**
|
||||
* https://en.wikipedia.org/wiki/Perfect_square
|
||||
*/
|
||||
public class PerfectSquare {
|
||||
|
||||
public static void main(String[] args) {
|
||||
assert !isPerfectSquare(-1);
|
||||
assert !isPerfectSquare(3);
|
||||
assert !isPerfectSquare(5);
|
||||
assert isPerfectSquare(9);
|
||||
assert isPerfectSquare(100);
|
||||
public final class PerfectSquare {
|
||||
private PerfectSquare() {
|
||||
}
|
||||
|
||||
/**
|
||||
@ -20,8 +14,8 @@ public class PerfectSquare {
|
||||
* @return <tt>true</tt> if {@code number} is perfect square, otherwise
|
||||
* <tt>false</tt>
|
||||
*/
|
||||
public static boolean isPerfectSquare(int number) {
|
||||
int sqrt = (int) Math.sqrt(number);
|
||||
public static boolean isPerfectSquare(final int number) {
|
||||
final int sqrt = (int) Math.sqrt(number);
|
||||
return sqrt * sqrt == number;
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,21 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
public class PerfectSquareTest {
|
||||
|
||||
@Test
|
||||
public void TestPerfectSquareifiscorrect() {
|
||||
// Valid Partition
|
||||
int number = 9;
|
||||
|
||||
boolean result = PerfectSquare.isPerfectSquare(number);
|
||||
|
||||
assertTrue(result);
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 1, 2 * 2, 3 * 3, 4 * 4, 5 * 5, 6 * 6, 7 * 7, 8 * 8, 9 * 9, 10 * 10, 11 * 11, 123 * 123})
|
||||
void positiveTest(final int number) {
|
||||
Assertions.assertTrue(PerfectSquare.isPerfectSquare(number));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestPerfectSquareifisnotcorrect() {
|
||||
// Invalid Partition 1
|
||||
int number = 3;
|
||||
|
||||
boolean result = PerfectSquare.isPerfectSquare(number);
|
||||
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestPerfectSquareifisNegativeNumber() {
|
||||
// Invalid Partition 2
|
||||
int number = -10;
|
||||
|
||||
boolean result = PerfectSquare.isPerfectSquare(number);
|
||||
|
||||
assertFalse(result);
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, -2, -3, -4, -5, -100, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 15, 17, 99, 101, 257, 999, 1001})
|
||||
void negativeTest(final int number) {
|
||||
Assertions.assertFalse(PerfectSquare.isPerfectSquare(number));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user