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