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.

712 lines
21 KiB

6 years ago
  1. // Test driver for the LinkedNumberStack and StackApps classes
  2. // Do not make any changes to this file!
  3. // Xiwei Wang
  4. import java.util.*;
  5. import java.io.*;
  6. public class TestStack
  7. {
  8. public static void main(String[] args)
  9. {
  10. System.out.println("================ Problem 1 ================");
  11. TestP1();
  12. System.out.println("================ End of Problem 1 ================\n\n");
  13. System.out.print("Press any key to test Problem 2...");
  14. try
  15. {
  16. System.in.read();
  17. }
  18. catch (Exception e)
  19. {
  20. e.printStackTrace();
  21. }
  22. System.out.println("================ Problem 2 ================");
  23. TestP2();
  24. System.out.println("================ End of Problem 2 ================");
  25. }
  26. public static void TestP1()
  27. {
  28. NumberStack myStack = new LinkedNumberStack();
  29. int numPassedTests = 0;
  30. int numTotalTests = 0;
  31. String testResult;
  32. // Test 1
  33. numTotalTests++;
  34. int iReturn = -1;
  35. testResult = "[Failed]";
  36. String eMsg = "N/A";
  37. try
  38. {
  39. iReturn = myStack.size();
  40. if (iReturn == 0)
  41. {
  42. numPassedTests++;
  43. testResult = "[Passed]";
  44. }
  45. }
  46. catch (RuntimeException e)
  47. {
  48. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  49. }
  50. System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 0" );
  51. if (eMsg.equals("N/A"))
  52. System.out.println(" Yours: " + iReturn + "\n");
  53. else
  54. System.out.println(" Yours: " + eMsg + "\n");
  55. // Test 2
  56. numTotalTests++;
  57. testResult = "[Failed]";
  58. eMsg = "N/A";
  59. try
  60. {
  61. iReturn = myStack.pop();
  62. }
  63. catch (RuntimeException e)
  64. {
  65. if (!(e instanceof NullPointerException))
  66. {
  67. numPassedTests++;
  68. testResult = "[Passed]";
  69. }
  70. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  71. }
  72. System.out.println("Test " + numTotalTests + ": pop() ==> " + testResult + "\n Expected: a RuntimeException");
  73. System.out.println(" Yours: " + eMsg + "\n");
  74. // Test 3
  75. numTotalTests++;
  76. boolean bReturn = false;
  77. testResult = "[Failed]";
  78. eMsg = "N/A";
  79. try
  80. {
  81. myStack.push(10);
  82. bReturn = myStack.isEmpty();
  83. if (bReturn == false)
  84. {
  85. numPassedTests++;
  86. testResult = "[Passed]";
  87. }
  88. }
  89. catch (RuntimeException e)
  90. {
  91. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  92. }
  93. System.out.println("Test " + numTotalTests + ": push(10) and then isEmpty() ==> " + testResult + "\n Expected: false");
  94. if (eMsg.equals("N/A"))
  95. System.out.println(" Yours: " + bReturn + "\n");
  96. else
  97. System.out.println(" Yours: " + eMsg + "\n");
  98. // Test 4
  99. numTotalTests++;
  100. String sReturn = myStack.toString();
  101. if (sReturn.equals("10 "))
  102. {
  103. numPassedTests++;
  104. testResult = "[Passed]";
  105. }
  106. else
  107. testResult = "[Failed]";
  108. System.out.println("Test " + numTotalTests + ": toString() ==> " + testResult + "\n Expected (from top to bottom): 10 ");
  109. System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
  110. // Test 5
  111. numTotalTests++;
  112. iReturn = -1;
  113. testResult = "[Failed]";
  114. eMsg = "N/A";
  115. try
  116. {
  117. iReturn = myStack.top();
  118. if (iReturn == 10)
  119. {
  120. numPassedTests++;
  121. testResult = "[Passed]";
  122. }
  123. }
  124. catch (RuntimeException e)
  125. {
  126. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  127. }
  128. System.out.println("Test " + numTotalTests + ": top() ==> " + testResult + "\n Expected: 10");
  129. if (eMsg.equals("N/A"))
  130. System.out.println(" Yours: " + iReturn + "\n");
  131. else
  132. System.out.println(" Yours: " + eMsg + "\n");
  133. // Test 6
  134. numTotalTests++;
  135. sReturn = "";
  136. testResult = "[Failed]";
  137. eMsg = "N/A";
  138. try
  139. {
  140. myStack.push(20);
  141. sReturn = myStack.toString();
  142. if (sReturn.equals("20 10 "))
  143. {
  144. numPassedTests++;
  145. testResult = "[Passed]";
  146. }
  147. }
  148. catch (RuntimeException e)
  149. {
  150. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  151. }
  152. System.out.println("Test " + numTotalTests + ": push(20) and then toString() ==> " + testResult + "\n Expected (from top to bottom): 20 10 ");
  153. if (eMsg.equals("N/A"))
  154. System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
  155. else
  156. System.out.println(" Yours: " + eMsg + "\n");
  157. // Test 7
  158. numTotalTests++;
  159. iReturn = -1;
  160. testResult = "[Failed]";
  161. eMsg = "N/A";
  162. try
  163. {
  164. iReturn = myStack.top();
  165. if (iReturn == 20)
  166. {
  167. numPassedTests++;
  168. testResult = "[Passed]";
  169. }
  170. }
  171. catch (RuntimeException e)
  172. {
  173. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  174. }
  175. System.out.println("Test " + numTotalTests + ": top() ==> " + testResult + "\n Expected: 20");
  176. if (eMsg.equals("N/A"))
  177. System.out.println(" Yours: " + iReturn + "\n");
  178. else
  179. System.out.println(" Yours: " + eMsg + "\n");
  180. // Test 8
  181. numTotalTests++;
  182. iReturn = -1;
  183. testResult = "[Failed]";
  184. eMsg = "N/A";
  185. try
  186. {
  187. iReturn = myStack.pop();
  188. if (iReturn == 20)
  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 + ": pop() ==> " + testResult + "\n Expected: 20");
  199. if (eMsg.equals("N/A"))
  200. System.out.println(" Yours: " + iReturn + "\n");
  201. else
  202. System.out.println(" Yours: " + eMsg + "\n");
  203. // Test 9
  204. numTotalTests++;
  205. sReturn = myStack.toString();
  206. if (sReturn.equals("10 "))
  207. {
  208. numPassedTests++;
  209. testResult = "[Passed]";
  210. }
  211. else
  212. testResult = "[Failed]";
  213. System.out.println("Test " + numTotalTests + ": toString() ==> " + testResult + "\n Expected (from top to bottom): 10 ");
  214. System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
  215. // Test 10
  216. numTotalTests++;
  217. sReturn = "";
  218. testResult = "[Failed]";
  219. eMsg = "N/A";
  220. try
  221. {
  222. myStack.push(30);
  223. myStack.push(40);
  224. myStack.push(50);
  225. sReturn = myStack.toString();
  226. if (sReturn.equals("50 40 30 10 "))
  227. {
  228. numPassedTests++;
  229. testResult = "[Passed]";
  230. }
  231. }
  232. catch (RuntimeException e)
  233. {
  234. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  235. }
  236. System.out.println("Test " + numTotalTests + ": push(30), push(40), push(50), and then toString() ==> " + testResult + "\n Expected (from top to bottom): 50 40 30 10 ");
  237. if (eMsg.equals("N/A"))
  238. System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
  239. else
  240. System.out.println(" Yours: " + eMsg + "\n");
  241. // Test 11
  242. numTotalTests++;
  243. iReturn = -1;
  244. testResult = "[Failed]";
  245. eMsg = "N/A";
  246. try
  247. {
  248. iReturn = myStack.size();
  249. if (iReturn == 4)
  250. {
  251. numPassedTests++;
  252. testResult = "[Passed]";
  253. }
  254. }
  255. catch (RuntimeException e)
  256. {
  257. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  258. }
  259. System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 4" );
  260. if (eMsg.equals("N/A"))
  261. System.out.println(" Yours: " + iReturn + "\n");
  262. else
  263. System.out.println(" Yours: " + eMsg + "\n");
  264. // Test 12
  265. numTotalTests++;
  266. iReturn = -1;
  267. testResult = "[Failed]";
  268. eMsg = "N/A";
  269. try
  270. {
  271. iReturn = myStack.pop();
  272. iReturn = myStack.top();
  273. if (iReturn == 40)
  274. {
  275. numPassedTests++;
  276. testResult = "[Passed]";
  277. }
  278. }
  279. catch (RuntimeException e)
  280. {
  281. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  282. }
  283. System.out.println("Test " + numTotalTests + ": pop() and then top() ==> " + testResult + "\n Expected: 40");
  284. if (eMsg.equals("N/A"))
  285. System.out.println(" Yours: " + iReturn + "\n");
  286. else
  287. System.out.println(" Yours: " + eMsg + "\n");
  288. // Test 13
  289. numTotalTests++;
  290. iReturn = -1;
  291. testResult = "[Failed]";
  292. eMsg = "N/A";
  293. try
  294. {
  295. iReturn = myStack.pop();
  296. iReturn = myStack.size();
  297. if (iReturn == 2)
  298. {
  299. numPassedTests++;
  300. testResult = "[Passed]";
  301. }
  302. }
  303. catch (RuntimeException e)
  304. {
  305. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  306. }
  307. System.out.println("Test " + numTotalTests + ": pop() and then size() ==> " + testResult + "\n Expected: 2");
  308. if (eMsg.equals("N/A"))
  309. System.out.println(" Yours: " + iReturn + "\n");
  310. else
  311. System.out.println(" Yours: " + eMsg + "\n");
  312. // Test 14
  313. numTotalTests++;
  314. sReturn = "";
  315. testResult = "[Failed]";
  316. eMsg = "N/A";
  317. try
  318. {
  319. myStack.push(20);
  320. sReturn = myStack.toString();
  321. if (sReturn.equals("20 30 10 "))
  322. {
  323. numPassedTests++;
  324. testResult = "[Passed]";
  325. }
  326. }
  327. catch (RuntimeException e)
  328. {
  329. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  330. }
  331. System.out.println("Test " + numTotalTests + ": push(20) and then toString() ==> " + testResult + "\n Expected (from top to bottom): 20 30 10 ");
  332. if (eMsg.equals("N/A"))
  333. System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
  334. else
  335. System.out.println(" Yours: " + eMsg + "\n");
  336. // Test 15
  337. numTotalTests++;
  338. iReturn = -1;
  339. testResult = "[Failed]";
  340. eMsg = "N/A";
  341. try
  342. {
  343. iReturn = myStack.size();
  344. if (iReturn == 3)
  345. {
  346. numPassedTests++;
  347. testResult = "[Passed]";
  348. }
  349. }
  350. catch (RuntimeException e)
  351. {
  352. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  353. }
  354. System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 3" );
  355. if (eMsg.equals("N/A"))
  356. System.out.println(" Yours: " + iReturn + "\n");
  357. else
  358. System.out.println(" Yours: " + eMsg + "\n");
  359. // Test 16
  360. numTotalTests++;
  361. iReturn = -1;
  362. testResult = "[Failed]";
  363. eMsg = "N/A";
  364. try
  365. {
  366. iReturn = myStack.pop();
  367. iReturn = myStack.pop();
  368. iReturn = myStack.top();
  369. if (iReturn == 10)
  370. {
  371. numPassedTests++;
  372. testResult = "[Passed]";
  373. }
  374. }
  375. catch (RuntimeException e)
  376. {
  377. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  378. }
  379. System.out.println("Test " + numTotalTests + ": pop() twice and then top() ==> " + testResult + "\n Expected: 10");
  380. if (eMsg.equals("N/A"))
  381. System.out.println(" Yours: " + iReturn + "\n");
  382. else
  383. System.out.println(" Yours: " + eMsg + "\n");
  384. // Test 17
  385. numTotalTests++;
  386. sReturn = "";
  387. testResult = "[Failed]";
  388. eMsg = "N/A";
  389. try
  390. {
  391. sReturn = myStack.toString();
  392. if (sReturn.equals("10 "))
  393. {
  394. numPassedTests++;
  395. testResult = "[Passed]";
  396. }
  397. }
  398. catch (RuntimeException e)
  399. {
  400. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  401. }
  402. System.out.println("Test " + numTotalTests + ": toString() ==> " + testResult + "\n Expected (from top to bottom): 10 ");
  403. if (eMsg.equals("N/A"))
  404. System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
  405. else
  406. System.out.println(" Yours: " + eMsg + "\n");
  407. // Test 18
  408. numTotalTests++;
  409. bReturn = false;
  410. testResult = "[Failed]";
  411. eMsg = "N/A";
  412. try
  413. {
  414. iReturn = myStack.pop();
  415. bReturn = myStack.isEmpty();
  416. if (bReturn == true && iReturn == 10)
  417. {
  418. numPassedTests++;
  419. testResult = "[Passed]";
  420. }
  421. }
  422. catch (RuntimeException e)
  423. {
  424. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  425. }
  426. System.out.println("Test " + numTotalTests + ": pop() and then isEmpty() ==> " + testResult + "\n Expected: 10, true");
  427. if (eMsg.equals("N/A"))
  428. System.out.println(" Yours: " + iReturn + ", " + bReturn + "\n");
  429. else
  430. System.out.println(" Yours: " + eMsg + "\n");
  431. // Test 19
  432. numTotalTests++;
  433. iReturn = -1;
  434. testResult = "[Failed]";
  435. eMsg = "N/A";
  436. try
  437. {
  438. iReturn = myStack.size();
  439. if (iReturn == 0)
  440. {
  441. numPassedTests++;
  442. testResult = "[Passed]";
  443. }
  444. }
  445. catch (RuntimeException e)
  446. {
  447. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  448. }
  449. System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 0" );
  450. if (eMsg.equals("N/A"))
  451. System.out.println(" Yours: " + iReturn + "\n");
  452. else
  453. System.out.println(" Yours: " + eMsg + "\n");
  454. // Test 20
  455. numTotalTests++;
  456. testResult = "[Failed]";
  457. eMsg = "N/A";
  458. try
  459. {
  460. iReturn = myStack.pop();
  461. }
  462. catch (RuntimeException e)
  463. {
  464. if (!(e instanceof NullPointerException))
  465. {
  466. numPassedTests++;
  467. testResult = "[Passed]";
  468. }
  469. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  470. }
  471. System.out.println("Test " + numTotalTests + ": pop() ==> " + testResult + "\n Expected: a RuntimeException");
  472. System.out.println(" Yours: " + eMsg + "\n");
  473. // Test 21
  474. numTotalTests++;
  475. sReturn = "";
  476. testResult = "[Failed]";
  477. eMsg = "N/A";
  478. try
  479. {
  480. myStack.push(70);
  481. sReturn = myStack.toString();
  482. if (sReturn.equals("70 "))
  483. {
  484. numPassedTests++;
  485. testResult = "[Passed]";
  486. }
  487. }
  488. catch (RuntimeException e)
  489. {
  490. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  491. }
  492. System.out.println("Test " + numTotalTests + ": push(70) and then toString() ==> " + testResult + "\n Expected (from top to bottom): 70 ");
  493. if (eMsg.equals("N/A"))
  494. System.out.println(" Yours (from top to bottom): " + sReturn + "\n");
  495. else
  496. System.out.println(" Yours: " + eMsg + "\n");
  497. // Test 22
  498. numTotalTests++;
  499. iReturn = -1;
  500. testResult = "[Failed]";
  501. eMsg = "N/A";
  502. try
  503. {
  504. iReturn = myStack.top();
  505. if (iReturn == 70)
  506. {
  507. numPassedTests++;
  508. testResult = "[Passed]";
  509. }
  510. }
  511. catch (RuntimeException e)
  512. {
  513. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  514. }
  515. System.out.println("Test " + numTotalTests + ": top() ==> " + testResult + "\n Expected: 70");
  516. if (eMsg.equals("N/A"))
  517. System.out.println(" Yours: " + iReturn + "\n");
  518. else
  519. System.out.println(" Yours: " + eMsg + "\n");
  520. // Test 23
  521. numTotalTests++;
  522. iReturn = -1;
  523. testResult = "[Failed]";
  524. eMsg = "N/A";
  525. try
  526. {
  527. iReturn = myStack.pop();
  528. iReturn = myStack.size();
  529. if (iReturn == 0)
  530. {
  531. numPassedTests++;
  532. testResult = "[Passed]";
  533. }
  534. }
  535. catch (RuntimeException e)
  536. {
  537. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  538. }
  539. System.out.println("Test " + numTotalTests + ": pop() and then size() ==> " + testResult + "\n Expected: 0");
  540. if (eMsg.equals("N/A"))
  541. System.out.println(" Yours: " + iReturn + "\n");
  542. else
  543. System.out.println(" Yours: " + eMsg + "\n");
  544. System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests));
  545. }
  546. public static void TestP2()
  547. {
  548. try
  549. {
  550. ObjectInputStream in = new ObjectInputStream(new FileInputStream("testNumbers.dat"));
  551. StackApps myApps = new StackApps();
  552. ArrayList<Integer> results = new ArrayList<Integer>();
  553. ArrayList<String> numbers = new ArrayList<String>();
  554. results = (ArrayList<Integer>)in.readObject();
  555. numbers = (ArrayList<String>)in.readObject();
  556. String r;
  557. String eMsg;
  558. String currentLine;
  559. int numPassedTests = 0;
  560. int numTotalTests = 0;
  561. for (int i = 0; i < 5; i++)
  562. {
  563. numTotalTests++;
  564. currentLine = numbers.get(i);
  565. r = "";
  566. eMsg = "N/A";
  567. try
  568. {
  569. r = myApps.decToBin(Integer.valueOf(currentLine));
  570. }
  571. catch (RuntimeException e)
  572. {
  573. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  574. }
  575. System.out.print("Test " + numTotalTests + ": decToBin(" + currentLine + ") ==> ");
  576. if (r.equals(results.get(i)))
  577. {
  578. System.out.println("[Passed]");
  579. numPassedTests++;
  580. }
  581. else
  582. System.out.println("[Failed]");
  583. System.out.println(" Expected: " + results.get(i));
  584. if (eMsg.equals("N/A"))
  585. System.out.println(" Yours: " + r + "\n");
  586. else
  587. System.out.println(" Yours: " + eMsg + "\n");
  588. }
  589. for (int i = 5; i < numbers.size(); i++)
  590. {
  591. numTotalTests++;
  592. currentLine = numbers.get(i);
  593. String[] operands = currentLine.split(" ");
  594. r = "";
  595. eMsg = "N/A";
  596. try
  597. {
  598. r = myApps.addBigIntegers(operands[0], operands[1]);
  599. }
  600. catch (RuntimeException e)
  601. {
  602. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  603. }
  604. System.out.print("Test " + numTotalTests + ": (" + operands[0] + " + " + operands[1] + ") ==> ");
  605. if (r.equals(results.get(i)))
  606. {
  607. System.out.println("[Passed]");
  608. numPassedTests++;
  609. }
  610. else
  611. System.out.println("[Failed]");
  612. System.out.println(" Expected: " + results.get(i));
  613. if (eMsg.equals("N/A"))
  614. System.out.println(" Yours: " + r + "\n");
  615. else
  616. System.out.println(" Yours: " + eMsg + "\n");
  617. }
  618. System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests));
  619. }
  620. catch (Exception e)
  621. {
  622. System.out.println("Error occurred: " + e.getMessage());
  623. }
  624. }
  625. }