Skip to content

Commit aded88f

Browse files
committed
Added test cases, and corrected the program to resolve the few test cases
that failed. Hence, test classes are important!
1 parent 99ecfa9 commit aded88f

2 files changed

Lines changed: 110 additions & 9 deletions

File tree

‎src/miscproblems/OneWayGridNavigator.java‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,27 @@ public class OneWayGridNavigator {
2020

2121
public static void main(String[] args) {
2222
//Hardcode length and height for now
23-
final int LENGTH = 5;
24-
final int HEIGHT = 6;
23+
final int LENGTH = 1;
24+
final int HEIGHT = 1;
2525

26-
List<Character> pathArray = new ArrayList<Character>();
26+
int numPaths = findPaths(LENGTH, HEIGHT);
27+
28+
System.out.println("Number of paths " + numPaths);
29+
}
2730

31+
static int findPaths(final int LENGTH, final int HEIGHT) {
32+
List<Character> pathArray = new ArrayList<Character>();
33+
2834
OneWayGridNavigator op = new OneWayGridNavigator();
2935

3036
String simplestPath = constructSimplestPath(LENGTH, HEIGHT);
3137

32-
Set<String> permutations = op.permute(simplestPath, 0, simplestPath.length() - 1);
33-
System.out.println(permutations.size());
38+
Set<String> permutations = op.permute(simplestPath);
39+
3440
for (String w : permutations) {
3541
System.out.println(w);
3642
}
43+
return permutations.size();
3744
}
3845

3946
/**
@@ -42,7 +49,8 @@ public static void main(String[] args) {
4249
* lt)
4350
*/
4451
private Set<String> permute(String word) {
45-
return permute(word, 0, word.length() - 1);
52+
Set<String> results = new HashSet<String>();
53+
return permute(word, 0, word.length() - 1, results);
4654
}
4755

4856
/**
@@ -52,14 +60,14 @@ private Set<String> permute(String word) {
5260
* Idea : Fix a letter, bring it to the front. Recursively permute the
5361
* remaining letters. Swap back the fixed letter. Repeat for all letters.
5462
*/
55-
private Set<String> permute(String word, int st, int lt) {
56-
Set<String> results = new HashSet<String>();
63+
private Set<String> permute(String word, int st, int lt, Set<String> results) {
64+
//Set<String> results = new HashSet<String>();
5765
if (st == lt) {
5866
results.add(word);
5967
}
6068
for (int i = st; i <= lt; i++) {
6169
word = swap(word, st, i);
62-
permute(word, st + 1, lt);
70+
permute(word, st + 1, lt, results);
6371
word = swap(word, st, i);
6472
}
6573
return results;
@@ -71,6 +79,9 @@ private Set<String> permute(String word, int st, int lt) {
7179
* Returns a String with the path.
7280
*/
7381
private static String constructSimplestPath(final int LENGTH, final int HEIGHT) {
82+
//Added to clear one side 0 lenght test case
83+
if (LENGTH * HEIGHT == 0) return "";
84+
7485
StringBuffer sb = new StringBuffer();
7586
for (int i = 0; i < LENGTH; i++) {
7687
sb.append("R");
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Test class for one way grid problem
3+
*/
4+
package miscproblems;
5+
6+
import org.junit.After;
7+
import org.junit.AfterClass;
8+
import org.junit.Before;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
*
15+
* @author Adarsh V
16+
*/
17+
public class OneWayGridNavigatorTest {
18+
19+
public OneWayGridNavigatorTest() {
20+
}
21+
22+
@BeforeClass
23+
public static void setUpClass() {
24+
25+
}
26+
27+
@AfterClass
28+
public static void tearDownClass() {
29+
}
30+
31+
@Before
32+
public void setUp() {
33+
}
34+
35+
@After
36+
public void tearDown() {
37+
}
38+
39+
/**
40+
* Test with 0 input, of class OneWayGridNavigator.
41+
*/
42+
@Test
43+
public void testWithBoth0() {
44+
int expected = 0;
45+
int length = 0;
46+
int height = 0;
47+
assertEquals(expected,OneWayGridNavigator.findPaths(length, height));
48+
}
49+
50+
/**
51+
* Test input with either length or heigh as 0
52+
*/
53+
@Test
54+
public void testWithOneSide0() {
55+
int expected = 0;
56+
int length = 0;
57+
int height = 1;
58+
assertEquals(expected,OneWayGridNavigator.findPaths(length, height));
59+
60+
length = 10;
61+
height = 0;
62+
assertEquals(expected,OneWayGridNavigator.findPaths(length, height));
63+
64+
}
65+
66+
/**
67+
* Test input with either length and height as 1
68+
*/
69+
@Test
70+
public void testWithLength1() {
71+
int expected = 2;
72+
int length = 1;
73+
int height = 1;
74+
assertEquals(expected,OneWayGridNavigator.findPaths(length, height));
75+
76+
}
77+
78+
/**
79+
* Test input with either length and height as 1
80+
*/
81+
@Test
82+
public void testWithReasonableLength() {
83+
int expected = 126;
84+
int length = 4;
85+
int height = 5;
86+
assertEquals(expected,OneWayGridNavigator.findPaths(length, height));
87+
88+
}
89+
90+
}

0 commit comments

Comments
 (0)