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

Commit c99da10

Browse files
committed
add JUnit testing
1 parent 9d705a5 commit c99da10

File tree

8 files changed

+342
-36
lines changed

8 files changed

+342
-36
lines changed

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Algorithms-and-Data-Structures-in-Java</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.m2e.core.maven2Builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package InsertionSort;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
5+
import org.junit.Test;
6+
7+
public class InsertionSort2Test {
8+
@Test
9+
public void shouldDoNothingWithEmptyArray() {
10+
int[] values = {};
11+
12+
InsertionSort2.insertionSortPart2(values);
13+
}
14+
15+
@Test
16+
public void shouldDoNothingWithOneElementArray() {
17+
int[] values = {16};
18+
19+
InsertionSort2.insertionSortPart2(values);
20+
21+
assertArrayEquals(new int[] {16}, values);
22+
}
23+
24+
@Test
25+
public void shouldSortValues() {
26+
int[] values = { 11, -4, 3, 0, 1};
27+
int[] expectedOrder = { -4, 0, 1, 3, 11};
28+
29+
InsertionSort2.insertionSortPart2(values);
30+
31+
assertArrayEquals(expectedOrder, values);
32+
}
33+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package palimdromeIndex;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class PalindromeIndexTest {
10+
11+
private String input;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
16+
input = null;
17+
18+
}
19+
20+
@After
21+
public void tearDown() throws Exception {
22+
23+
}
24+
25+
@Test(expected = NullPointerException.class)
26+
public void nullStringTest() throws Exception {
27+
28+
PalindromeIndex.isPalindrome(null);
29+
30+
}
31+
32+
@Test
33+
public void emptyStringTest() throws Exception {
34+
35+
input = "";
36+
37+
assertTrue(PalindromeIndex.isPalindrome(input));
38+
39+
}
40+
41+
@Test
42+
public void singleCharTest() throws Exception {
43+
44+
input = "H";
45+
46+
assertTrue(PalindromeIndex.isPalindrome(input));
47+
48+
}
49+
50+
@Test
51+
public void alphaNumericPalindromeTest() throws Exception {
52+
53+
input = "1234321";
54+
55+
assertTrue(PalindromeIndex.isPalindrome(input));
56+
}
57+
58+
@Test
59+
public void validPalindromeTest() throws Exception {
60+
61+
input = "madam";
62+
63+
assertTrue(PalindromeIndex.isPalindrome(input));
64+
}
65+
66+
@Test
67+
public void invalidWordPalindromeTest() throws Exception {
68+
69+
input = "rotators";
70+
71+
assertFalse(PalindromeIndex.isPalindrome(input));
72+
}
73+
74+
@Test
75+
public void invalidPalindromeTest() throws Exception {
76+
77+
input = "I am a tester";
78+
79+
assertFalse(PalindromeIndex.isPalindrome(input));
80+
}
81+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package sentencePalindrome;
2+
3+
public class SentencePalindrome {
4+
static boolean isPalindrome(String str) {
5+
int l = 0;
6+
int h = str.length() - 1;
7+
8+
// Lowercase string
9+
str = str.toLowerCase();
10+
11+
// Compares character until they are equal
12+
while (l <= h) {
13+
14+
char getAtl = str.charAt(l);
15+
char getAth = str.charAt(h);
16+
17+
// If there is another symbol in left
18+
// of sentence
19+
if (!(getAtl >= 'a' && getAtl <= 'z'))
20+
l++;
21+
22+
// If there is another symbol in right
23+
// of sentence
24+
else if (!(getAth >= 'a' && getAth <= 'z'))
25+
h--;
26+
27+
// If characters are equal
28+
else if (getAtl == getAth) {
29+
l++;
30+
h--;
31+
}
32+
33+
else
34+
return false;
35+
}
36+
return true;
37+
}
38+
public static void main(String[] args) {
39+
40+
41+
}
42+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package sentencePalindrome;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class SentencePalindromeTest {
10+
11+
private String input;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
16+
input = null;
17+
18+
}
19+
20+
@After
21+
public void tearDown() throws Exception {
22+
23+
}
24+
25+
@Test(expected = NullPointerException.class)
26+
public void nullStringTest() throws Exception {
27+
28+
SentencePalindrome.isPalindrome(null);
29+
}
30+
31+
@Test
32+
public void emptyStringTest() throws Exception {
33+
34+
input = "";
35+
36+
assertTrue(SentencePalindrome.isPalindrome(input));
37+
38+
}
39+
40+
@Test
41+
public void multipleWhiteSpaceTest() throws Exception {
42+
43+
input = "A Santa at Nasa";
44+
45+
assertTrue(SentencePalindrome.isPalindrome(input));
46+
47+
}
48+
49+
@Test
50+
public void singleCharTest() throws Exception {
51+
52+
input = "H";
53+
54+
assertTrue(SentencePalindrome.isPalindrome(input));
55+
56+
}
57+
58+
@Test
59+
public void punctuationTest() throws Exception {
60+
61+
input = "Eva, can I see bees in a cave?";
62+
63+
assertTrue(SentencePalindrome.isPalindrome(input));
64+
65+
}
66+
67+
@Test
68+
public void unicodeTest() throws Exception {
69+
70+
input = "Step on no pet.";
71+
72+
assertFalse(SentencePalindrome.isPalindrome(input));
73+
74+
}
75+
76+
@Test
77+
public void alphaNumericPalindromeTest() throws Exception {
78+
79+
input = "Air 2 an a2ria";
80+
81+
assertTrue(SentencePalindrome.isPalindrome(input));
82+
}
83+
84+
@Test
85+
public void validPalindromeTest() throws Exception {
86+
87+
input = "No lemon no melon";
88+
89+
assertTrue(SentencePalindrome.isPalindrome(input));
90+
}
91+
92+
@Test
93+
public void invalidPalindromeTest() throws Exception {
94+
95+
input = "I am a tester";
96+
97+
assertFalse(SentencePalindrome.isPalindrome(input));
98+
}
99+
}

src/main/java/com/rampatra/sorting/BubbleSort.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@
88
*/
99
public class BubbleSort {
1010

11-
/**
12-
* In bubble sort, we start at the beginning of the array and swap
13-
* the first two elements if the first is greater than the second.
14-
* Then, we go to the next pair, and so on, continuously making sweeps
15-
* of the array until it is sorted. In doing so, the smaller items
16-
* slowly "bubble" up to the beginning of the list and in each inner
17-
* iteration the largest element is sorted. Ergo, the inner loop runs
18-
* until {@code length - i - 1} times. Time complexity: O(n^2). Space
19-
* complexity: O(1), in place. To learn more: {@see https://youtu.be/6Gv8vg0kcHc}
20-
*
21-
* @param ar to be sorted
22-
*/
23-
private static void bubbleSort(int[] ar) {
24-
for (int i = 0; i < ar.length - 1; i++) {
25-
for (int j = 0; j < ar.length - i - 1; j++) {
26-
if (ar[j] > ar[j + 1]) {
27-
swap(ar, j, j + 1);
28-
}
29-
}
30-
}
31-
}
11+
/**
12+
* In bubble sort, we start at the beginning of the array and swap the first two
13+
* elements if the first is greater than the second. Then, we go to the next
14+
* pair, and so on, continuously making sweeps of the array until it is sorted.
15+
* In doing so, the smaller items slowly "bubble" up to the beginning of the
16+
* list and in each inner iteration the largest element is sorted. Ergo, the
17+
* inner loop runs until {@code length - i - 1} times. Time complexity: O(n^2).
18+
* Space complexity: O(1), in place. To learn more:
19+
* {@see https://youtu.be/6Gv8vg0kcHc}
20+
*
21+
* @param ar to be sorted
22+
*/
23+
static void bubbleSort(int[] ar) {
24+
for (int i = 0; i < ar.length - 1; i++) {
25+
for (int j = 0; j < ar.length - i - 1; j++) {
26+
if (ar[j] > ar[j + 1]) {
27+
swap(ar, j, j + 1);
28+
}
29+
}
30+
}
31+
}
3232

33-
private static void swap(int[] ar, int i, int j) {
34-
int temp = ar[i];
35-
ar[i] = ar[j];
36-
ar[j] = temp;
37-
}
33+
private static void swap(int[] ar, int i, int j) {
34+
int temp = ar[i];
35+
ar[i] = ar[j];
36+
ar[j] = temp;
37+
}
3838

39-
public static void main(String[] args) {
40-
int[] ar = {2, 5, 1, 7, 8};
41-
System.out.println(Arrays.toString(ar));
42-
bubbleSort(ar);
43-
System.out.println(Arrays.toString(ar));
44-
ar = new int[]{7, 5, 1, 7, 8, 0, 23};
45-
System.out.println(Arrays.toString(ar));
46-
bubbleSort(ar);
47-
System.out.println(Arrays.toString(ar));
48-
}
39+
public static void main(String[] args) {
40+
int[] ar = { 2, 5, 1, 7, 8 };
41+
System.out.println(Arrays.toString(ar));
42+
bubbleSort(ar);
43+
System.out.println(Arrays.toString(ar));
44+
ar = new int[] { 7, 5, 1, 7, 8, 0, 23 };
45+
System.out.println(Arrays.toString(ar));
46+
bubbleSort(ar);
47+
System.out.println(Arrays.toString(ar));
48+
}
4949
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package BubbleSort;
2+
import static org.junit.Assert.assertArrayEquals;
3+
4+
import org.junit.Test;
5+
6+
public class BubbleSortTest {
7+
@Test
8+
public void shouldDoNothingWithEmptyArray() {
9+
int[] values = {};
10+
11+
BubbleSort.bubbleSort(values);
12+
}
13+
14+
@Test
15+
public void shouldDoNothingWithOneElementArray() {
16+
int[] values = {16};
17+
18+
BubbleSort.bubbleSort(values);
19+
20+
assertArrayEquals(new int[] {16}, values);
21+
}
22+
23+
@Test
24+
public void shouldSortValues() {
25+
int[] values = { 11, -4, 3, 0, 1};
26+
int[] expectedOrder = { -4, 0, 1, 3, 11};
27+
28+
BubbleSort.bubbleSort(values);
29+
30+
assertArrayEquals(expectedOrder, values);
31+
}
32+
}

0 commit comments

Comments
 (0)