/* * [TestLinkedList.java] * * Summary: Tests LinkedList class to ensure it is bug free. Also demonstrates the use of its methods. * * Copyright: (c) 1997-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1997-05-13 initial release. * 1.1 1997-05-29 full test harness * 1.2 1997-06-16 add test for insertElement, also numbers for failures. * 1.3 1998-06-24 add JavaDOC */ package com.mindprod.linkedlist; import java.util.Enumeration; import java.util.Random; import java.util.Vector; import static java.lang.System.*; /** * Tests LinkedList class to ensure it is bug free. Also demonstrates the use of its methods. * * @author Roedy Green, Canadian Mind Products * @version 1.3 1998-06-24 add JavaDOC * @since 1997-05-13 */ public class TestLinkedList { /** * Enumerate a list and display each element on the console. * * @param c the list to be enumerated. */ private static void chase( LinkedList c ) { out.print( "[ " ); for ( Enumeration e = c.elements(); e.hasMoreElements(); ) { Item item = ( Item ) e.nextElement(); item.show(); } // end for out.println( "]" ); } // end chase /** * Exercise the LinkedList class. * * @param args command line arguments are ignored. */ public static void main( String[] args ) { LinkedList c = new LinkedList<>(); LinkedList e = new LinkedList<>();// empty list Item u = new Item( 0 ); Item v = new Item( 1 ); Item w = new Item( 2 ); Item x = new Item( 3 ); Item y = new Item( 4 ); Item z = new Item( 5 ); out.println( "tests on an empty list" ); if ( e.firstElement() != null ) { out.println( "firstElement failure 1" ); } if ( e.getElement() != null ) { out.println( "getElement failure 2" ); } if ( e.indexOf( v ) != -1 ) { out.println( "indexOf failure 3" ); } if ( !e.isEmpty() ) { out.println( "isEmpty failure 4" ); } if ( e.lastElement() != null ) { out.println( "lastElement failure 5" ); } if ( e.lastIndexOf( z ) != -1 ) { out.println( "lastIndexOf failure 6" ); } if ( e.size() != 0 ) { out.println( "size failure 7" ); } chase( e ); out.println( "tests on a non-null list" ); c.addElement( u ); c.addElement( v ); c.addElement( w ); c.addElement( x ); c.addElement( y ); c.addElement( z ); c.consistency(); chase( c ); Random wheel = new Random(); for ( int i = 0; i < 1000; i++ ) { int r = ( wheel.nextInt() & Integer.MAX_VALUE ) % 5; c.setCursor( r ); c.getElement().expect( r ); } c.removeAllElements(); if ( !c.isEmpty() ) { out.println( "isEmpty failure 8" ); } c.addElement( u ); c.addElement( v ); c.addElement( w ); if ( c.isEmpty() ) { out.println( "isEmpty failure 9" ); } c.elementAt( 0 ).expect( 0 ); c.elementAt( 1 ).expect( 1 ); c.elementAt( 2 ).expect( 2 ); c.firstElement().expect( 0 ); if ( c.indexOf( v ) != 1 ) { out.println( "indexOf failure 10" ); } c.insertElementAt( x, 2 ); c.insertElementAt( y, 4 ); if ( c.size() != 5 ) { out.println( "size failure 11" ); } if ( c.lastIndexOf( x ) != 2 ) { out.println( "lastIndexOf failure 12" ); } c.elementAt( 0 ).expect( 0 ); c.elementAt( 1 ).expect( 1 ); c.elementAt( 2 ).expect( 3 ); c.elementAt( 3 ).expect( 2 ); c.elementAt( 4 ).expect( 4 ); c.lastElement().expect( 4 ); c.removeElementAt( 0 ); if ( c.size() != 4 ) { out.println( "size failure 13" ); } c.elementAt( 0 ).expect( 1 ); c.elementAt( 1 ).expect( 3 ); c.elementAt( 2 ).expect( 2 ); c.elementAt( 3 ).expect( 4 ); c.removeElementAt( 3 ); if ( c.size() != 3 ) { out.println( "size failure 14" ); } c.elementAt( 0 ).expect( 1 ); c.elementAt( 1 ).expect( 3 ); c.elementAt( 2 ).expect( 2 ); c.setCursor( 1 ); c.getElement().expect( 3 ); c.removeElementAt( 2 ); if ( c.size() != 2 ) { out.println( "size failure 15" ); } c.elementAt( 0 ).expect( 1 ); c.elementAt( 1 ).expect( 3 ); c.removeAllElements(); c.addElement( u ); c.addElement( v ); c.addElement( w ); c.setCursor( 1 ); c.setElement( x ); c.setElementAt( y, 0 ); c.elementAt( 0 ).expect( 4 ); c.elementAt( 1 ).expect( 3 ); c.elementAt( 2 ).expect( 2 ); Vector q = c.toVector(); LinkedList d = new LinkedList<>( q ); if ( d.size() != 3 ) { out.println( "size failure 16" ); } d.elementAt( 0 ).expect( 4 ); d.elementAt( 1 ).expect( 3 ); c.elementAt( 2 ).expect( 2 ); boolean except = false; try { c.setElementAt( z, 8 ); } catch ( ArrayIndexOutOfBoundsException ex ) { except = true; } if ( !except ) { out.println( "failure 17 to raise exception." ); } out.println( "addElement test" ); c.removeAllElements(); c.insertElement( u ); c.insertElement( v ); c.insertElement( w ); if ( c.size() != 3 ) { out.println( "size failure 18" ); } c.elementAt( 0 ).expect( 2 ); c.elementAt( 1 ).expect( 1 ); c.elementAt( 2 ).expect( 0 ); out.println( "Test complete" ); } // end main } // end class TestLiskedList /** * Sample Item objects to put on our LinkedList to test it. */ class Item { /** * Unique identifier for this Item object */ private final int id; /** * Constructor * * @param id a unique number to identify each item when it is displayed. */ public Item( int id ) { this.id = id; } /** * Check that this object has the given expected id. Used to test that list operations are shuffling objects as * expected. * * @param expectedId the id the item should have if the list functions are working properly. */ public void expect( int expectedId ) { if ( expectedId != id ) { out.println( "mismatch: got:" + id + " expected:" + expectedId ); } } // end expect /** * Display this object on the console. */ public void show() { out.print( id + " " ); } // end show } // end class Item // -30-