checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
1 package edu.berkeley.sbp.misc;
2 import java.io.*;
3 import java.util.*;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.tib.*;
7
8 public class RegressionTests {
9
10     public static boolean yes = false;
11     public static class MyWalker extends ReflectiveWalker {
12         public String top(Object[] o) { return "top("+join(o)+")"; }
13         public String str(String[] s) { String ret = ""; for(String st : s) ret += st; return ret; }
14         public String join(Object[] o) { String ret = ""; for(Object st : o) ret += st; return ret; }
15         public String whilex(Object s, Object y) { return "while("+s+") " + y; }
16         public String seq(Object[] statements) {
17             String ret = "";
18             for(Object s : statements) ret += s + ";\n";
19             return ret;
20         }
21         /*
22         public String bl(String s) { return "{" + s + "}"; }
23         */
24     };
25
26     public static void main(String[] s) throws Exception {
27         try {
28             boolean profile = false;
29             if (s[0].equals("-profile")) {
30                 profile = true;
31                 String[] s2 = new String[s.length-1];
32                 System.arraycopy(s, 1, s2, 0, s2.length);
33                 s = s2;
34             }
35
36             //MetaGrammar mg0 = new MetaGrammar();
37             //mg0.walk(MetaGrammar.meta);
38             //System.out.println(mg0);
39             Tree<String> res = new Parser(MetaGrammar.make(), CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(s[0]))));
40             MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res);
41             //System.out.println(mg);
42             Union meta = mg.done();
43             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
44             res = new Parser(meta, CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(sis), "parsing " + s[1] + " using " + s[0]));
45             Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
46             if (testcasegrammar==null) return;
47             CharToken.Stream cs = new CharToken.Stream(new InputStreamReader(new FileInputStream(s[2])), "parsing " + s[2] + " using " + s[1]);
48             Parser parser = new Parser(testcasegrammar, CharToken.top());
49
50             if (profile) {
51                 System.out.println("\nready...");
52                 System.in.read();
53             }
54             Forest<String> r2 = parser.parse(cs);
55             if (profile) {
56                 System.out.println("\ndone");
57                 System.in.read();
58                 System.exit(0);
59             }
60             for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
61
62         } catch (Throwable t) {
63             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
64             System.err.println(t);
65             System.err.println();
66             t.printStackTrace();
67             System.err.println();
68         }
69     }
70
71     public static class TestCase {
72         private final Token.Stream inp;
73         public final String input;
74         public final String[] output;
75         public final Union grammar;
76         public TestCase(String input, String[] output, Union grammar, boolean tib) {
77             this.inp = tib
78                 ? new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":"")
79                 /*: new TibCharToken.Stream(new StringReader(input))*/
80                 : new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":"")
81                 ;
82             this.input = input;
83             this.output = output;
84             this.grammar = grammar;
85         }
86         public String toString() {
87             String ret = "testcase {\n" + "  input \""+input+"\";\n";
88             for(String s : output) ret += "  output \""+s+"\";\n";
89             ret += grammar +"\n}\n";
90             return ret;
91         }
92         public boolean execute() throws Exception {
93             Forest<String> res = new Parser(grammar, CharToken.top()).parse(inp);
94             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
95             System.out.print("\r");
96             if (results.size() == 0 && output.length > 0) {
97                 System.out.print("\033[31m");
98                 System.out.println("PARSE FAILED");
99                 System.out.print("\033[0m");
100             } else {
101                 System.out.println("\r                                                                                                              \r");
102             }
103             HashSet<String> outs = new HashSet<String>();
104             if (output != null) for(String s : output) outs.add(s.trim());
105             boolean bad = false;
106             for (Tree<String> r : results) {
107                 String s = r.toString().trim();
108                 if (outs.contains(s)) { outs.remove(s); continue; }
109                 System.out.print("\033[33m");
110                 System.out.println("     GOT: " + s);
111                 bad = true;
112             }
113             for(String s : outs) {
114                 System.out.print("\033[31m");
115                 System.out.println("EXPECTED: " + s);
116                 bad = true;
117             }
118             if (bad) {
119                 System.out.print("\033[0m");
120                 return true;
121             }             
122             System.out.println("\033[32mPASS\033[0m");
123             return false;
124         }
125     }
126
127     public static class TestCaseBuilder extends MetaGrammar {
128         public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
129         public TestCase[] ts(TestCase[] ts) { return ts; }
130         public TestCase testcase(String input, String[] output, Union grammar) { return new TestCase(input, output,        grammar, false); }
131         public TestCase testcase(String input,                  Union grammar) { return new TestCase(input, new String[0], grammar, false); }
132         public TestCase  tibcase(String input,                  Union grammar) { return new TestCase(input, new String[0], grammar, false); }
133         public MetaGrammar grammar(Object[] o) { return this; }
134         public Object walk(String tag, Object[] args) {
135             if ("grammar".equals(tag)) return done("s");
136             else return super.walk(tag, args);
137         }
138     }
139
140     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
141 }
142