Skip to content

Commit 4b45ac7

Browse files
authored
Add unit tests for PalindromeNumber (TheAlgorithms#4227)
1 parent 8862a4d commit 4b45ac7

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@
655655
* [MedianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MedianTest.java)
656656
* [MobiusFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MobiusFunctionTest.java)
657657
* [NthUglyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/NthUglyNumberTest.java)
658+
* [PalindromeNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java)
658659
* [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
659660
* [PerfectCubeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java)
660661
* [PerfectNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java)

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package com.thealgorithms.maths;
22

33
public class PalindromeNumber {
4-
5-
public static void main(String[] args) {
6-
assert isPalindrome(12321);
7-
assert !isPalindrome(1234);
8-
assert isPalindrome(1);
9-
}
10-
114
/**
125
* Check if {@code n} is palindrome number or not
136
*
@@ -17,7 +10,7 @@ public static void main(String[] args) {
1710
*/
1811
public static boolean isPalindrome(int number) {
1912
if (number < 0) {
20-
throw new IllegalArgumentException(number + "");
13+
throw new IllegalArgumentException("Input parameter must not be negative!");
2114
}
2215
int numberCopy = number;
2316
int reverseNumber = 0;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* @author Albina Gimaletdinova on 01/07/2023
8+
*/
9+
public class PalindromeNumberTest {
10+
@Test
11+
public void testNumbersArePalindromes() {
12+
Assertions.assertTrue(PalindromeNumber.isPalindrome(0));
13+
Assertions.assertTrue(PalindromeNumber.isPalindrome(1));
14+
Assertions.assertTrue(PalindromeNumber.isPalindrome(2332));
15+
Assertions.assertTrue(PalindromeNumber.isPalindrome(12321));
16+
}
17+
18+
@Test
19+
public void testNumbersAreNotPalindromes() {
20+
Assertions.assertFalse(PalindromeNumber.isPalindrome(12));
21+
Assertions.assertFalse(PalindromeNumber.isPalindrome(990));
22+
Assertions.assertFalse(PalindromeNumber.isPalindrome(1234));
23+
}
24+
25+
@Test
26+
public void testIfNegativeInputThenExceptionExpected() {
27+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> PalindromeNumber.isPalindrome(-1));
28+
Assertions.assertEquals(exception.getMessage(), "Input parameter must not be negative!");
29+
}
30+
}

0 commit comments

Comments
 (0)