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