/* Test.dtd for MetaTF 1.2

cached section S1 1 {
    one : (tag, desc) { tag = 1; string desc = none; }
}

section S2 2 {
    two :   (tag, a, a, b, cmt) { tag=2; int a = none; long b = none; string cmt = none;}
    three : (tag, a, b, cmt)    { tag=3; int a = none; index b = sectionOffset S1; string cmt = none; }
}

*/

import Test.*;
import MetaTF.Runtime.*;
import MetaTF.BadRecordException;
import MetaTF.MetaTFConstants;
import java.io.*;

class TestDriver  {
    public static void main(String[] args) {
        
        try {
            LowLevelWriter wtr = new AsciiWriter(new PrintStream(new FileOutputStream("test.trace")));
            Test.init();
            new Comment("TEST TRACE for MetaTF 1.2").putRecord(wtr);
            new Comment("Format: the string contains <record>.putRecord(wtr); trace: <expected trace record>").putRecord(wtr);

            new Comment("Test.putSection(wtr,1); trace: $ 1").putRecord(wtr);
            Test.putSection(wtr,1);

            // Set up section one
            new Comment("Some section data, 20 records").putRecord(wtr);
            S1_One[] ones = new S1_One[20];
            for(int i=0; i<20; i++) {
               ones[i] = new S1_One("S1_One(cmt).putRecord(wtr); trace: 1 ");
               ones[i].putRecord(wtr);
            }

            new Comment("Test.putSection(wtr,2); trace: $ 2").putRecord(wtr);
            Test.putSection(wtr, 2);

            new S2_Two(1, 10, 100, "S2_Two(1,10,100,cmt).putRecord(wtr); trace: 2 1 "+Integer.toHexString(10)+" "+Long.toHexString(100)).putRecord(wtr);

            new Comment("S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.Default, 333); trace: ! 2 2 1 "+Long.toHexString(333)).putRecord(wtr);
            S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.Default, 333);
            new S2_Two(1, 10, 100, "S2_Two(1, 10, 100, cmt).putRecord(wtr); trace: 2 1 "+Long.toHexString(100)).putRecord(wtr);
            new S2_Two(1, 20, 100, "S2_Two(1, 20, 100, cmt).putRecord(wtr); trace: 2 1 "+Long.toHexString(100)).putRecord(wtr);

            new Comment("S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.BaseOffset, 100); trace: ! 2 2 0 "+Long.toHexString(100)).putRecord(wtr);
            S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.BaseOffset, 100);
            new S2_Two(1, 110, 100, "S2_Two(1, 110, 100,cmt).putRecord(wtr); trace: 2 1 a "+Long.toHexString(100)).putRecord(wtr);
            new S2_Two(1, 111, 100, "S2_Two(1, 111, 100,cmt).putRecord(wtr); trace: 2 1 b "+Long.toHexString(100)).putRecord(wtr);
            new S2_Two(1, 112, 100, "S2_Two(1, 112, 100,cmt).putRecord(wtr); trace: 2 1 c "+Long.toHexString(100)).putRecord(wtr);

