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