Skip to content

Commit 2456d86

Browse files
authored
Add unit tests for ParseInteger (TheAlgorithms#4228)
1 parent 4b45ac7 commit 2456d86

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@
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)
658658
* [PalindromeNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java)
659+
* [ParseIntegerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ParseIntegerTest.java)
659660
* [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
660661
* [PerfectCubeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java)
661662
* [PerfectNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java)

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
package com.thealgorithms.maths;
22

33
public class ParseInteger {
4-
5-
public static void main(String[] args) {
6-
assert parseInt("123") == Integer.parseInt("123");
7-
assert parseInt("-123") == Integer.parseInt("-123");
8-
assert parseInt("0123") == Integer.parseInt("0123");
9-
assert parseInt("+123") == Integer.parseInt("+123");
10-
}
11-
124
/**
135
* Parse a string to integer
146
*
157
* @param s the string
168
* @return the integer value represented by the argument in decimal.
179
* @throws NumberFormatException if the {@code string} does not contain a
18-
* parsable integer.
10+
* parsable integer.
1911
*/
2012
public static int parseInt(String s) {
2113
if (s == null || s.length() == 0) {
22-
throw new NumberFormatException("null");
14+
throw new NumberFormatException("Input parameter must not be null!");
2315
}
2416
boolean isNegative = s.charAt(0) == '-';
2517
boolean isPositive = s.charAt(0) == '+';
2618
int number = 0;
2719
for (int i = isNegative ? 1 : isPositive ? 1 : 0, length = s.length(); i < length; ++i) {
2820
if (!Character.isDigit(s.charAt(i))) {
29-
throw new NumberFormatException("s=" + s);
21+
throw new NumberFormatException("Input parameter of incorrect format: " + s);
3022
}
3123
number = number * 10 + s.charAt(i) - '0';
3224
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 ParseIntegerTest {
10+
private static final String NULL_PARAMETER_MESSAGE = "Input parameter must not be null!";
11+
private static final String INCORRECT_FORMAT_MESSAGE = "Input parameter of incorrect format";
12+
13+
@Test
14+
public void testNullInput() {
15+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> ParseInteger.parseInt(null));
16+
Assertions.assertEquals(exception.getMessage(), NULL_PARAMETER_MESSAGE);
17+
}
18+
19+
@Test
20+
public void testInputOfIncorrectFormat() {
21+
IllegalArgumentException exception = Assertions.assertThrows(NumberFormatException.class, () -> ParseInteger.parseInt("+0a123"));
22+
Assertions.assertTrue(exception.getMessage().contains(INCORRECT_FORMAT_MESSAGE));
23+
24+
exception = Assertions.assertThrows(NumberFormatException.class, () -> ParseInteger.parseInt("b"));
25+
Assertions.assertTrue(exception.getMessage().contains(INCORRECT_FORMAT_MESSAGE));
26+
}
27+
28+
@Test
29+
public void testPositiveValueIsSuccessfullyConverted() {
30+
Assertions.assertEquals(ParseInteger.parseInt("0"), Integer.parseInt("0"));
31+
Assertions.assertEquals(ParseInteger.parseInt("123"), Integer.parseInt("123"));
32+
Assertions.assertEquals(ParseInteger.parseInt("0123"), Integer.parseInt("0123"));
33+
Assertions.assertEquals(ParseInteger.parseInt("+0123"), Integer.parseInt("+0123"));
34+
Assertions.assertEquals(ParseInteger.parseInt("+123"), Integer.parseInt("+123"));
35+
}
36+
37+
@Test
38+
public void testNegativeValueIsSuccessfullyConverted() {
39+
Assertions.assertEquals(ParseInteger.parseInt("-1"), Integer.parseInt("-1"));
40+
Assertions.assertEquals(ParseInteger.parseInt("-123"), Integer.parseInt("-123"));
41+
Assertions.assertEquals(ParseInteger.parseInt("-0123"), Integer.parseInt("-0123"));
42+
Assertions.assertEquals(ParseInteger.parseInt("-00123"), Integer.parseInt("-00123"));
43+
}
44+
}

0 commit comments

Comments
 (0)