Repository: TheAlgorithms/Java
Pull request #4228 (2023-07-01 19:27:05)

Added unit tests for ParseInteger class
Changes:
- added unit tests
- minor refactoring

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 926c4bd9..5f02fbe4 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -656,6 +656,7 @@
             * [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)
+            * [ParseIntegerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ParseIntegerTest.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/ParseInteger.java b/src/main/java/com/thealgorithms/maths/ParseInteger.java
index d0cf89b9..a396a7b0 100644
--- a/src/main/java/com/thealgorithms/maths/ParseInteger.java
+++ b/src/main/java/com/thealgorithms/maths/ParseInteger.java
@@ -1,32 +1,24 @@
 package com.thealgorithms.maths;
 
 public class ParseInteger {
-
-    public static void main(String[] args) {
-        assert parseInt("123") == Integer.parseInt("123");
-        assert parseInt("-123") == Integer.parseInt("-123");
-        assert parseInt("0123") == Integer.parseInt("0123");
-        assert parseInt("+123") == Integer.parseInt("+123");
-    }
-
     /**
      * Parse a string to integer
      *
      * @param s the string
      * @return the integer value represented by the argument in decimal.
      * @throws NumberFormatException if the {@code string} does not contain a
-     * parsable integer.
+     *                               parsable integer.
      */
     public static int parseInt(String s) {
         if (s == null || s.length() == 0) {
-            throw new NumberFormatException("null");
+            throw new NumberFormatException("Input parameter must not be null!");
         }
         boolean isNegative = s.charAt(0) == '-';
         boolean isPositive = s.charAt(0) == '+';
         int number = 0;
         for (int i = isNegative ? 1 : isPositive ? 1 : 0, length = s.length(); i < length; ++i) {
             if (!Character.isDigit(s.charAt(i))) {
-                throw new NumberFormatException("s=" + s);
+                throw new NumberFormatException("Input parameter of incorrect format: " + s);
             }
             number = number * 10 + s.charAt(i) - '0';
         }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/ParseIntegerTest.java b/src/test/java/com/thealgorithms/maths/ParseIntegerTest.java
new file mode 100644
index 00000000..dc5bf37f
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/ParseIntegerTest.java
@@ -0,0 +1,44 @@
+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 ParseIntegerTest {
+    private static final String NULL_PARAMETER_MESSAGE = "Input parameter must not be null!";
+    private static final String INCORRECT_FORMAT_MESSAGE = "Input parameter of incorrect format";
+
+    @Test
+    public void testNullInput() {
+        IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> ParseInteger.parseInt(null));
+        Assertions.assertEquals(exception.getMessage(), NULL_PARAMETER_MESSAGE);
+    }
+
+    @Test
+    public void testInputOfIncorrectFormat() {
+        IllegalArgumentException exception = Assertions.assertThrows(NumberFormatException.class, () -> ParseInteger.parseInt("+0a123"));
+        Assertions.assertTrue(exception.getMessage().contains(INCORRECT_FORMAT_MESSAGE));
+
+        exception = Assertions.assertThrows(NumberFormatException.class, () -> ParseInteger.parseInt("b"));
+        Assertions.assertTrue(exception.getMessage().contains(INCORRECT_FORMAT_MESSAGE));
+    }
+
+    @Test
+    public void testPositiveValueIsSuccessfullyConverted() {
+        Assertions.assertEquals(ParseInteger.parseInt("0"), Integer.parseInt("0"));
+        Assertions.assertEquals(ParseInteger.parseInt("123"), Integer.parseInt("123"));
+        Assertions.assertEquals(ParseInteger.parseInt("0123"), Integer.parseInt("0123"));
+        Assertions.assertEquals(ParseInteger.parseInt("+0123"), Integer.parseInt("+0123"));
+        Assertions.assertEquals(ParseInteger.parseInt("+123"), Integer.parseInt("+123"));
+    }
+
+    @Test
+    public void testNegativeValueIsSuccessfullyConverted() {
+        Assertions.assertEquals(ParseInteger.parseInt("-1"), Integer.parseInt("-1"));
+        Assertions.assertEquals(ParseInteger.parseInt("-123"), Integer.parseInt("-123"));
+        Assertions.assertEquals(ParseInteger.parseInt("-0123"), Integer.parseInt("-0123"));
+        Assertions.assertEquals(ParseInteger.parseInt("-00123"), Integer.parseInt("-00123"));
+    }
+}