-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapTest.java
More file actions
370 lines (339 loc) · 11.3 KB
/
MapTest.java
File metadata and controls
370 lines (339 loc) · 11.3 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.Test;
import java.util.StringTokenizer;
import java.util.Set;
import java.util.HashSet;
public class MapTest {
private static boolean isPathCorrect(String path, int startX, int startY, int destX, int destY) {
StringTokenizer token = new StringTokenizer(path);
boolean correct = true;
String point = null;
String x = "";
String y = "";
while (token.hasMoreTokens()) {
point = token.nextToken();
x = point.substring(point.indexOf('(') + 1 , point.indexOf(','));
y = point.substring(point.indexOf(',') + 1 , point.indexOf(')'));
if ((Integer.parseInt(x) - startX == 0 && Math.abs(Integer.parseInt(y) - startY) == 1) ||
(Math.abs(Integer.parseInt(x) - startX)== 1) && Integer.parseInt(y) - startY == 0 ) {
startX = Integer.parseInt(x);
startY = Integer.parseInt(y);
}
else {
correct = false;
break;
}
}
if (correct) {
if (startX != destX || startY != destY)
correct = false;
}
return correct;
}
@Test
public void testgoSoutWest1() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 4, 1, "");
assertTrue(isPathCorrect(actual, 5, 5, 4, 1));
}
@Test
public void testgoSoutWest2() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 5, 1, "");
assertTrue(isPathCorrect(actual, 5, 5, 5, 1));
}
@Test
public void testgoSoutWest3() {
Map city = new Map (10, 10);
String actual = city.getPath(5,1, 5, 0, "");
assertTrue(isPathCorrect(actual, 5, 1, 5, 0));
}
@Test
public void testgoSoutWest4() {
Map city = new Map (1, 1);
String actual = city.getPath(0,0, 0, 0, "");
assertTrue(isPathCorrect(actual, 0, 0, 0, 0));
}
@Test
public void testgoSoutWest5() {
Map city = new Map (5, 8);
String actual = city.getPath(3,2, 2, 1, "");
assertTrue(isPathCorrect(actual, 3, 2, 2, 1));
}
@Test
public void testgoSoutEast1() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 4, 8, "");
assertTrue(isPathCorrect(actual, 5, 5, 4, 8));
}
@Test
public void testgoSoutEast2() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 5, 9, "");
assertTrue(isPathCorrect(actual, 5, 5, 5, 9));
}
@Test
public void testgoSoutEast3() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 0, 9, "");
assertTrue(isPathCorrect(actual, 5, 5, 0, 9));
}
@Test
public void testgoSoutEast4() {
Map city = new Map (1, 1);
String actual = city.getPath(0,0, 0, 0, "");
assertTrue(isPathCorrect(actual, 0, 0, 0, 0));
}
@Test
public void testgoSoutEast5() {
Map city = new Map (15, 27);
String actual = city.getPath(10,15, 3, 25, "");
assertTrue(isPathCorrect(actual, 10,15, 3, 25));
}
@Test
public void testgoNorthWest1() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 8, 0, "");
assertTrue(isPathCorrect(actual, 5, 5, 8, 0));
}
@Test
public void testgoNorthWest2() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 5, 0, "");
assertTrue(isPathCorrect(actual, 5, 5, 5, 0));
}
@Test
public void testgoNorthWest3() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 8, 0, "");
assertTrue(isPathCorrect(actual, 5, 5, 8, 0));
}
@Test
public void testgoNorthWest4() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 5, 5, "");
assertTrue(isPathCorrect(actual, 5, 5, 5, 5));
}
@Test
public void testgoNorthWest5() {
Map city = new Map (23, 18);
String actual = city.getPath(11,13, 18, 5, "");
assertTrue(isPathCorrect(actual, 11,13, 18, 5));
}
@Test
public void testgoNorthEast1() {
Map city = new Map (100, 100);
String actual = city.getPath(50,50, 80, 90, "");
assertTrue(isPathCorrect(actual, 50, 50, 80, 90));
}
@Test
public void testgoNorthEast2() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 5, 9, "");
assertTrue(isPathCorrect(actual, 5, 5, 5, 9));
}
@Test
public void testgoNorthEast3() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 9, 5, "");
assertTrue(isPathCorrect(actual, 5, 5, 9, 5));
}
@Test
public void testgoNorthEast4() {
Map city = new Map (10, 10);
String actual = city.getPath(5,5, 5, 5, "");
assertTrue(isPathCorrect(actual, 5, 5, 5, 5));
}
@Test
public void testgoNorthEast5() {
Map city = new Map (50, 25);
String actual = city.getPath(15,24, 49, 24, "");
assertTrue(isPathCorrect(actual, 15,24, 49, 24));
}
@Test
public void testgetPathException1() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(-1, 5, 5, 6, ""));
}
@Test
public void testgetPathException2() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(1, -5, 5, 6, ""));
}
@Test
public void testgetPathException3() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(1, 5, -5, 6, ""));
}
@Test
public void testgetPathException4() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(1, 5, 5, -6, ""));
}
@Test
public void testgetPathException5() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(11, 5, 5, 6, ""));
}
@Test
public void testgetPathException6() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(1, 15, 5, 6, ""));
}
@Test
public void testgetPathException7() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(1, 5, 15, 6, ""));
}
@Test
public void testgetPathException8() {
Map city = new Map (10, 10);
assertThrows(IllegalArgumentException.class, ()->city.getPath(1, 5, 5, 16, ""));
}
private static boolean toBorder(String path, int row, int col) {
StringTokenizer token = new StringTokenizer(path);
boolean correct = false;
String point = null;
while (token.hasMoreTokens()) {
point = token.nextToken();
}
int x = Integer.parseInt(point.substring(point.indexOf('(')+ 1, point.indexOf(',')));
int y = Integer.parseInt(point.substring(point.indexOf(',')+ 1, point.indexOf(')')));
if ( x == 0 || x == row -1 || y == 0 || y == col - 1)
correct = true;
return correct;
}
private static String findBorder(String path) {
StringTokenizer token = new StringTokenizer(path);
String point = null;
while (token.hasMoreTokens()) {
point = token.nextToken();
}
return point;
}
private static boolean findDuplication(String path) {
StringTokenizer token = new StringTokenizer(path);
boolean correct = true;
Set<String> set = new HashSet<String>();
while (token.hasMoreTokens()) {
if (!set.add(token.nextToken())) {
correct = false;
break;
}
}
return correct;
}
@Test
public void testFindPath1() {
int startX = 5;
int startY = 5;
int n1 = 10;
int n2 = 10;
Map city = new Map (n1, n2);
String actualPath = city.findPath (startX, startX);
String point = findBorder(actualPath);
String actual = (actualPath.substring(actualPath.indexOf(')') + 1)).trim();
int destX = Integer.parseInt(point.substring(point.indexOf('(') + 1, point.indexOf(','))) ;
int destY = Integer.parseInt(point.substring(point.indexOf(',') + 1 , point.indexOf(')')));
boolean correct = toBorder(actualPath, n1, n2) && findDuplication(actualPath) &&
isPathCorrect(actual, startX, startY, destX, destY);
assertTrue(correct);
}
@Test
public void testFindPath2() {
int startX = 5;
int startY = 5;
int n1 = 30;
int n2 = 45;
Map city = new Map (n1, n2);
String actualPath = city.findPath (startX, startX);
String point = findBorder(actualPath);
String actual = (actualPath.substring(actualPath.indexOf(')') + 1)).trim();
int destX = Integer.parseInt(point.substring(point.indexOf('(') + 1, point.indexOf(','))) ;
int destY = Integer.parseInt(point.substring(point.indexOf(',') + 1 , point.indexOf(')')));
boolean correct = toBorder(actualPath, n1, n2) && findDuplication(actualPath) &&
isPathCorrect(actual, startX, startY, destX, destY);
assertTrue(correct);
}
@Test
public void testFindPath3() {
int startX = 29;
int startY = 5;
int n1 = 30;
int n2 = 45;
Map city = new Map (n1, n2);
String actualPath = city.findPath (startX, startY);
String point = findBorder(actualPath);
String actual = (actualPath.substring(actualPath.indexOf(')') + 1)).trim();
int destX = Integer.parseInt(point.substring(point.indexOf('(') + 1, point.indexOf(','))) ;
int destY = Integer.parseInt(point.substring(point.indexOf(',') + 1 , point.indexOf(')')));
boolean correct = toBorder(actualPath, n1, n2) && findDuplication(actualPath) &&
isPathCorrect(actual, startX, startY, destX, destY);
assertTrue(correct);
}
@Test
public void testFindPath4() {
int startX = 29;
int startY = 44;
int n1 = 30;
int n2 = 45;
Map city = new Map (n1, n2);
String actualPath = city.findPath (startX, startY);
String point = findBorder(actualPath);
String actual = (actualPath.substring(actualPath.indexOf(')') + 1)).trim();
int destX = Integer.parseInt(point.substring(point.indexOf('(') + 1, point.indexOf(','))) ;
int destY = Integer.parseInt(point.substring(point.indexOf(',') + 1 , point.indexOf(')')));
boolean correct = toBorder(actualPath, n1, n2) && findDuplication(actualPath) &&
isPathCorrect(actual, startX, startY, destX, destY);
assertTrue(correct);
}
@Test
public void testFindPath5() {
int startX = 0;
int startY = 0;
int n1 = 1;
int n2 = 1;
Map city = new Map (n1, n2);
String actualPath = city.findPath (startX, startY);
String point = findBorder(actualPath);
String actual = (actualPath.substring(actualPath.indexOf(')') + 1)).trim();
int destX = Integer.parseInt(point.substring(point.indexOf('(') + 1, point.indexOf(','))) ;
int destY = Integer.parseInt(point.substring(point.indexOf(',') + 1 , point.indexOf(')')));
boolean correct = toBorder(actualPath, n1, n2) && findDuplication(actualPath) &&
isPathCorrect(actual, startX, startY, destX, destY);
assertTrue(correct);
}
@Test
public void testFindPath6() {
int startX = 4;
int startY = 3;
int n1 = 13;
int n2 = 12;
Map city = new Map (n1, n2);
String actualPath = city.findPath (startX, startY);
String point = findBorder(actualPath);
String actual = (actualPath.substring(actualPath.indexOf(')') + 1)).trim();
int destX = Integer.parseInt(point.substring(point.indexOf('(') + 1, point.indexOf(','))) ;
int destY = Integer.parseInt(point.substring(point.indexOf(',') + 1 , point.indexOf(')')));
boolean correct = toBorder(actualPath, n1, n2) && findDuplication(actualPath) &&
isPathCorrect(actual, startX, startY, destX, destY);
assertTrue(correct);
}
@Test
public void testFindPath7() {
int startX = 1;
int startY = 1;
int n1 = 13;
int n2 = 12;
Map city = new Map (n1, n2);
String actualPath = city.findPath (startX, startY);
String point = findBorder(actualPath);
String actual = (actualPath.substring(actualPath.indexOf(')') + 1)).trim();
int destX = Integer.parseInt(point.substring(point.indexOf('(') + 1, point.indexOf(','))) ;
int destY = Integer.parseInt(point.substring(point.indexOf(',') + 1 , point.indexOf(')')));
boolean correct = toBorder(actualPath, n1, n2) && findDuplication(actualPath) &&
isPathCorrect(actual, startX, startY, destX, destY);
assertTrue(correct);
}
}