From 4b45ac7e710f49014dab5b70dfa51eddbf8e1b0b Mon Sep 17 00:00:00 2001 From: Albina Gimaletdinova Date: Sat, 1 Jul 2023 21:29:10 +0300 Subject: [PATCH] Add unit tests for PalindromeNumber (#4227) --- DIRECTORY.md | 1 + .../thealgorithms/maths/PalindromeNumber.java | 9 +----- .../maths/PalindromeNumberTest.java | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index 782bb74a..926c4bd9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -655,6 +655,7 @@ * [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) * [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) * [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) diff --git a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java index 56c34d3d..5dad99ef 100644 --- a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java +++ b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java @@ -1,13 +1,6 @@ package com.thealgorithms.maths; 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 * @@ -17,7 +10,7 @@ public class PalindromeNumber { */ public static boolean isPalindrome(int number) { if (number < 0) { - throw new IllegalArgumentException(number + ""); + throw new IllegalArgumentException("Input parameter must not be negative!"); } int numberCopy = number; int reverseNumber = 0; diff --git a/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java b/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java new file mode 100644 index 00000000..a70100c0 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java @@ -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!"); + } +}