mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Add unit tests for ParseInteger (#4228)
This commit is contained in:

committed by
GitHub

parent
4b45ac7e71
commit
2456d86432
@ -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)
|
||||
|
@ -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';
|
||||
}
|
||||
|
44
src/test/java/com/thealgorithms/maths/ParseIntegerTest.java
Normal file
44
src/test/java/com/thealgorithms/maths/ParseIntegerTest.java
Normal file
@ -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"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user