-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA5Tester.java
189 lines (156 loc) · 5.81 KB
/
A5Tester.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.io.File;
import java.io.FileNotFoundException;
public class A5Tester {
private static int testPassCount = 0;
private static int testCount = 0;
public static void main(String[] args) {
testStackOperations();
testStackIsGeneric();
testExceptions();
testMazeRunner(args);
System.out.println("Passed " + testPassCount + " / " + testCount + " tests");
}
public static void testStackOperations() {
System.out.println("Stack Operations Tests:");
A5Stack<Integer> testStack = new A5Stack<Integer>();
int result = 0;
displayResults(testStack.isEmpty(), "stack initially empty");
testStack.push(2);
result = testStack.top();
displayResults(!testStack.isEmpty(), "stack no longer empty");
displayResults(result==2, "top works after initial push");
//TODO: add more tests here
System.out.println();
}
public static void testStackIsGeneric() {
System.out.println("Stack Generics Tests:");
A5Stack<Integer> s1 = new A5Stack<Integer>();
A5Stack<String> s2 = new A5Stack<String>();
A5Stack<Double> s3 = new A5Stack<Double>();
int result1;
String result2;
double result3;
s1.push(3);
s1.push(8);
s2.push("CSC");
s2.push("ENGR");
s3.push(5.5);
s3.push(9.1);
result1 = s1.pop();
result2 = s2.pop();
result3 = s3.pop();
displayResults(result1==8, "Integer Stack");
displayResults(result2.equals("ENGR"), "String Stack");
displayResults(result3==9.1, "Double Stack");
result1 = s1.top();
result2 = s2.top();
result3 = s3.top();
displayResults(result1==3, "Integer Stack");
displayResults(result2.equals("CSC"), "String Stack");
displayResults(result3==5.5, "Double Stack");
//TODO: add more tests here
System.out.println();
}
public static void testExceptions() {
System.out.println("Stack Exception Tests:");
A5Stack<Integer> s1 = new A5Stack<Integer>();
try {
s1.pop();
displayResults(false, "popping from empty stack should throw exception");
} catch (EmptyStackException e) {
displayResults(true, "popping from empty stack should throw StackEmptyException");
} catch (Exception e) {
displayResults(false, "popping from empty stack should throw exception");
}
try {
s1.top();
displayResults(false, "trying to get top from empty stack should throw exception");
} catch (EmptyStackException e) {
displayResults(true, "trying to get top from empty stack should throw StackEmptyException");
} catch (Exception e) {
displayResults(false, "trying to get top from empty stack should throw exception");
}
//TODO: add more tests here
System.out.println();
}
public static void testMazeRunner(String[] args) {
if (args.length < 1) {
displayResults(false, "testing MazeRunner");
System.err.println("You must specify a maze file");
System.err.println("Usage: java A5Tester <mazefile>\n");
return;
}
System.out.println("testing MazeRunner with "+args[0]);
System.out.println("Results are in A5-output.txt");
Maze maze = initialize(args[0]);
MazeRunner mr = new MazeRunner(maze);
boolean solved = mr.solve(maze.getStart(), maze.getFinish());
mr.printResults(solved);
}
/*
* Purpose: Creates a maze by reading the contents of an input file
* Parameters: String - name of the input file containing maze data
* Returns: Maze - the maze created from input file data
*/
public static Maze initialize(String data) {
try {
Scanner infileScanner = new Scanner(new File(data));
int rows = infileScanner.nextInt();
int columns = infileScanner.nextInt();
int startRow = infileScanner.nextInt();
int startColumn = infileScanner.nextInt();
int finishRow = infileScanner.nextInt();
int finishColumn = infileScanner.nextInt();
infileScanner.nextLine();
char[][] mazeData = new char[rows][columns];
for (int row = 0; row < rows; row++) {
String line = infileScanner.nextLine();
for (int col = 0; col < columns; col++) {
mazeData[row][col] = line.charAt(col);
}
}
return new Maze(mazeData, new MazeLocation(startRow, startColumn), new MazeLocation(finishRow, finishColumn));
} catch (FileNotFoundException ex) {
System.out.println("Error scanning file "+data);
System.exit(2);
} catch(NoSuchElementException ex) {
System.out.println("Maze data file is not formatted correctly, should be:");
System.out.println("<num rows> <num columns>");
System.out.println("<start row> <start column>");
System.out.println("<finish row> <finish column>");
System.out.println("<Maze data>");
System.out.println("Example:");
System.out.println("7 8");
System.out.println("0 1");
System.out.println("6 6");
System.out.println("H HHHHHH");
System.out.println("H H H");
System.out.println("HHHH H H");
System.out.println("H H");
System.out.println("H HHHHHH");
System.out.println("H H");
System.out.println("HHHHHH H");
System.exit(3);
}
return null;
}
public static void displayResults (boolean passed, String testName) {
/* There is some magic going on here getting the line number
* Borrowed from:
* http://blog.taragana.com/index.php/archive/core-java-how-to-get-java-source-code-line-number-file-name-in-code/
*/
testCount++;
if (passed)
{
System.out.println ("Passed test: " + testName);
testPassCount++;
}
else
{
System.out.println ("Failed test: " + testName + " at line "
+ Thread.currentThread().getStackTrace()[2].getLineNumber());
}
}
}