/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ /** * @author Uwe Schmidt * * ein Testprogramm fuer die LinkedList Klasse * */ public class LinkedListTest { //-------------------- // count failed tests static int failed = 0; //-------------------- static void test( Object result, Object expected, String testCase ) { if ( ! result.equals(expected) ) { ++ failed; System.out.println ("Test Case " + testCase + " failed\n" + "result : " + result.toString() + "\n" + "expected : " + expected.toString() ); } } //-------------------- static void test( boolean result, boolean expected, String testCase ) { test( new Boolean(result), new Boolean(expected), testCase ); } //-------------------- static void test( int result, int expected, String testCase ) { test( new Integer(result), new Integer(expected), testCase ); } //-------------------- public static void main(String[] argv) { //-------------------- // toString Tests test(LinkedList.mkEmptyList().toString(), "[]", "toString 1.1"); test(LinkedList.mkOne(new Integer(42)).toString(), "[42]", "toString 1.2"); test(LinkedList.mkOne("abc").cons("def").toString(), "[def,abc]", "toString 1.3"); test(LinkedList.mkOne("abc").append(new Integer(13)).toString(), "[abc,13]", "toString 1.4"); //-------------------- // isEmpty Tests test(LinkedList.mkEmptyList().isEmpty(), true, "isEmpty 1.1"); test(LinkedList.mkOne(new Integer(42)).isEmpty(), false, "isEmpty 1.2"); test(LinkedList.mkEmptyList().cons("abc").isEmpty(), false, "isEmpty 1.3"); test(LinkedList.mkEmptyList().concat(LinkedList.mkEmptyList()).isEmpty(), true, "isEmpty 1.4"); test(LinkedList.mkOne("xyz").concat(LinkedList.mkEmptyList()).isEmpty(), false, "isEmpty 1.5"); test(LinkedList.mkEmptyList().concat(LinkedList.mkOne("abc")).isEmpty(), false, "isEmpty 1.6"); test(LinkedList.mkOne("abc").concat(LinkedList.mkOne("abc")).isEmpty(), false, "isEmpty 1.7"); //-------------------- // isIn Tests test(LinkedList.mkEmptyList().isIn("abc"), false, "isIn 1.1"); test(LinkedList.mkOne("xyz").isIn("abc"), false, "isIn 1.2"); test(LinkedList.mkOne("abc").isIn("abc"), true, "isIn 1.3"); test(LinkedList.mkOne("abc").isIn(new Integer(42)), false, "isIn 1.4"); { Integer i = new Integer(2001); LinkedList l = LinkedList.mkEmptyList().insert(i); test(l.isIn(i), true, "isIn 1.5.1"); l = l.remove(i); test(l.isIn(i), false, "isIn 1.5.2"); } //-------------------- // len Tests test(LinkedList.mkEmptyList().len(), 0, "len 1.1"); test(LinkedList.mkOne("abc").len(), 1, "len 1.2"); //-------------------- { Integer e = new Integer(42); LinkedList l = LinkedList.mkEmptyList(); // Liste mit 5 gleichen Elementen for (int i = 0; i < 5; ++i) { l = l.cons(e); test(l.len(), i + 1, "len 1.3." + (i + 1)); } // Liste schrittweise verkuerzen for (int i = 0; i < 5; ++i) { l = l.remove(e); test(l.len(), 5 - (i + 1), "len 1.4." + (i + 1)); } test(l.isEmpty(), true, "len 1.5"); // 3 x das gleiche Element einfuegen for (int i = 0; i < 3; ++i) { l = l.insert(e); test(l.len(), 1, "len 1.6." + (i + 1)); } } //-------------------- // Accumulate Tests mit leerer Liste { LinkedList l = LinkedList.mkEmptyList(); // die Laenge mit NoOfElements berechnet { Object result = l.forall(new NoOfElements()); int len = ((Integer)result).intValue(); test(len, 0, "NoOfElements 1.1"); } // das IntegerSum Kommando { Object result = l.forall(new IntegerSum()); int sum = ((Integer)result).intValue(); test(sum, 0, "IntegerSum 1.1"); } // ToString Konversion test(l.forall(new ToString("[", ",", "]")), "[]", "ToString 1.1"); } //-------------------- // Accumulate Tests mit Liste [5,4,3,2,1] { LinkedList l = LinkedList.mkEmptyList(); for (int i = 1; i < 6; ++i) { l = l.cons( new Integer(i) ); } // die Laenge mit NoOfElements berechnet { Object result = l.forall(new NoOfElements()); int len = ((Integer)result).intValue(); test(len, 5, "NoOfElements 1.2"); } // das IntegerSum Kommando { Object result = l.forall(new IntegerSum()); int sum = ((Integer)result).intValue(); test(sum, 15, "IntegerSum 1.2"); } // ToString Konversion test(l.forall(new ToString("[", ",", "]")), "[5,4,3,2,1]", "ToString 1.1"); } //-------------------- // len1 Tests { LinkedList l = LinkedList.mkEmptyList(); test(l.len1(), l.len(), "len1 1.1"); l.cons("abc"); test(l.len1(), l.len(), "len1 1.2"); l.cons("xyz"); test(l.len1(), l.len(), "len1 1.3"); } //-------------------- // toString1 Tests { LinkedList l = LinkedList.mkEmptyList(); test(l.toString1(), l.toString(), "toString1 1.1"); l.cons("abc"); test(l.toString1(), l.toString(), "toString1 1.2"); l.cons("xyz"); test(l.toString1(), l.toString(), "toString1 1.3"); } //-------------------- // statistics System.out.println ( (failed == 0) ? "all tests passed" : (failed + " Test(s) failed") ); } }