From 2456d86432613c84ec1a33c52b1349c904341520 Mon Sep 17 00:00:00 2001 From: Albina Gimaletdinova Date: Sun, 2 Jul 2023 11:31:55 +0300 Subject: [PATCH] Add unit tests for ParseInteger (#4228) --- DIRECTORY.md | 1 + .../com/thealgorithms/maths/ParseInteger.java | 14 ++---- .../thealgorithms/maths/ParseIntegerTest.java | 44 +++++++++++++++++++ 3 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/ParseIntegerTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index 926c4bd9d..5f02fbe42 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 d0cf89b94..a396a7b0d 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'; } 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 000000000..dc5bf37f0 --- /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")); + } +}