
import java.io.*;


public class IntListTest {


  private static void save( int itms, String dstName ) {
  
    System.out.println( "new list of " + itms + " items..." );
      
    IntList list = null;
    
    for ( int k=itms; k>0; k-- ) {
    
      list = new IntList( k, list );
    }
    System.out.println( "ref1, ref2 and ref3 refer to the same list..." );
      
    IntList ref1 = list;
    IntList ref2 = list;
    IntList ref3 = list;
    
    System.out.println( "ref1: " + ref1 );
    System.out.println( "ref2: " + ref2 );
    System.out.println( "ref3: " + ref3 );
      
    try {
      System.out.println( "opening output stream " + dstName + "..." );
      
      ObjectOutputStream dstFile = new ObjectOutputStream( new FileOutputStream(dstName) );
      
      System.out.println( "saving ref1 and ref2..." );
      
      ref1.save( dstFile );
      ref2.save( dstFile );
      
      System.out.println( "working with ref3 [cdr-cdr-cons]..." );
      
      ref3 = ref3.cdr();
      ref3 = ref3.cdr();
      ref3 = new IntList( 0, ref3 );
      
      System.out.println( "ref1: " + ref1 );
      System.out.println( "ref2: " + ref2 );
      System.out.println( "ref3: " + ref3 );
      
      System.out.println( "saving ref3..." );
      
      ref3.save( dstFile );
      
      dstFile.close();
      
      System.out.println( "done." );
    
    } catch ( Exception e ) {
      System.out.println( "save failure: " + e );
    }
  }
  
  
  private static void restore( String srcName ) {
  
    try {
      System.out.println( "opening input stream " + srcName + "..." );
      
      ObjectInputStream srcFile = new ObjectInputStream( new FileInputStream(srcName) );
      
      System.out.println( "restoring ref1..." );
      
      IntList ref1 = IntList.restore( srcFile );
      
      System.out.println( "ref1: " + ref1 );
      
      System.out.println( "working with ref1 [cdr/cut]..." );
      
      IntList tmp = ref1.cdr();
      ref1.cut();
      ref1 = tmp;
      tmp = ( tmp.cdr() ).cdr();
      tmp.cut();
      
      System.out.println( "restoring ref2 and ref3..." );
      
      IntList ref2 = IntList.restore( srcFile );
      IntList ref3 = IntList.restore( srcFile );
      
      System.out.println( "ref1: " + ref1 );
      System.out.println( "ref2: " + ref2 );
      System.out.println( "ref3: " + ref3 );
      
      srcFile.close();
      
      System.out.println( "done." );
    
    } catch ( Exception e ) {
      System.out.println( "restore failure: " + e );
    }
  }
  
  
  public static void main( String args[] )  {
  
    if ( args[0].equals("create") ) {               // java IntListTest create 5 models
    
      save( Integer.parseInt(args[1]), args[2] );
    
    } else if ( args[0].equals("restore") ) {       // java IntListTest restore models
    
      restore( args[1] );
    }
  }

}  // test class IntListTest


/*

% javac *.java
% java IntListTest create 5 testdata
new list of 5 items...
ref1, ref2 and ref3 refer to the same list...
ref1: <1, 2, 3, 4, 5>
ref2: <1, 2, 3, 4, 5>
ref3: <1, 2, 3, 4, 5>
opening output stream testdata...
saving ref1 and ref2...
working with ref3 [cdr-cdr-cons]...
ref1: <1, 2, 3, 4, 5>
ref2: <1, 2, 3, 4, 5>
ref3: <0, 3, 4, 5>
saving ref3...
done.
% java IntListTest restore testdata
opening input stream testdata...
restoring ref1...
ref1: <1, 2, 3, 4, 5>
working with ref1 [cdr/cut]...
restoring ref2 and ref3...
ref1: <2, 3, 4>
ref2: <1>
ref3: <0, 3, 4>
done.
% 

*/

