Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b00116b

Browse files
committed
Added jUnit test and fixed 2 bugs
1 parent 957d03b commit b00116b

File tree

6 files changed

+317
-6
lines changed

6 files changed

+317
-6
lines changed

src/main/java/com/rampatra/strings/IntegerToString.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.rampatra.strings;
22

33
/**
4-
* @author rampatra
4+
* @author rampatra, magicExists
55
* @since 2019-04-01
66
*/
7+
8+
/*
9+
* Fix the negative case for this function. The problem is that when you modulo the number, it's a negative remainder, thus, making the assigning at char[i] wrong. For example, with number 2, your modulo operation gives -2, and when converted into the char, it's (-2 + '0') = (-2 + 48) = 46 (not 50)
10+
*
11+
* My solution is that I use absolute method before finding our remainder in order to find the positive value
12+
*/
13+
714
public class IntegerToString {
815

916
private static final int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999,
1017
999999999, Integer.MAX_VALUE};
1118

12-
private static String getStringFromInteger(int num) {
19+
public static String getStringFromInteger(int num) {
1320
boolean isNegative = num < 0;
1421
num = isNegative ? -num : num;
1522
int size = getStringSize(num);
@@ -18,7 +25,8 @@ private static String getStringFromInteger(int num) {
1825

1926
int rem;
2027
for (int i = size - 1; isNegative ? i > 0 : i >= 0; i--) {
21-
rem = num % 10;
28+
//Changed here
29+
rem = Math.abs(num % 10);
2230
num = num / 10;
2331
chars[i] = (char) (rem + '0');
2432
}
@@ -31,7 +39,7 @@ private static String getStringFromInteger(int num) {
3139
}
3240

3341
private static int getStringSize(int num) {
34-
if (num == Integer.MAX_VALUE) return 10;
42+
if (num == Integer.MAX_VALUE || num == Integer.MIN_VALUE) return 10; // Not recommend this magic number at all
3543

3644
for (int i = 0; ; i++) {
3745
if (num < sizeTable[i]) {

src/main/java/com/rampatra/strings/StringToInteger.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class StringToInteger {
1313
* @param str the input string, for example, 0, 123, +123, -123, etc.
1414
* @return the equivalent integer.
1515
*/
16-
private static int getIntegerFromString(String str) {
16+
public static int getIntegerFromString(String str) {
1717
int number = 0;
1818
int digit;
1919
char ch;
@@ -43,7 +43,15 @@ private static int getIntegerFromString(String str) {
4343

4444
number += digit * (Math.pow(10, weight++));
4545
}
46-
return isNegative ? -number : number;
46+
47+
//Make sure to check this case, when we give the number MIN_VALUE inside this function, first the number that converted will be positive, thus can only handle up to 2^31 - 1 value, so 1 value is offseted. So I check for the case if the value is negative, and the current value equals MAX_VALUE, to return the MIN_VALUE thoroughly
48+
if (number == Integer.MAX_VALUE && isNegative) {
49+
return -number - 1;
50+
}else {
51+
return isNegative ? -number : number;
52+
}
53+
54+
4755
}
4856

4957
public static void main(String[] args) {
@@ -61,4 +69,5 @@ public static void main(String[] args) {
6169
System.out.println(getIntegerFromString(" "));
6270
System.out.println(getIntegerFromString("123L"));
6371
}
72+
6473
}

src/main/java/com/rampatra/strings/SubStringCheck.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class SubStringCheck {
1919
* @return
2020
*/
2121
public static boolean isSubString(String s1, String s2) {
22+
if (s1 == null || s2 == null) {
23+
return false; //Since null doesn't present in any string, we can return false on such cases
24+
}
25+
2226
char[] c1 = s1.toCharArray(),
2327
c2 = s2.toCharArray();
2428
int l1 = c1.length,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.rampatra.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
8+
public class TestIntegerToString {
9+
10+
@Test
11+
static void integerCases(){
12+
//Positive, small number, expected: true
13+
int a1 = 123;
14+
String a2 = "123";
15+
16+
//Negative, small number, expected: true
17+
int b1 = -123;
18+
String b2 = "-123";
19+
20+
//0, expected: true
21+
int c1 = 0;
22+
String c2 = "0";
23+
24+
boolean resultA = a2.equals(IntegerToString.getStringFromInteger(a1));
25+
boolean resultB = b2.equals(IntegerToString.getStringFromInteger(b1));
26+
boolean resultC = c2.equals(IntegerToString.getStringFromInteger(c1));
27+
28+
assertTrue(resultA);
29+
assertTrue(resultB);
30+
assertTrue(resultB);
31+
}
32+
33+
@Test
34+
static void boundaryCases() {
35+
//INT_MAX
36+
int a1 = Integer.MAX_VALUE;
37+
String a2 = "2147483647";
38+
39+
//INT_MIN
40+
int b1 = Integer.MIN_VALUE;
41+
String b2 = "-2147483648";
42+
43+
//Trailing zero
44+
int c1 = 000000000000;
45+
String c2 = "0";
46+
47+
boolean resultA = a2.equals(IntegerToString.getStringFromInteger(a1));
48+
boolean resultB = b2.equals(IntegerToString.getStringFromInteger(b1));
49+
boolean resultC = c2.equals(IntegerToString.getStringFromInteger(c1));
50+
51+
assertTrue(resultA);
52+
assertTrue(resultB);
53+
assertTrue(resultC);
54+
}
55+
56+
public static void main(String args[]){
57+
integerCases();
58+
boundaryCases();
59+
}
60+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.rampatra.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class TestStringToInteger {
8+
//It assumes the {@code string} contains ASCII characters only.
9+
10+
@Test
11+
public static void AllTestCases() {
12+
//Empty cases
13+
String test1 = "";
14+
int retVal = -1;
15+
//In this case, we're expected to be thrown the numberFormatException
16+
try {
17+
retVal = StringToInteger.getIntegerFromString(test1);
18+
}catch (NumberFormatException e) {
19+
System.out.println("Exception case for \" Empty cases \" executed successfully.");
20+
}finally {
21+
System.out.println("End exception case. Return value: " + retVal);
22+
}
23+
24+
25+
//Positive number
26+
String test2 = "2147483647";
27+
retVal = Integer.MAX_VALUE;
28+
29+
assertEquals(StringToInteger.getIntegerFromString(test2), retVal);
30+
31+
//Negative number
32+
String test3 = "-2147483648";
33+
retVal = Integer.MIN_VALUE;
34+
35+
assertEquals(retVal, StringToInteger.getIntegerFromString(test3));
36+
37+
//Zero leading
38+
String test4 = "00000000000000000000000123123123";
39+
retVal = 123123123;
40+
41+
assertEquals(StringToInteger.getIntegerFromString(test4), retVal);
42+
43+
44+
//Zero leading and minus sign
45+
String test5 = "00000-213";
46+
retVal = 0;
47+
48+
//In this case, we're expected to be thrown the numberFormatException
49+
try {
50+
retVal = StringToInteger.getIntegerFromString(test5);
51+
}catch (NumberFormatException e) {
52+
System.out.println("Exception case for \" Zero leading and minus sign \" executed successfully.");
53+
}finally {
54+
System.out.println("End exception case. Return value: " + retVal);
55+
}
56+
57+
58+
//Ascii string
59+
String test6 = "QWERTYUIOPASDFGHJKLZXCVBNM 1234567890 qwertyuiopasdfghjklzxcvbnm -=+!@#$%^&*() >:{ }|\\\\]";
60+
61+
//In this case, we're expected to be thrown the numberFormatException
62+
try {
63+
retVal = StringToInteger.getIntegerFromString(test6);
64+
}catch (NumberFormatException e) {
65+
System.out.println("Exception case for \" Ascii string \" executed successfully.");
66+
}finally {
67+
System.out.println("End exception case. Return value: " + retVal);
68+
}
69+
70+
71+
//Trailing ASCII character, positive value
72+
String test7 = "123AAABBB";
73+
retVal = 123;
74+
75+
//In this case, we're expected to be thrown the numberFormatException
76+
try {
77+
retVal = StringToInteger.getIntegerFromString(test7);
78+
}catch (NumberFormatException e) {
79+
System.out.println("Exception case for \" Trailing ASCII character, positive value \" executed successfully.");
80+
}finally {
81+
System.out.println("End exception case. Return value: " + retVal);
82+
}
83+
84+
85+
//Trailing ASCII character, negative value
86+
String test8 = "-123AAABBB";
87+
retVal = 123;
88+
89+
//In this case, we're expected to be thrown the numberFormatException
90+
try {
91+
retVal = StringToInteger.getIntegerFromString(test8);
92+
}catch (NumberFormatException e) {
93+
System.out.println("Exception case for \" Trailing ASCII character, negative value \" executed successfully.");
94+
}finally {
95+
System.out.println("End exception case. Return value: " + retVal);
96+
}
97+
98+
99+
}
100+
101+
public static void main(String[] args) {
102+
AllTestCases();
103+
}
104+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.rampatra.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
9+
public class TestSubStringCheck {
10+
static String assumedFilledString = "QWERTYUIOPASDFGHJKLZXCVBNM 1234567890 qwertyuiopasdfghjklzxcvbnm -=+!@#$%^&*() >:{ }|\\]";
11+
static String assumedFilledSubString = "QWERTYUIOPASDFGHJKLZXCVBNM";
12+
static String emptyString = "";
13+
static String nullString = null;
14+
static String nonASCIIString = "Ă Â Ô Ơ Ư Ê ⏩ ☕ ☝ ♈ ぁ あ ぃ い ぅ う ぇ え ぉ お ؀ ؄ ç ù";
15+
static String nonASCIISubString = "ぁ あ ぃ い ぅ う ぇ え ぉ お ؀ ؄ ç ù";
16+
static String spaceString = " ";
17+
18+
19+
@Test
20+
static void happyCases() {
21+
//First slot is main string
22+
boolean resultA = SubStringCheck.isSubString(assumedFilledString, assumedFilledSubString);
23+
24+
//Second slot is main string
25+
boolean resultB = SubStringCheck.isSubString(assumedFilledSubString, assumedFilledString);
26+
27+
//Both slot is the same
28+
boolean resultC = SubStringCheck.isSubString(assumedFilledSubString, assumedFilledSubString);
29+
30+
31+
//Assertion
32+
assertTrue(resultA);
33+
assertFalse(resultB);
34+
assertTrue(resultC);
35+
}
36+
37+
@Test
38+
static void emptyCases() {
39+
40+
//First slot is empty
41+
boolean resultA = SubStringCheck.isSubString(emptyString, assumedFilledString);
42+
43+
//Second slot is empty
44+
boolean resultB = SubStringCheck.isSubString(assumedFilledString, emptyString);
45+
46+
//Both slot is empty
47+
boolean resultC = SubStringCheck.isSubString(emptyString, emptyString);
48+
49+
50+
//Assertion
51+
assertFalse(resultA);
52+
assertTrue(resultB);
53+
assertTrue(resultC);
54+
}
55+
56+
@Test
57+
static void nullCases() {
58+
//First slot is null
59+
boolean resultA = SubStringCheck.isSubString(nullString, assumedFilledString);
60+
61+
//Second slot is null
62+
boolean resultB = SubStringCheck.isSubString(assumedFilledString, nullString);
63+
64+
//Both slot is null
65+
boolean resultC = SubStringCheck.isSubString(nullString, nullString);
66+
67+
//First slot is null and second slot is empty
68+
boolean resultD = SubStringCheck.isSubString(nullString, emptyString);
69+
70+
//First slot is empty and second slot is null
71+
boolean resultE = SubStringCheck.isSubString(emptyString, nullString);
72+
73+
//Assertion
74+
assertFalse(resultA);
75+
assertFalse(resultB);
76+
assertFalse(resultC);
77+
assertFalse(resultD);
78+
assertFalse(resultE);
79+
}
80+
81+
@Test
82+
static void spaceCases() {
83+
//First slot is full of spaces
84+
boolean resultA = SubStringCheck.isSubString(spaceString, assumedFilledString);
85+
86+
87+
//Second slot is full of spaces
88+
boolean resultB = SubStringCheck.isSubString(assumedFilledString, spaceString);
89+
90+
91+
//Both slot is full of spaces
92+
boolean resultC = SubStringCheck.isSubString(spaceString, spaceString);
93+
94+
//Assertion
95+
assertFalse(resultA);
96+
assertFalse(resultB);
97+
assertTrue(resultC);
98+
}
99+
100+
@Test
101+
static void nonASCIICases() {
102+
//Good case
103+
boolean resultA = SubStringCheck.isSubString(nonASCIIString, nonASCIISubString);
104+
105+
106+
//Second slot is empty
107+
boolean resultB = SubStringCheck.isSubString(nonASCIIString, emptyString);
108+
109+
110+
//Second slot is null
111+
boolean resultC = SubStringCheck.isSubString(nonASCIIString, nullString);
112+
113+
//Assertion
114+
assertTrue(resultA);
115+
assertTrue(resultB);
116+
assertFalse(resultC);
117+
}
118+
119+
public static void main(String[] args) {
120+
happyCases();
121+
emptyCases();
122+
nullCases();
123+
spaceCases();
124+
nonASCIICases();
125+
}
126+
}

0 commit comments

Comments
 (0)