Skip to content

Commit 1ea95ff

Browse files
authored
Cleanup PerfectSquare and its tests (TheAlgorithms#4992)
1 parent 092ac57 commit 1ea95ff

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

src/main/java/com/thealgorithms/maths/PerfectSquare.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
/**
44
* https://en.wikipedia.org/wiki/Perfect_square
55
*/
6-
public class PerfectSquare {
7-
8-
public static void main(String[] args) {
9-
assert !isPerfectSquare(-1);
10-
assert !isPerfectSquare(3);
11-
assert !isPerfectSquare(5);
12-
assert isPerfectSquare(9);
13-
assert isPerfectSquare(100);
6+
public final class PerfectSquare {
7+
private PerfectSquare() {
148
}
159

1610
/**
@@ -20,8 +14,8 @@ public static void main(String[] args) {
2014
* @return <tt>true</tt> if {@code number} is perfect square, otherwise
2115
* <tt>false</tt>
2216
*/
23-
public static boolean isPerfectSquare(int number) {
24-
int sqrt = (int) Math.sqrt(number);
17+
public static boolean isPerfectSquare(final int number) {
18+
final int sqrt = (int) Math.sqrt(number);
2519
return sqrt * sqrt == number;
2620
}
2721
}
Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,21 @@
11
package com.thealgorithms.maths;
22

3-
import static org.junit.jupiter.api.Assertions.*;
4-
3+
import java.util.stream.Stream;
4+
import org.junit.jupiter.api.Assertions;
55
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.ValueSource;
68

79
public class PerfectSquareTest {
8-
9-
@Test
10-
public void TestPerfectSquareifiscorrect() {
11-
// Valid Partition
12-
int number = 9;
13-
14-
boolean result = PerfectSquare.isPerfectSquare(number);
15-
16-
assertTrue(result);
17-
}
18-
19-
@Test
20-
public void TestPerfectSquareifisnotcorrect() {
21-
// Invalid Partition 1
22-
int number = 3;
23-
24-
boolean result = PerfectSquare.isPerfectSquare(number);
25-
26-
assertFalse(result);
10+
@ParameterizedTest
11+
@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})
12+
void positiveTest(final int number) {
13+
Assertions.assertTrue(PerfectSquare.isPerfectSquare(number));
2714
}
2815

29-
@Test
30-
public void TestPerfectSquareifisNegativeNumber() {
31-
// Invalid Partition 2
32-
int number = -10;
33-
34-
boolean result = PerfectSquare.isPerfectSquare(number);
35-
36-
assertFalse(result);
16+
@ParameterizedTest
17+
@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})
18+
void negativeTest(final int number) {
19+
Assertions.assertFalse(PerfectSquare.isPerfectSquare(number));
3720
}
3821
}

0 commit comments

Comments
 (0)