-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathTestStringToInteger.java
104 lines (78 loc) · 3.07 KB
/
TestStringToInteger.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.rampatra.strings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class TestStringToInteger {
//It assumes the {@code string} contains ASCII characters only.
@Test
public static void AllTestCases() {
//Empty cases
String test1 = "";
int retVal = -1;
//In this case, we're expected to be thrown the numberFormatException
try {
retVal = StringToInteger.getIntegerFromString(test1);
}catch (NumberFormatException e) {
System.out.println("Exception case for \" Empty cases \" executed successfully.");
}finally {
System.out.println("End exception case. Return value: " + retVal);
}
//Positive number
String test2 = "2147483647";
retVal = Integer.MAX_VALUE;
assertEquals(StringToInteger.getIntegerFromString(test2), retVal);
//Negative number
String test3 = "-2147483648";
retVal = Integer.MIN_VALUE;
assertEquals(retVal, StringToInteger.getIntegerFromString(test3));
//Zero leading
String test4 = "00000000000000000000000123123123";
retVal = 123123123;
assertEquals(StringToInteger.getIntegerFromString(test4), retVal);
//Zero leading and minus sign
String test5 = "00000-213";
retVal = 0;
//In this case, we're expected to be thrown the numberFormatException
try {
retVal = StringToInteger.getIntegerFromString(test5);
}catch (NumberFormatException e) {
System.out.println("Exception case for \" Zero leading and minus sign \" executed successfully.");
}finally {
System.out.println("End exception case. Return value: " + retVal);
}
//Ascii string
String test6 = "QWERTYUIOPASDFGHJKLZXCVBNM 1234567890 qwertyuiopasdfghjklzxcvbnm -=+!@#$%^&*() >:{ }|\\\\]";
//In this case, we're expected to be thrown the numberFormatException
try {
retVal = StringToInteger.getIntegerFromString(test6);
}catch (NumberFormatException e) {
System.out.println("Exception case for \" Ascii string \" executed successfully.");
}finally {
System.out.println("End exception case. Return value: " + retVal);
}
//Trailing ASCII character, positive value
String test7 = "123AAABBB";
retVal = 123;
//In this case, we're expected to be thrown the numberFormatException
try {
retVal = StringToInteger.getIntegerFromString(test7);
}catch (NumberFormatException e) {
System.out.println("Exception case for \" Trailing ASCII character, positive value \" executed successfully.");
}finally {
System.out.println("End exception case. Return value: " + retVal);
}
//Trailing ASCII character, negative value
String test8 = "-123AAABBB";
retVal = 123;
//In this case, we're expected to be thrown the numberFormatException
try {
retVal = StringToInteger.getIntegerFromString(test8);
}catch (NumberFormatException e) {
System.out.println("Exception case for \" Trailing ASCII character, negative value \" executed successfully.");
}finally {
System.out.println("End exception case. Return value: " + retVal);
}
}
public static void main(String[] args) {
AllTestCases();
}
}