Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import org.junit.*;
  4. import static org.junit.Assert.*;
  5. import fi.helsinki.cs.tmc.edutestutils.Points;
  6.  
  7. @Points("10.7")
  8. public class LabyrinttiTest {
  9. private void testi(char[][] t, int a) {
  10. int n = t.length;
  11. int m = t[0].length;
  12. char[][] u = new char[n][m];
  13. for (int i = 0; i < n; i++) {
  14. u[i] = t[i].clone();
  15. }
  16. Labyrintti l = new Labyrintti();
  17. String s = l.etsi(t);
  18. if (a == 0 && s == null) return;
  19. if (a == 0) fail("Reittiä ei ole olemassa, mutta metodisi antaa reitin");
  20. if (s == null) fail("Reitti on olemassa, mutta metodisi ei anna reittiä");
  21. if (a != s.length()) fail("Metodisi antaa väärän pituisen reitin");
  22. int ay = 0, ax = 0;
  23. for (int i = 0; i < n; i++) {
  24. for (int j = 0; j < m; j++) {
  25. if (u[i][j] == 'x') {ay = i; ax = j;}
  26. }
  27. }
  28. String v = "Metodisi antaa virheellisen reitin";
  29. boolean[] kk = new boolean[4];
  30. for (int i = 0; i < a; i++) {
  31. char c = s.charAt(i);
  32. if (c == 'Y') {
  33. ay--;
  34. } else if (c == 'A') {
  35. ay++;
  36. } else if (c == 'V') {
  37. ax--;
  38. } else if (c == 'O') {
  39. ax++;
  40. } else {
  41. fail(v);
  42. }
  43. if (u[ay][ax] == '#') fail(v);
  44. if (u[ay][ax] >= 'a' && u[ay][ax] <= 'd') {
  45. kk[u[ay][ax]-'a'] = true;
  46. }
  47. if (u[ay][ax] >= 'A' && u[ay][ax] <= 'D') {
  48. if (!kk[u[ay][ax]-'A']) fail(v);
  49. }
  50. }
  51. if (u[ay][ax] != 'y') fail(v);
  52. }
  53.  
  54. @Test(timeout = 1000)
  55. public void testi1() {
  56. char[][] t = {{'#','#','#','#'},
  57. {'#','x','y','#'},
  58. {'#','#','#','#'}};
  59. testi(t,1);
  60. }
  61.  
  62. @Test(timeout = 1000)
  63. public void testi2() {
  64. char[][] t = {{'#','#','#','#','#'},
  65. {'#','x','#','y','#'},
  66. {'#','#','#','#','#'}};
  67. testi(t,0);
  68. }
  69.  
  70. @Test(timeout = 1000)
  71. public void testi3() {
  72. char[][] t = {{'#','#','#','#','#'},
  73. {'#','x','.','.','#'},
  74. {'#','.','.','.','#'},
  75. {'#','.','.','y','#'},
  76. {'#','#','#','#','#'}};
  77. testi(t,4);
  78. }
  79.  
  80. @Test(timeout = 1000)
  81. public void testi4() {
  82. char[][] t = {{'#','#','#','#','#'},
  83. {'#','x','#','y','#'},
  84. {'#','.','#','.','#'},
  85. {'#','.','.','.','#'},
  86. {'#','#','#','#','#'}};
  87. testi(t,6);
  88. }
  89.  
  90. @Test(timeout = 1000)
  91. public void testi5() {
  92. int n = 50;
  93. char[][] t = new char[n][n];
  94. for (int i = 0; i < n; i++) {
  95. for (int j = 0; j < n; j++) {
  96. t[i][j] = '.';
  97. if (i == 0 || i == n-1) t[i][j] = '#';
  98. if (j == 0 || j == n-1) t[i][j] = '#';
  99. }
  100. }
  101. t[1][1] = 'x';
  102. t[n-2][n-2] = 'y';
  103. testi(t,2*n-6);
  104. }
  105.  
  106. @Test(timeout = 1000)
  107. public void testi6() {
  108. int n = 50;
  109. char[][] t = new char[n][n];
  110. for (int i = 0; i < n; i++) {
  111. for (int j = 0; j < n; j++) {
  112. t[i][j] = '.';
  113. if (i == 0 || i == n-1) t[i][j] = '#';
  114. if (j == 0 || j == n-1) t[i][j] = '#';
  115. }
  116. }
  117. t[1][1] = 'x';
  118. t[n-2][n-2] = 'y';
  119. t[n-3][n-2] = '#';
  120. t[n-2][n-3] = '#';
  121. t[n-3][n-3] = '#';
  122. testi(t,0);
  123. }
  124.  
  125. @Test(timeout = 1000)
  126. public void testi7() {
  127. int n = 47;
  128. char[][] t = new char[n][n];
  129. for (int i = 0; i < n; i++) {
  130. for (int j = 0; j < n; j++) {
  131. t[i][j] = '.';
  132. if (i == 0 || i == n-1) t[i][j] = '#';
  133. if (j == 0 || j == n-1) t[i][j] = '#';
  134. if (i%4 == 2 && j < n-2) t[i][j] = '#';
  135. if (i%4 == 0 && j > 1) t[i][j] = '#';
  136. }
  137. }
  138. t[1][1] = 'x';
  139. t[n-2][n-2] = 'y';
  140. testi(t,(n+1)/2*(n-3));
  141. }
  142.  
  143. @Test(timeout = 1000)
  144. public void testi8() {
  145. int n = 50;
  146. char[][] t = new char[n][n];
  147. try {
  148. Scanner input = new Scanner(new File("laby.txt"));
  149. for (int i = 0; i < n; i++) {
  150. String s = input.next();
  151. for (int j = 0; j < n; j++) {
  152. t[i][j] = s.charAt(j);
  153. }
  154. }
  155. } catch (Exception e) {}
  156. testi(t,137);
  157. }
  158.  
  159. @Test(timeout = 1000)
  160. public void testi9() {
  161. char[][] t = {{'#','#','#','#','#','#'},
  162. {'#','x','a','A','y','#'},
  163. {'#','#','#','#','#','#'}};
  164. testi(t,3);
  165. }
  166.  
  167. @Test(timeout = 1000)
  168. public void testi10() {
  169. char[][] t = {{'#','#','#','#','#','#'},
  170. {'#','x','A','a','y','#'},
  171. {'#','#','#','#','#','#'}};
  172. testi(t,0);
  173. }
  174.  
  175. @Test(timeout = 1000)
  176. public void testi11() {
  177. char[][] t = {{'#','#','#','#','#','#'},
  178. {'#','x','b','A','y','#'},
  179. {'#','#','#','#','#','#'}};
  180. testi(t,0);
  181. }
  182.  
  183. @Test(timeout = 1000)
  184. public void testi12() {
  185. char[][] t = {{'#','#','#','#','#','#','#','#'},
  186. {'#','x','a','A','A','A','y','#'},
  187. {'#','#','#','#','#','#','#','#'}};
  188. testi(t,5);
  189. }
  190.  
  191. @Test(timeout = 1000)
  192. public void testi13() {
  193. char[][] t = {{'#','#','#','#','#','#','#','#'},
  194. {'#','x','a','A','b','B','y','#'},
  195. {'#','#','#','#','#','#','#','#'}};
  196. testi(t,5);
  197. }
  198.  
  199. @Test(timeout = 1000)
  200. public void testi14() {
  201. char[][] t = {{'#','#','#','#','#','#','#','#'},
  202. {'#','x','a','B','b','A','y','#'},
  203. {'#','#','#','#','#','#','#','#'}};
  204. testi(t,0);
  205. }
  206.  
  207. @Test(timeout = 1000)
  208. public void testi15() {
  209. int n = 50;
  210. char[][] t = new char[n][n];
  211. Random r = new Random();
  212. for (int i = 0; i < n; i++) {
  213. for (int j = 0; j < n; j++) {
  214. int c = r.nextInt(4);
  215. t[i][j] = i < n/2 ? (char)(c+'a') : (char)(c+'A');
  216. if (i == 0 || i == n-1) t[i][j] = '#';
  217. if (j == 0 || j == n-1) t[i][j] = '#';
  218. }
  219. }
  220. t[1][1] = 'x';
  221. t[n-2][n-2] = 'y';
  222. testi(t,2*n-6);
  223. }
  224.  
  225. @Test(timeout = 1000)
  226. public void testi16() {
  227. int n = 50;
  228. char[][] t = new char[n][n];
  229. try {
  230. Scanner input = new Scanner(new File("laby2.txt"));
  231. for (int i = 0; i < n; i++) {
  232. String s = input.next();
  233. for (int j = 0; j < n; j++) {
  234. t[i][j] = s.charAt(j);
  235. }
  236. }
  237. } catch (Exception e) {}
  238. testi(t,629);
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement