/* * Should generalize the test_* routines to reduce redundancy. */ import list.*; public class TestListClient { private static double score; private static int num_incorrect = 0; /* * List equality. * Homework didn't specify to implement this. * Even if it did, couldn't trust student to correctly implement it. * As a result, forced to implemment in non-OOP style. */ private static boolean int_list_equal(AList list1, AList list2) { if (list1 == ListFactory.Singleton.makeEmptyList () && list2 == ListFactory.Singleton.makeEmptyList ()) return true; else if (list1 == ListFactory.Singleton.makeEmptyList () || list2 == ListFactory.Singleton.makeEmptyList ()) return false; else return ((Integer) list1.getFirst()).intValue() == ((Integer) list2.getFirst()).intValue() && int_list_equal (list1.getRest(), list2.getRest()); } private static void test_header() { System.out.println ("*******************************************"); } private static void test_end(boolean correct, String intended_result_rep, double points) { if (!correct) { System.out.println (" Incorrect. Should = " + intended_result_rep); num_incorrect += 1; System.out.println (" Score: -" + points); score -= points; } } private static void test_toString(AList list, String intended_result, double points) { boolean correct; System.out.print (list + ".toString() = "); try { String result = list.toString(); System.out.println (result); correct = result.equals(intended_result); } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = false; } test_end (correct,intended_result,points); } private static void test_makeClone(AList list, AList intended_result, String list_rep, String intended_result_rep, double points) { boolean correct; System.out.print (list + ".makeClone() = "); try { AList result = list.makeClone(); System.out.println (result); correct = (result == ListFactory.Singleton.makeEmptyList () || result != intended_result) && int_list_equal(result,intended_result); } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = false; } test_end (correct,intended_result_rep,points); } private static void test_nthElement_int_pass(AList list, int n, int intended_result, String list_rep, String intended_result_rep, double points) { boolean correct; System.out.print (list + ".nthElement(" + n + ") = "); try { Integer result = (Integer) list.nthElement(n); System.out.println (result); correct = (result.intValue() == intended_result); } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = false; } test_end (correct,intended_result_rep,points); } private static void test_nthElement_int_fail(AList list, int n, String list_rep, double points) { boolean correct; System.out.print (list + ".nthElement(" + n + ") = "); try { Integer result = (Integer) list.nthElement(n); System.out.println (result); correct = false; } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = true; } test_end (correct,"Exception",points); } private static void test_lastElement_int_pass(AList list, int intended_result, String list_rep, String intended_result_rep, double points) { boolean correct; System.out.print (list + ".lastElement() = "); try { Integer result = (Integer) list.lastElement(); System.out.println (result); correct = (result.intValue() == intended_result); } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = false; } test_end (correct,intended_result_rep,points); } private static void test_lastElement_int_fail(AList list, String list_rep, double points) { boolean correct; System.out.print (list + ".lastElement() = "); try { Integer result = (Integer) list.lastElement(); System.out.println (result); correct = false; } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = true; } test_end (correct,"Exception",points); } private static void test_firstElements_int_pass(AList list, int num_elts, AList intended_result, String list_rep, String intended_result_rep, double points) { boolean correct; System.out.print (list + ".firstElements(" + num_elts + ") = "); try { AList result = list.firstElements(num_elts); System.out.println (result); correct = int_list_equal(result,intended_result); } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = false; } test_end (correct,intended_result_rep,points); } private static void test_firstElements_int_fail(AList list, int num_elts, String list_rep, double points) { boolean correct; System.out.print (list + ".firstElements(" + num_elts + ") = "); try { AList result = list.firstElements(num_elts); System.out.println (result); correct = false; } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = true; } test_end (correct,"Exception",points); } private static void test_reverse(AList list, AList intended_result, String list_rep, String intended_result_rep, double points) { boolean correct; System.out.print (list + ".reverse() = "); try { AList result = list.reverse(); System.out.println (result); correct = int_list_equal(result,intended_result); } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = false; } test_end (correct,intended_result_rep,points); } private static void test_append(AList list1, AList list2, AList intended_result, String list1_rep, String list2_rep, String intended_result_rep, double points) { boolean correct; System.out.print (list1 + ".append(" + list2 + ") = "); try { AList result = list1.append(list2); System.out.println (result); correct = int_list_equal(result,intended_result); } catch (RuntimeException e) { System.out.println ("EXCEPTION: " + e); correct = false; } test_end (correct,intended_result_rep,points); } public static void main (String[] args) { System.out.println ("Testing for COMP 212 Spring 2001 Assignment 1:"); AList empty = ListFactory.Singleton.makeEmptyList (); AList list1 = ListFactory.Singleton.makeNEList (new Integer (-1), ListFactory.Singleton.makeNEList (new Integer (-5), ListFactory.Singleton.makeNEList (new Integer (2), ListFactory.Singleton.makeNEList (new Integer (6),empty)))); String empty_rep = "()"; String list1_rep = "(-1 -5 2 6)"; boolean correct; double max_score = 7 * 7; // 7 methods, 7 points each score = max_score; /************************************************************* * toString tests *************************************************************/ test_header(); test_toString(empty,empty_rep, 3); test_toString(list1,list1_rep, 4); /************************************************************* * makeClone tests *************************************************************/ test_header(); test_makeClone(empty,empty, empty_rep,empty_rep, 3); test_makeClone(list1,list1, list1_rep,list1_rep, 4); /************************************************************* * nthElement tests *************************************************************/ test_header(); test_nthElement_int_fail(empty,-5, empty_rep, .5); test_nthElement_int_fail(empty,0, empty_rep, 2); test_nthElement_int_fail(empty,3, empty_rep, .5); test_nthElement_int_fail(list1,-5, list1_rep, .5); test_nthElement_int_pass(list1,0,-1, list1_rep,"-1", 1.5); test_nthElement_int_pass(list1,3,6, list1_rep,"6", 1.5); test_nthElement_int_fail(list1,5, list1_rep, .5); /************************************************************* * lastElement tests *************************************************************/ test_header(); test_lastElement_int_fail(empty, empty_rep, 3); test_lastElement_int_pass(list1,6, list1_rep,"6", 4); /************************************************************* * firstElements tests *************************************************************/ test_header(); test_firstElements_int_fail(empty,-5, empty_rep, .5); test_firstElements_int_pass(empty,0,empty, empty_rep,empty_rep, 2); test_firstElements_int_fail(empty,3, empty_rep, .5); test_firstElements_int_fail(list1,-5, list1_rep, .5); test_firstElements_int_pass(list1,0,empty, list1_rep,empty_rep, 1.5); test_firstElements_int_pass(list1,3, ListFactory.Singleton.makeNEList (new Integer (-1), ListFactory.Singleton.makeNEList (new Integer (-5), ListFactory.Singleton.makeNEList (new Integer (2),empty))), list1_rep,"(-1 -5 2)", 1.5); test_firstElements_int_fail(list1,5, list1_rep, .5); /************************************************************* * reverse tests *************************************************************/ test_header(); test_reverse(empty,empty, empty_rep,empty_rep, 3); test_reverse(list1, ListFactory.Singleton.makeNEList (new Integer (6), ListFactory.Singleton.makeNEList (new Integer (2), ListFactory.Singleton.makeNEList (new Integer (-5), ListFactory.Singleton.makeNEList (new Integer (-1),empty)))), list1_rep,"(6 2 -5 -1)", 4); /************************************************************* * append tests *************************************************************/ test_header(); test_append(empty,empty,empty, empty_rep,empty_rep,empty_rep, 1.5); test_append(empty,list1,list1, empty_rep,list1_rep,list1_rep, 1.5); test_append(list1,empty,list1, list1_rep,empty_rep,list1_rep, 2); test_append(list1,list1, ListFactory.Singleton.makeNEList (new Integer (-1), ListFactory.Singleton.makeNEList (new Integer (-5), ListFactory.Singleton.makeNEList (new Integer (2), ListFactory.Singleton.makeNEList (new Integer (6),list1)))), list1_rep,list1_rep,"(-1 -5 2 6 -1 -5 2 6)", 2); System.out.println ("*******************************************"); System.out.println ("Tests failed : " + num_incorrect); System.out.println ("Correctness score : " + score + " of " + max_score); if (score < max_score) System.out.println (" (May be increased for partial credit.)"); } }