001 /* Copyright 2000, 2001, Compaq Computer Corporation */
002
003 package javafe.util;
004
005 import java.util.Random;
006 import java.io.*;
007
008 public class CorrelatedReaderTest
009 {
010 static final int SEED = 0xCAFEBABE;
011 static final int MASKRESET = 0x1f;
012 static final int MASKSUBREADER = 0x1ff;
013 static final int MASKDISCARD = 0xf;
014
015 //----------------------------------------------------------------------
016
017 //@ ensures false;
018 //@ diverges true;
019 //@ signals (Exception) false;
020 static void error() {
021 System.out.println("Usage: java CorrelatedReader [locs|reset|subreader]");
022 System.exit(1);
023 }
024
025 /** A test harness.
026 */
027
028 //@ requires \nonnullelements(argv);
029 public static void main(String[] argv)
030 throws IOException, IndexOutOfBoundsException {
031
032 CorrelatedReader cin = new FileCorrelatedReader( System.in, "stdin" );
033 Random random = new Random( SEED ); // deterministic test
034 int c;
035
036 try {
037 if( argv.length != 1 ) {
038 error();
039 }
040 else if( argv[0].equals("locs") ) {
041 while( (c=cin.read()) != -1 ) {
042 int loc=cin.getLocation();
043 System.out.println("["+c+"'"+ (new Character((char)c)).toString()
044 +"' loc:"+ loc+" "+Location.toString(loc)
045 +" offset "+Location.toOffset(loc)+"]");
046 }
047 }
048 else if( argv[0].equals("reset") ) {
049
050 while( (c=cin.read()) != -1 ) {
051 cin.mark();
052 c=cin.read();
053 int loc=cin.getLocation();
054 /*
055 System.out.println("["+c+"'"+ (new Character((char)c)).toString()
056 +"' loc:"+ loc+" "+Location.toString(loc)
057 +" offset "+Location.toOffset(loc)+"]");
058 */
059
060 int toRead = random.nextInt() & MASKRESET ;
061 for(int j=0; j<toRead; j++) cin.read();
062 cin.reset();
063 int c2=cin.read();
064 int loc2 = cin.getLocation();
065 Assert.notFalse( c==c2 && loc==loc2, //@ nowarn Pre;
066 "c="+c
067 +" c2="+c2
068 +" loc="+loc
069 +" loc2="+loc2);
070 }
071 }
072 else if( argv[0].equals("subreader") ) {
073
074 while( (c=cin.read()) != -1 ) {
075
076 StringBuffer sb = new StringBuffer();
077
078
079
080 // mark the position of the next character;
081 // then get the next character and its location:
082 cin.mark();
083 sb.append( (char)cin.read() );
084 int loc=cin.getLocation();
085
086
087 int toRead = random.nextInt() & MASKSUBREADER;
088 int read;
089
090 for(read=1; read<toRead && (c=cin.read()) != -1; read++)
091 sb.append( (char)c );
092
093
094 int discard = random.nextInt() & MASKDISCARD;
095 if( discard > read )
096 discard = read;
097
098 CorrelatedReader subReader = cin.createReaderFromMark(discard);
099
100 // Test location if subReader not empty:
101 if (discard<read) {
102 // loc2 = location of first character in subReader:
103 subReader.mark();
104 subReader.read();
105 int loc2 = subReader.getLocation();
106 subReader.reset();
107
108 Assert.notFalse(loc==loc2, //@ nowarn Pre;
109 "loc="+loc
110 +" loc2="+loc2
111 +" read="+read
112 +" discard="+discard );
113 }
114
115
116 for(int j=0; j<read-discard; j++) {
117 int sc = subReader.read();
118 Assert.notFalse(sb.charAt(j) == sc, //@ nowarn Pre;
119 "sc='"+(char)sc+"'"
120 +" sb("+j+")='"+sb.charAt(j)+"'"
121 +"sb='"+sb+"'"
122 +" loc="+loc
123 +" read="+read+" discard="+discard );
124 }
125
126 Assert.notFalse( subReader.read() == -1 ); //@ nowarn Pre;
127 }
128 }
129 else
130 error();
131 } catch( AssertionFailureException e ) {
132 e.printStackTrace();
133 System.exit(1);
134 }
135 }
136 }