            new Comment("S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.Delta, 50); ! 2 2 2 "+Long.toHexString(50)).putRecord(wtr);
            S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.Delta, 50);
            new S2_Two(1, 50, 100, "S2_Two(1, 50, 100,cmt).putRecord(wtr); trace: 2 1 0 "+Long.toHexString(100)).putRecord(wtr);
            new S2_Two(1, 55, 100, "S2_Two(1, 55, 100,cmt).putRecord(wtr); trace: 2 1 5 "+Long.toHexString(100)).putRecord(wtr);
            new S2_Two(1, 55, 100, "S2_Two(1, 55, 100,cmt).putRecord(wtr); trace: 2 1 0 "+Long.toHexString(100)).putRecord(wtr);
            new S2_Two(1, 75, 100, "S2_Two(1, 75, 100,cmt).putRecord(wtr); trace: 2 1 14 "+Long.toHexString(100)).putRecord(wtr);
            new S2_Two(1, 65, 100, "S2_Two(1, 65, 100,cmt).putRecord(wtr); trace: 2 1 -a "+Long.toHexString(100)).putRecord(wtr);

            new Comment("S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.Stride, 10, 20); trace: ! 2 2 6 a "+Long.toHexString(20)).putRecord(wtr);
            S2_Two.putMetadata(wtr, S2_Two.A2, MetaTFConstants.Stride, 10, 20);
            new S2_Two(1, 2, 3, "S2_Two(1, 2, 3,cmt).putRecord(wtr); trace: 2 1 3").putRecord(wtr);
            new S2_Two(1, 2, 3, "S2_Two(1, 2, 3,cmt).putRecord(wtr); trace: 2 1 3").putRecord(wtr);
            new S2_Two(1, 2, 3, "S2_Two(1, 2, 3,cmt).putRecord(wtr); trace: 2 1 3").putRecord(wtr);
            new S2_Two(1, 2, 3, "S2_Two(1, 2, 3,cmt).putRecord(wtr); trace: 2 1 3").putRecord(wtr);

            S2_Three[] threes = new S2_Three[20];
            for(int i=0; i<20; i++) {
                threes[i] = new S2_Three(0, new Index(ones[i], i), "S2_Three(0, new Index(ones["+i+"], "+i+"), cmt).putRecord(wtr); trace: 3 0 "+Integer.toHexString(i));
                threes[i].putRecord(wtr);
            }
        
            new S2_Two(1, 2, 3, "S2_Two(1, 2, 3, cmt).putRecord(wtr); trace: 2 1 3").putRecord(wtr);
            new S2_Two(5, 6, 4, "S2_Two(5, 6, 4, cmt).putRecord(wtr); trace: 2 5 4").putRecord(wtr);
            new Comment("S2.putMetadata(wtr, Test.ALL_A, MetaTFConstants.Default, 5); trace: ! * 2 1 5").putRecord(wtr);
            S2.putMetadata(wtr, Test.ALL_A, MetaTFConstants.Default, 5);
            new S2_Two(1, 2, 3, "S2_Two(1, 2, 3, cmt).putRecord(wtr); trace: 2 3").putRecord(wtr);
            new S2_Two(5, 6, 4, "S2_Two(5, 6, 4, cmt).putRecord(wtr); trace: 2 4").putRecord(wtr);

            wtr.flush();
            wtr.close();


            // Need to try reading this data back

            LowLevelReader rdr = new AsciiReader(new FileInputStream("test.trace"));
            Test.init();
            int i = 0;
            do {
                try {
                    Record r = Test.getNextRecord(rdr);
                    
                    if(r instanceof Comment) {
                        System.out.println("Comment: "+((Comment)r).text);
                    }
                    else if(r instanceof S1_One) {
                        System.out.println("S1_One: "+((S1_One)r).desc);
                    }
                    else if(r instanceof S2_Two) {
                        S2_Two r_ = (S2_Two)r;
                        System.out.println("S2_Two: "+r_.a+" "+r_.a2+" "+r_.b+" "+r_.cmt);
                    }
                    else if(r instanceof S2_Three) {
                        S2_Three r_ = (S2_Three)r;
                        System.out.println("S2_Three: "+r_.a+" ("+((S1_One)r_.b.record).desc+", "+r_.b.index+") "+r_.cmt);
                    }
                    i++;
                } catch (BadRecordException e) {
                    System.err.println(">>>"+e.getMessage()+" at record "+i);
                } catch (IOException e) {
                    System.err.println(i+" records read");
                    System.exit(0);
                }
            } while(true);


        } catch(BadRecordException e) {
            System.err.println(e.getMessage());
        } catch(IOException e) {
            System.err.println(e.getMessage());
        }
    }
}



