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) throws IOException {
77             this.inp = tib
78                 ? new Tib(input)
79                 : new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":"");
80             this.input = input;
81             this.output = output;
82             this.grammar = grammar;
83         }
84         public String toString() {
85             String ret = "testcase {\n" + "  input \""+input+"\";\n";
86             for(String s : output) ret += "  output \""+s+"\";\n";
87             ret += grammar +"\n}\n";
88             return ret;
89         }
90         public boolean execute() throws Exception {
91             Forest<String> res = new Parser(grammar, CharToken.top()).parse(inp);
92             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
93             System.out.print("\r");
94             if (results.size() == 0 && output.length > 0) {
95                 System.out.print("\033[31m");
96                 System.out.println("PARSE FAILED");
97                 System.out.print("\033[0m");
98             } else {
99                 System.out.println("\r                                                                                                              \r");
100             }
101             HashSet<String> outs = new HashSet<String>();
102             if (output != null) for(String s : output) outs.add(s.trim());
103             boolean bad = false;
104             for (Tree<String> r : results) {
105                 String s = r.toString().trim();
106                 if (outs.contains(s)) { outs.remove(s); continue; }
107                 System.out.print("\033[33m");
108                 System.out.println("     GOT: " + s);
109                 bad = true;
110             }
111             for(String s : outs) {
112                 System.out.print("\033[31m");
113                 System.out.println("EXPECTED: " + s);
114                 bad = true;
115             }
116             if (bad) {
117                 System.out.print("\033[0m");
118                 return true;
119             }             
120             System.out.println("\033[32mPASS\033[0m");
121             return false;
122         }
123     }
124
125     public static class TestCaseBuilder extends MetaGrammar {
126         public Object walk(Tree<String> tree) {
127             try {
128                 if ("grammar".equals(tree.head())) { walkChildren(tree); return done("s"); }
129                 else if ("output".equals(tree.head())) return string(tree.children());
130                 else if ("input".equals(tree.head())) return string(tree.children());
131                 else if ("tibcase".equals(tree.head()) || "testcase".equals(tree.head())) {
132                     String input = string(tree.child(0));
133                     String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
134                     Union grammar = (Union)walk(tree, tree.numChildren()-1);
135                     return new TestCase(input, output, grammar, "tibcase".equals(tree.head()));
136                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
137                 else return super.walk(tree);
138             } catch (Exception e) {
139                 throw new Error(e);
140             }
141         }
142     }
143
144     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
145 }
146