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.

375 lines
13 KiB

6 years ago
  1. // Test driver for the SongList class
  2. // Do not make any changes to this file!
  3. // Xiwei Wang
  4. public class TestSongList
  5. {
  6. public static void main(String[] args)
  7. {
  8. SongList mylist = new SongList();
  9. SongList builtlist = null;
  10. int numPassedTests = 0;
  11. int numTotalTests = 0;
  12. String testResult;
  13. // Test 1
  14. numTotalTests++;
  15. boolean bReturn = false;
  16. testResult = "[Failed]";
  17. String eMsg = "N/A";
  18. try
  19. {
  20. mylist.add("Lewis Capaldi", "Someone You Loved");
  21. bReturn = mylist.isEmpty();
  22. if (bReturn == false)
  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 + ": " + testResult + "\n add(\"Lewis Capaldi\", \"Someone You Loved\")\n Expected return of isEmpty(): false" );
  33. if (eMsg.equals("N/A"))
  34. System.out.println(" Your isEmpty() returns: " + bReturn + "\n");
  35. else
  36. System.out.println(" Yours: " + eMsg + "\n");
  37. // Test 2
  38. numTotalTests++;
  39. int iReturn = -1;
  40. testResult = "[Failed]";
  41. eMsg = "N/A";
  42. try
  43. {
  44. mylist.add("Lil Nas X", "Old Town Road");
  45. mylist.add("Ed Sheeran & Justin Bieber", "I Don't Care");
  46. iReturn = mylist.size();
  47. if (iReturn == 3)
  48. {
  49. numPassedTests++;
  50. testResult = "[Passed]";
  51. }
  52. }
  53. catch (RuntimeException e)
  54. {
  55. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  56. }
  57. System.out.println("Test " + numTotalTests + ": " + testResult + "\n add(\"Lil Nas X\", \"Old Town Road\")\n add(\"Ed Sheeran & Justin Bieber\", \"I Don't Care\")\n Expected return of size(): 3" );
  58. if (eMsg.equals("N/A"))
  59. System.out.println(" Your size() returns: " + iReturn + "\n");
  60. else
  61. System.out.println(" Yours: " + eMsg + "\n");
  62. // Test 3
  63. numTotalTests++;
  64. String sReturn = "";
  65. testResult = "[Failed]";
  66. eMsg = "N/A";
  67. try
  68. {
  69. sReturn = mylist.toString();
  70. if (sReturn.equals(" [Ed Sheeran & Justin Bieber - I Don't Care]\n [Lil Nas X - Old Town Road]\n [Lewis Capaldi - Someone You Loved]\n"))
  71. {
  72. numPassedTests++;
  73. testResult = "[Passed]";
  74. }
  75. }
  76. catch (RuntimeException e)
  77. {
  78. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  79. }
  80. System.out.println("Test " + numTotalTests + ": " + testResult + "\n Expected return of toString():\n [Ed Sheeran & Justin Bieber - I Don't Care]\n [Lil Nas X - Old Town Road]\n [Lewis Capaldi - Someone You Loved]\n");
  81. if (eMsg.equals("N/A"))
  82. System.out.println(" Your toString() returns:\n" + sReturn + "\n");
  83. else
  84. System.out.println(" Yours: " + eMsg + "\n");
  85. // Test 4
  86. numTotalTests++;
  87. bReturn = false;
  88. testResult = "[Failed]";
  89. eMsg = "N/A";
  90. try
  91. {
  92. bReturn = mylist.remove("Ava Max", "Sweet But Psycho");
  93. if (bReturn == false)
  94. {
  95. numPassedTests++;
  96. testResult = "[Passed]";
  97. }
  98. }
  99. catch (RuntimeException e)
  100. {
  101. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  102. }
  103. System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Ava Max\", \"Sweet But Psycho\")\n Expected: false" );
  104. if (eMsg.equals("N/A"))
  105. System.out.println(" Yours: " + bReturn + "\n");
  106. else
  107. System.out.println(" Yours: " + eMsg + "\n");
  108. // Test 5
  109. numTotalTests++;
  110. iReturn = -1;
  111. testResult = "[Failed]";
  112. eMsg = "N/A";
  113. try
  114. {
  115. mylist.add("Ava Max", "Sweet But Psycho");
  116. mylist.add("Billie Eilish", "Bad Guy");
  117. mylist.add("Mabel", "Don't Call Me Up");
  118. mylist.add("Lewis Capaldi", "Hold Me While You Wait");
  119. iReturn = mylist.size();
  120. if (iReturn == 7)
  121. {
  122. numPassedTests++;
  123. testResult = "[Passed]";
  124. }
  125. }
  126. catch (RuntimeException e)
  127. {
  128. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  129. }
  130. System.out.println("Test " + numTotalTests + ": " + testResult + "\n add(\"Ava Max\", \"Sweet But Psycho\")\n add(\"Billie Eilish\", \"Bad Guy\")\n add(\"Mabel\", \"Don't Call Me Up\")\n add(\"Lewis Capaldi\", \"Hold Me While You Wait\")\n Expected return of size(): 7" );
  131. if (eMsg.equals("N/A"))
  132. System.out.println(" Your size() returns: " + iReturn + "\n");
  133. else
  134. System.out.println(" Yours: " + eMsg + "\n");
  135. System.out.println(mylist);
  136. // Test 6
  137. numTotalTests++;
  138. bReturn = false;
  139. testResult = "[Failed]";
  140. eMsg = "N/A";
  141. try
  142. {
  143. bReturn = mylist.remove("Billie Eilish", "Bad Guy");
  144. if (bReturn == true)
  145. {
  146. numPassedTests++;
  147. testResult = "[Passed]";
  148. }
  149. }
  150. catch (RuntimeException e)
  151. {
  152. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  153. }
  154. System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Billie Eilish\", \"Bad Guy\")\n Expected: true" );
  155. if (eMsg.equals("N/A"))
  156. System.out.println(" Yours: " + bReturn + "\n");
  157. else
  158. System.out.println(" Yours: " + eMsg + "\n");
  159. // Test 7
  160. numTotalTests++;
  161. bReturn = false;
  162. testResult = "[Failed]";
  163. eMsg = "N/A";
  164. try
  165. {
  166. bReturn = mylist.remove("Ava Max", "Sweet But Psycho");
  167. if (bReturn == true)
  168. {
  169. numPassedTests++;
  170. testResult = "[Passed]";
  171. }
  172. }
  173. catch (RuntimeException e)
  174. {
  175. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  176. }
  177. System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Ava Max\", \"Sweet But Psycho\")\n Expected: true" );
  178. if (eMsg.equals("N/A"))
  179. System.out.println(" Yours: " + bReturn + "\n");
  180. else
  181. System.out.println(" Yours: " + eMsg + "\n");
  182. // Test 8
  183. numTotalTests++;
  184. sReturn = "";
  185. testResult = "[Failed]";
  186. eMsg = "N/A";
  187. try
  188. {
  189. mylist.add("Billie Eilish", "Bury A Friend");
  190. mylist.add("Lewis Capaldi", "Grace");
  191. sReturn = mylist.toString();
  192. if (sReturn.equals(" [Billie Eilish - Bury A Friend]\n [Mabel - Don't Call Me Up]\n [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\n [Ed Sheeran & Justin Bieber - I Don't Care]\n [Lil Nas X - Old Town Road]\n [Lewis Capaldi - Someone You Loved]\n"))
  193. {
  194. numPassedTests++;
  195. testResult = "[Passed]";
  196. }
  197. }
  198. catch (RuntimeException e)
  199. {
  200. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  201. }
  202. System.out.println("Test " + numTotalTests + ": " + testResult + "\n add(\"Billie Eilish\", \"Bury A Friend\")\n add(\"Lewis Capaldi\", \"Grace\")\n Expected return of toString():\n"
  203. + " [Billie Eilish - Bury A Friend]\n [Mabel - Don't Call Me Up]\n [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\n"
  204. + " [Ed Sheeran & Justin Bieber - I Don't Care]\n [Lil Nas X - Old Town Road]\n [Lewis Capaldi - Someone You Loved]\n");
  205. if (eMsg.equals("N/A"))
  206. System.out.println(" Your toString() returns:\n" + sReturn + "\n");
  207. else
  208. System.out.println(" Yours: " + eMsg + "\n");
  209. // Test 9
  210. numTotalTests++;
  211. bReturn = false;
  212. testResult = "[Failed]";
  213. eMsg = "N/A";
  214. try
  215. {
  216. bReturn = mylist.remove("Mabel", "Don't Call Me Up");
  217. if (bReturn == true)
  218. {
  219. numPassedTests++;
  220. testResult = "[Passed]";
  221. }
  222. }
  223. catch (RuntimeException e)
  224. {
  225. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  226. }
  227. System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Mabel\", \"Don't Call Me Up\")\n Expected: true" );
  228. if (eMsg.equals("N/A"))
  229. System.out.println(" Yours: " + bReturn + "\n");
  230. else
  231. System.out.println(" Yours: " + eMsg + "\n");
  232. // Test 10
  233. numTotalTests++;
  234. iReturn = -1;
  235. testResult = "[Failed]";
  236. eMsg = "N/A";
  237. try
  238. {
  239. iReturn = mylist.size();
  240. if (iReturn == 6)
  241. {
  242. numPassedTests++;
  243. testResult = "[Passed]";
  244. }
  245. }
  246. catch (RuntimeException e)
  247. {
  248. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  249. }
  250. System.out.println("Test " + numTotalTests + ": " + testResult + "\n Expected return of size(): 6" );
  251. if (eMsg.equals("N/A"))
  252. System.out.println(" Your size() returns: " + iReturn + "\n");
  253. else
  254. System.out.println(" Yours: " + eMsg + "\n");
  255. // Test 11
  256. numTotalTests++;
  257. builtlist = null;
  258. sReturn = "";
  259. testResult = "[Failed]";
  260. eMsg = "N/A";
  261. try
  262. {
  263. builtlist = mylist.buildList("Lewis Capaldi");
  264. sReturn = builtlist.toString();
  265. if (sReturn.equals(" [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\n [Lewis Capaldi - Someone You Loved]\n"))
  266. {
  267. numPassedTests++;
  268. testResult = "[Passed]";
  269. }
  270. }
  271. catch (RuntimeException e)
  272. {
  273. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  274. }
  275. System.out.println("Test " + numTotalTests + ": " + testResult + "\n mylist.buildList(\"Lewis Capaldi\")\n Expected return of toString() on the new list:\n"
  276. + " [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\n [Lewis Capaldi - Someone You Loved]\n");
  277. if (eMsg.equals("N/A"))
  278. System.out.println(" Your toString() returns:\n" + sReturn + "\n");
  279. else
  280. System.out.println(" Yours: " + eMsg + "\n");
  281. // Test 12
  282. numTotalTests++;
  283. builtlist = null;
  284. sReturn = "";
  285. testResult = "[Failed]";
  286. eMsg = "N/A";
  287. try
  288. {
  289. builtlist = mylist.buildList("Lil Nas X");
  290. sReturn = builtlist.toString();
  291. if (sReturn.equals(" [Lil Nas X - Old Town Road]\n"))
  292. {
  293. numPassedTests++;
  294. testResult = "[Passed]";
  295. }
  296. }
  297. catch (RuntimeException e)
  298. {
  299. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  300. }
  301. System.out.println("Test " + numTotalTests + ": " + testResult + "\n mylist.buildList(\"Lil Nas X\")\n Expected return of toString() on the new list:\n"
  302. + " [Lil Nas X - Old Town Road]\n");
  303. if (eMsg.equals("N/A"))
  304. System.out.println(" Your toString() returns:\n" + sReturn + "\n");
  305. else
  306. System.out.println(" Yours: " + eMsg + "\n");
  307. // Test 13
  308. numTotalTests++;
  309. builtlist = null;
  310. sReturn = "";
  311. testResult = "[Failed]";
  312. eMsg = "N/A";
  313. try
  314. {
  315. builtlist = mylist.buildList("Ava Max");
  316. sReturn = builtlist.toString();
  317. if (sReturn.equals(""))
  318. {
  319. numPassedTests++;
  320. testResult = "[Passed]";
  321. }
  322. }
  323. catch (RuntimeException e)
  324. {
  325. eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
  326. }
  327. System.out.println("Test " + numTotalTests + ": " + testResult + "\n mylist.buildList(\"Ava Max\")\n Expected return of toString() on the new list:\n An empty list\n");
  328. if (eMsg.equals("N/A"))
  329. System.out.println(" Your toString() returns:\n" + (sReturn.equals("") ? " An empty list\n" : sReturn));
  330. else
  331. System.out.println(" Yours: " + eMsg + "\n");
  332. System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests));
  333. }
  334. }