/** * Code to unit test the AList implementation of ListInterface * * @author R. Flatland */ public class TestListInterface { private int score; private int maxscore; public TestListInterface() { } public void test() { score = 0; maxscore = 0; AList list1 = new AList( 10 ); // Test add methods and getLength method list1.add( 1 ); list1.add( 2 ); list1.add( 3 ); list1.add( 4 ); if ( list1.getLength() == 4 ) score++; maxscore++; if ( list1.add( 5 ) == true ) score++; maxscore++; if ((list1.getEntry(2) != null) && (list1.getEntry(2) == 2 )) score++; maxscore++; if (( list1.getEntry(5) != null) && (list1.getEntry(5) == 5 )) score++; maxscore++; if ( list1.add(2, 6) == true) score++; maxscore++; if (( list1.getEntry(2) != null) && (list1.getEntry(2) == 6 )) score++; maxscore++; if (( list1.getEntry(3) != null) && (list1.getEntry(3) == 2 )) score++; maxscore++; if (( list1.getEntry(6) != null) && (list1.getEntry(6) == 5 )) score++; maxscore++; if ( list1.add(9, 6) == false) score++; maxscore++; if ( list1.add(20, 6) == false) score++; maxscore++; if ( list1.add(7, 6) == true) score++; maxscore++; if ( list1.getLength() == 7 ) score++; maxscore++; // Test replace method AList list2 = new AList( 10 ); if (list2.replace( 4, 8 ) == false) score++; maxscore++; if (list2.replace( 0, 8 ) == false) score++; maxscore++; if (list2.replace( -3, 8 ) == false) score++; maxscore++; for (int i = 1; i <= 10; i++ ) list2.add( i ); if ( list2.replace( 5, 15 ) == true ) score++; maxscore++; if (( list2.getEntry(5) != null) && (list2.getEntry(5) == 15 )) score++; maxscore++; if ( list2.replace( -5, 4 ) == false ) score++; maxscore++; // Test Remove method AList list3 = new AList( 10 ); if (list3.remove( 5 ) == null) score++; maxscore++; for (int i = 1; i <= 10; i++ ) list3.add( i ); if ((list3.remove( 5 ) != null) && (list3.remove( 5 ) == 5)) score++; maxscore++; // Test isEmpty & isFull & clear methods AList list4 = new AList( 10 ); if (list4.isEmpty() == true) score++; maxscore++; if (list4.isFull() == false) score++; maxscore++; list4.add( 1 ); if (list4.isEmpty() == false) score++; maxscore++; for (int i = 2; i <= 10; i++ ) list4.add( i ); if (list4.isFull() == true) score++; maxscore++; list4.clear(); if (list4.isEmpty() == true) score++; maxscore++; // Test contains method AList list5 = new AList( 10 ); list5.add(5); if (list5.contains(5)){ score++; } if(list5.contains(10000)== false){ score++; } maxscore++; // Test reverse method AList list6 = new AList( 3 ); list6.add(1); list6.add(2); list6.add(3); Integer temp = list6.getEntry(1); list6.reverse(); if(( list6.getEntry(list6.getLength()) != null) && (temp.equals(list6.getEntry(list6.getLength())))){ score++; } maxscore++; System.out.println( "You scored " + score + " out of a possible " + maxscore + " points." ); if ( score < maxscore ) System.out.println( "Keep on it - you can fix the bugs!" ); else System.out.println( "You rock -- a perfect score!" ); } }