You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
14 KiB

6 years ago
  1. // Test driver for the RecursiveMethods class
  2. // Do not make any changes to this file!
  3. // Xiwei Wang
  4. import java.util.Arrays;
  5. public class TestRecursiveMethods
  6. {
  7. public static void main(String[] args)
  8. {
  9. RecursiveMethods myMethods = new RecursiveMethods();
  10. int numPassedTests = 0;
  11. int numTotalTests = 0;
  12. String testResult;
  13. // Test 1
  14. numTotalTests++;
  15. int iReturn = -1;
  16. testResult = "[Failed]";
  17. String eMsg = "N/A";
  18. try
  19. {
  20. int[] myArray = {10};
  21. iReturn = myMethods.sumSquares(myArray);
  22. if (iReturn == 100)
  23. {
  24. numPassedTests++;
  25. testResult = "[Passed]";
  26. }
  27. }
  28. catch (RuntimeException e)
  29. {
  30. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  31. }
  32. System.out.println("Test " + numTotalTests + ": sumSquares(10) ==> " + testResult + "\n Expected: 100" );
  33. if (eMsg.equals("N/A"))
  34. System.out.println(" Yours: " + iReturn + "\n");
  35. else
  36. System.out.println(" Yours: " + eMsg + "\n");
  37. // Test 2
  38. numTotalTests++;
  39. iReturn = -1;
  40. testResult = "[Failed]";
  41. eMsg = "N/A";
  42. try
  43. {
  44. int[] myArray = {10, 20, 30, 40, 50, 60};
  45. iReturn = myMethods.sumSquares(myArray);
  46. if (iReturn == 9100)
  47. {
  48. numPassedTests++;
  49. testResult = "[Passed]";
  50. }
  51. }
  52. catch (RuntimeException e)
  53. {
  54. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  55. }
  56. System.out.println("Test " + numTotalTests + ": sumSquares(10, 20, 30, 40, 50, 60) ==> " + testResult + "\n Expected: 9100" );
  57. if (eMsg.equals("N/A"))
  58. System.out.println(" Yours: " + iReturn + "\n");
  59. else
  60. System.out.println(" Yours: " + eMsg + "\n");
  61. // Test 3
  62. numTotalTests++;
  63. CharStack s = new CharStack(5);
  64. String sReturn = "";
  65. testResult = "[Failed]";
  66. eMsg = "N/A";
  67. try
  68. {
  69. s.push('a');
  70. myMethods.upperStackRec(s);
  71. sReturn = s.toString();
  72. if (sReturn.equals("A"))
  73. {
  74. numPassedTests++;
  75. testResult = "[Passed]";
  76. }
  77. }
  78. catch (RuntimeException e)
  79. {
  80. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  81. }
  82. System.out.println("Test " + numTotalTests + ": upperStackRec(\"a\" (from top to bottom)) ==> " + testResult + "\n Expected: \"A\" (from top to bottom)" );
  83. if (eMsg.equals("N/A"))
  84. System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
  85. else
  86. System.out.println(" Yours: " + eMsg + "\n");
  87. // Test 4
  88. numTotalTests++;
  89. s = new CharStack(5);
  90. sReturn = "";
  91. testResult = "[Failed]";
  92. eMsg = "N/A";
  93. try
  94. {
  95. s.push('7');
  96. s.push('?');
  97. s.push('p');
  98. myMethods.upperStackRec(s);
  99. sReturn = s.toString();
  100. if (sReturn.equals("P?7"))
  101. {
  102. numPassedTests++;
  103. testResult = "[Passed]";
  104. }
  105. }
  106. catch (RuntimeException e)
  107. {
  108. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  109. }
  110. System.out.println("Test " + numTotalTests + ": upperStackRec(\"p?7\" (from top to bottom)) ==> " + testResult + "\n Expected: \"P?7\" (from top to bottom)" );
  111. if (eMsg.equals("N/A"))
  112. System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
  113. else
  114. System.out.println(" Yours: " + eMsg + "\n");
  115. // Test 5
  116. numTotalTests++;
  117. s = new CharStack(5);
  118. sReturn = "";
  119. testResult = "[Failed]";
  120. eMsg = "N/A";
  121. try
  122. {
  123. s.push('4');
  124. s.push('K');
  125. s.push('3');
  126. s.push('s');
  127. s.push('c');
  128. myMethods.upperStackRec(s);
  129. sReturn = s.toString();
  130. if (sReturn.equals("CS3K4"))
  131. {
  132. numPassedTests++;
  133. testResult = "[Passed]";
  134. }
  135. }
  136. catch (RuntimeException e)
  137. {
  138. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  139. }
  140. System.out.println("Test " + numTotalTests + ": upperStackRec(\"cs3K4\" (from top to bottom)) ==> " + testResult + "\n Expected: \"CS3K4\" (from top to bottom)" );
  141. if (eMsg.equals("N/A"))
  142. System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
  143. else
  144. System.out.println(" Yours: " + eMsg + "\n");
  145. // Test 6
  146. numTotalTests++;
  147. s = new CharStack(9);
  148. sReturn = "";
  149. testResult = "[Failed]";
  150. eMsg = "N/A";
  151. try
  152. {
  153. s.push('Z');
  154. s.push('y');
  155. s.push('X');
  156. s.push('|');
  157. s.push('E');
  158. s.push('d');
  159. s.push('C');
  160. s.push('b');
  161. s.push('A');
  162. myMethods.upperStackRec(s);
  163. sReturn = s.toString();
  164. if (sReturn.equals("ABCDE|XYZ"))
  165. {
  166. numPassedTests++;
  167. testResult = "[Passed]";
  168. }
  169. }
  170. catch (RuntimeException e)
  171. {
  172. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  173. }
  174. System.out.println("Test " + numTotalTests + ": upperStackRec(\"AbCdE|XyZ\" (from top to bottom)) ==> " + testResult + "\n Expected: \"ABCDE|XYZ\" (from top to bottom)" );
  175. if (eMsg.equals("N/A"))
  176. System.out.println(" Yours: \"" + sReturn + "\" (from top to bottom)\n");
  177. else
  178. System.out.println(" Yours: " + eMsg + "\n");
  179. // Test 7
  180. numTotalTests++;
  181. sReturn = "";
  182. testResult = "[Failed]";
  183. eMsg = "N/A";
  184. try
  185. {
  186. String myString = "";
  187. sReturn = myMethods.reverseStringRec(myString);
  188. if (sReturn.equals(""))
  189. {
  190. numPassedTests++;
  191. testResult = "[Passed]";
  192. }
  193. }
  194. catch (RuntimeException e)
  195. {
  196. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  197. }
  198. System.out.println("Test " + numTotalTests + ": reverseStringRec(\"\") ==> " + testResult + "\n Expected: \"\"" );
  199. if (eMsg.equals("N/A"))
  200. System.out.println(" Yours: \"" + sReturn + "\"\n");
  201. else
  202. System.out.println(" Yours: " + eMsg + "\n");
  203. // Test 8
  204. numTotalTests++;
  205. sReturn = "";
  206. testResult = "[Failed]";
  207. eMsg = "N/A";
  208. try
  209. {
  210. String myString = "a";
  211. sReturn = myMethods.reverseStringRec(myString);
  212. if (sReturn.equals("a"))
  213. {
  214. numPassedTests++;
  215. testResult = "[Passed]";
  216. }
  217. }
  218. catch (RuntimeException e)
  219. {
  220. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  221. }
  222. System.out.println("Test " + numTotalTests + ": reverseStringRec(\"a\") ==> " + testResult + "\n Expected: \"a\"" );
  223. if (eMsg.equals("N/A"))
  224. System.out.println(" Yours: \"" + sReturn + "\"\n");
  225. else
  226. System.out.println(" Yours: " + eMsg + "\n");
  227. // Test 9
  228. numTotalTests++;
  229. sReturn = "";
  230. testResult = "[Failed]";
  231. eMsg = "N/A";
  232. try
  233. {
  234. String myString = "abc";
  235. sReturn = myMethods.reverseStringRec(myString);
  236. if (sReturn.equals("cba"))
  237. {
  238. numPassedTests++;
  239. testResult = "[Passed]";
  240. }
  241. }
  242. catch (RuntimeException e)
  243. {
  244. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  245. }
  246. System.out.println("Test " + numTotalTests + ": reverseStringRec(\"abc\") ==> " + testResult + "\n Expected: \"cba\"" );
  247. if (eMsg.equals("N/A"))
  248. System.out.println(" Yours: \"" + sReturn + "\"\n");
  249. else
  250. System.out.println(" Yours: " + eMsg + "\n");
  251. // Test 10
  252. numTotalTests++;
  253. sReturn = "";
  254. testResult = "[Failed]";
  255. eMsg = "N/A";
  256. try
  257. {
  258. String myString = "Hello, Data Structures!";
  259. sReturn = myMethods.reverseStringRec(myString);
  260. if (sReturn.equals("!serutcurtS ataD ,olleH"))
  261. {
  262. numPassedTests++;
  263. testResult = "[Passed]";
  264. }
  265. }
  266. catch (RuntimeException e)
  267. {
  268. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  269. }
  270. System.out.println("Test " + numTotalTests + ": reverseStringRec(\"Hello, Data Structures!\") ==> " + testResult + "\n Expected: \"!serutcurtS ataD ,olleH\"" );
  271. if (eMsg.equals("N/A"))
  272. System.out.println(" Yours: \"" + sReturn + "\"\n");
  273. else
  274. System.out.println(" Yours: " + eMsg + "\n");
  275. // Test 11
  276. numTotalTests++;
  277. sReturn = "";
  278. testResult = "[Failed]";
  279. eMsg = "N/A";
  280. try
  281. {
  282. LNode myNode = null;
  283. sReturn = traverseList(myMethods.reverseListRec(myNode));
  284. if (sReturn.equals("head->null"))
  285. {
  286. numPassedTests++;
  287. testResult = "[Passed]";
  288. }
  289. }
  290. catch (RuntimeException e)
  291. {
  292. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  293. }
  294. System.out.println("Test " + numTotalTests + ": reverseListRec(null) ==> " + testResult + "\n Expected: head->null" );
  295. if (eMsg.equals("N/A"))
  296. System.out.println(" Yours: " + sReturn + "\n");
  297. else
  298. System.out.println(" Yours: " + eMsg + "\n");
  299. // Test 12
  300. numTotalTests++;
  301. sReturn = "";
  302. testResult = "[Failed]";
  303. eMsg = "N/A";
  304. try
  305. {
  306. LNode myNode = new LNode(20);
  307. sReturn = traverseList(myMethods.reverseListRec(myNode));
  308. if (sReturn.equals("head->20->null"))
  309. {
  310. numPassedTests++;
  311. testResult = "[Passed]";
  312. }
  313. }
  314. catch (RuntimeException e)
  315. {
  316. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  317. }
  318. System.out.println("Test " + numTotalTests + ": reverseListRec(20) ==> " + testResult + "\n Expected: head->20->null" );
  319. if (eMsg.equals("N/A"))
  320. System.out.println(" Yours: " + sReturn + "\n");
  321. else
  322. System.out.println(" Yours: " + eMsg + "\n");
  323. // Test 13
  324. numTotalTests++;
  325. sReturn = "";
  326. testResult = "[Failed]";
  327. eMsg = "N/A";
  328. try
  329. {
  330. LNode myNode = new LNode(20);
  331. LNode myNode1 = new LNode(30);
  332. LNode myNode2 = new LNode(40);
  333. myNode.setLink(myNode1);
  334. myNode1.setLink(myNode2);
  335. sReturn = traverseList(myMethods.reverseListRec(myNode));
  336. if (sReturn.equals("head->40->30->20->null"))
  337. {
  338. numPassedTests++;
  339. testResult = "[Passed]";
  340. }
  341. }
  342. catch (RuntimeException e)
  343. {
  344. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  345. }
  346. System.out.println("Test " + numTotalTests + ": reverseListRec(20->30->40) ==> " + testResult + "\n Expected: head->40->30->20->null" );
  347. if (eMsg.equals("N/A"))
  348. System.out.println(" Yours: " + sReturn + "\n");
  349. else
  350. System.out.println(" Yours: " + eMsg + "\n");
  351. // Test 14
  352. numTotalTests++;
  353. sReturn = "";
  354. testResult = "[Failed]";
  355. eMsg = "N/A";
  356. try
  357. {
  358. LNode myNode = new LNode(90);
  359. LNode myNode1 = new LNode(80);
  360. LNode myNode2 = new LNode(70);
  361. LNode myNode3 = new LNode(60);
  362. LNode myNode4 = new LNode(50);
  363. LNode myNode5 = new LNode(40);
  364. myNode.setLink(myNode1);
  365. myNode1.setLink(myNode2);
  366. myNode2.setLink(myNode3);
  367. myNode3.setLink(myNode4);
  368. myNode4.setLink(myNode5);
  369. sReturn = traverseList(myMethods.reverseListRec(myNode));
  370. if (sReturn.equals("head->40->50->60->70->80->90->null"))
  371. {
  372. numPassedTests++;
  373. testResult = "[Passed]";
  374. }
  375. }
  376. catch (RuntimeException e)
  377. {
  378. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  379. }
  380. System.out.println("Test " + numTotalTests + ": reverseListRec(90->80->70->60->50->40) ==> " + testResult + "\n Expected: head->40->50->60->70->80->90->null" );
  381. if (eMsg.equals("N/A"))
  382. System.out.println(" Yours: " + sReturn + "\n");
  383. else
  384. System.out.println(" Yours: " + eMsg + "\n");
  385. System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests));
  386. }
  387. public static String traverseList(LNode head)
  388. {
  389. String listContent = "head->";
  390. LNode current = head;
  391. while (current != null)
  392. {
  393. listContent += current.getInfo() + "->";
  394. current = current.getLink();
  395. }
  396. listContent += "null";
  397. return listContent;
  398. }
  399. }