/** * A class to test LinkedList.java * * @author Josef Nilsen * @version v1.0 BETA */public class testList{ public static void main(String args[]) { test(); } public static void test() { LinkedList<String> testList = new LinkedList<String>(); boolean working = true; System.out.println("** LinkedList simulation log **\n"); String[] inputData = {null, "testOne", "testTwo", "testThree"}; for(int i=0; i<4; i++) { if(testList.size()==i) { System.out.println("The method size() returned "+testList.size()+", the actual size is "+i+"! Success!"); } else { System.out.println("The method size() returned "+testList.size()+", the actual size is "+i+"! Failure!"); working = false; } if(testList.isEmpty() == (i==0)) { System.out.println("Success! The boolean expression i==0 (true when i=0) in the loop, is only equal to isEmpty (true when empty), when it's empty!."); } else { System.out.println("Failure! The boolean expression i==0 (true when i=0) in the loop, isn't equal to isEmpty (true when empty), when it's empty!."); working = false; } if(i%2 == 0) { testList.addFirst(inputData[i]); if(testList.getFirst() == inputData[i]) { System.out.println("Success! The method getFirst() returned "+testList.getFirst()+", which is correct!"); } else { System.out.println("Failure! The method getFirst() returned "+testList.getFirst()+", which is not correct!"); working = false; } } else { testList.addLast(inputData[i]); if(testList.getLast() == inputData[i]) { System.out.println("Success! The method getLast() returned "+testList.getLast()+", which is correct!"); } else { System.out.println("Failure! The method getLast() returned "+testList.getLast()+", which is not correct!"); working = false; } } } if(testList.toString().equals("[testTwo], [*null*], [testOne], [testThree]")) { System.out.println("The output from toString() matches the actual data and pattern. Success!"); } else { System.out.println("The output from toString() doesn't match the actual data and pattern. Failure!"); working = false; } if(testList.get(0).equals("testTwo")) { System.out.println("The output from get(0) matches the actual data. Success!"); } else { System.out.println("The output from get(0) didn't match the actual data. Success!"); working = false; } testList.removeFirst(); System.out.println("Removing the first element with removeFirst()..."); if(testList.toString().equals("[*null*], [testOne], [testThree]")) { System.out.println("The first element was removed. Success!"); } else { System.out.println("The first element wasn't removed. Failure!"); working = false; } testList.clear(); System.out.println("Clearing the list with clear()..."); if(testList.size() == 0) { System.out.println("The cleared list (should be size=0) has a size of "+testList.size()+" elements! Success!"); } else { System.out.println("The cleared list (should be size=0) has a size of "+testList.size()+" elements! Failure!"); working = false; } if(working) { System.out.println("\nSummary: The LinkedList is working properly!"); } else { System.out.println("\nSummary: The LinkedList is NOT working properly!"); } }}