-
Notifications
You must be signed in to change notification settings - Fork 19.6k
/
Copy pathParseIntegerTest.java
51 lines (43 loc) · 2.42 KB
/
ParseIntegerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 EMPTY_PARAMETER_MESSAGE = "Input parameter must not be empty!";
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 testEmptyInput() {
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> ParseInteger.parseInt(""));
Assertions.assertEquals(exception.getMessage(), EMPTY_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"));
}
}