Add unit tests for PalindromeNumber (#4227)
This commit is contained in:
parent
8862a4dea5
commit
4b45ac7e71
@ -655,6 +655,7 @@
|
|||||||
* [MedianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MedianTest.java)
|
* [MedianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MedianTest.java)
|
||||||
* [MobiusFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MobiusFunctionTest.java)
|
* [MobiusFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MobiusFunctionTest.java)
|
||||||
* [NthUglyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/NthUglyNumberTest.java)
|
* [NthUglyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/NthUglyNumberTest.java)
|
||||||
|
* [PalindromeNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java)
|
||||||
* [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
|
* [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
|
||||||
* [PerfectCubeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java)
|
* [PerfectCubeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java)
|
||||||
* [PerfectNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java)
|
* [PerfectNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java)
|
||||||
|
@ -1,13 +1,6 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
public class PalindromeNumber {
|
public class PalindromeNumber {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
assert isPalindrome(12321);
|
|
||||||
assert !isPalindrome(1234);
|
|
||||||
assert isPalindrome(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if {@code n} is palindrome number or not
|
* Check if {@code n} is palindrome number or not
|
||||||
*
|
*
|
||||||
@ -17,7 +10,7 @@ public class PalindromeNumber {
|
|||||||
*/
|
*/
|
||||||
public static boolean isPalindrome(int number) {
|
public static boolean isPalindrome(int number) {
|
||||||
if (number < 0) {
|
if (number < 0) {
|
||||||
throw new IllegalArgumentException(number + "");
|
throw new IllegalArgumentException("Input parameter must not be negative!");
|
||||||
}
|
}
|
||||||
int numberCopy = number;
|
int numberCopy = number;
|
||||||
int reverseNumber = 0;
|
int reverseNumber = 0;
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Albina Gimaletdinova on 01/07/2023
|
||||||
|
*/
|
||||||
|
public class PalindromeNumberTest {
|
||||||
|
@Test
|
||||||
|
public void testNumbersArePalindromes() {
|
||||||
|
Assertions.assertTrue(PalindromeNumber.isPalindrome(0));
|
||||||
|
Assertions.assertTrue(PalindromeNumber.isPalindrome(1));
|
||||||
|
Assertions.assertTrue(PalindromeNumber.isPalindrome(2332));
|
||||||
|
Assertions.assertTrue(PalindromeNumber.isPalindrome(12321));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNumbersAreNotPalindromes() {
|
||||||
|
Assertions.assertFalse(PalindromeNumber.isPalindrome(12));
|
||||||
|
Assertions.assertFalse(PalindromeNumber.isPalindrome(990));
|
||||||
|
Assertions.assertFalse(PalindromeNumber.isPalindrome(1234));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIfNegativeInputThenExceptionExpected() {
|
||||||
|
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> PalindromeNumber.isPalindrome(-1));
|
||||||
|
Assertions.assertEquals(exception.getMessage(), "Input parameter must not be negative!");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user