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 import edu.berkeley.sbp.chr.*;
8
9 public class RegressionTests {
10
11     public static boolean yes = false;
12
13     public static void main(String[] s) throws Exception {
14         try {
15             boolean profile = false;
16             if (s[0].equals("-profile")) {
17                 profile = true;
18                 String[] s2 = new String[s.length-1];
19                 System.arraycopy(s, 1, s2, 0, s2.length);
20                 s = s2;
21             }
22
23             //MetaGrammar mg0 = new MetaGrammar();
24             //mg0.walk(MetaGrammar.meta);
25             //System.out.println(mg0);
26             Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
27             MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res);
28             //System.out.println(mg);
29             Union meta = mg.done();
30             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
31             res = new CharParser(meta).parse(sis).expand1();
32             Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
33             if (testcasegrammar==null) return;
34             CharParser parser = new CharParser(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(new FileInputStream(s[2]));
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 boolean tib;
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.tib = tib;
68             this.input = input;
69             this.output = output;
70             this.grammar = grammar;
71         }
72         public String toString() {
73             String ret = "testcase {\n" + "  input \""+input+"\";\n";
74             for(String s : output) ret += "  output \""+s+"\";\n";
75             ret += grammar +"\n}\n";
76             return ret;
77         }
78         public boolean execute() throws Exception {
79             Forest<String> res = null;
80             ParseFailed pfe = null;
81             try {
82                 res = tib 
83                     ? /*new CharParser(grammar).parse(new Tib(input))*/ null
84                 : new CharParser(grammar).parse(new StringReader(input));
85             } catch (ParseFailed pf) {
86                 pfe = pf;
87             }
88             //ystem.out.println("res=="+res);
89             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
90             System.out.print("\r");
91             if (results.size() == 0 && output.length > 0) {
92                 System.out.print("\033[31m");
93                 System.out.println("PARSE FAILED");
94                 System.out.print("\033[0m");
95                 if (pfe != null) pfe.printStackTrace();
96             } else {
97                 System.out.print("\r                                                                                                              \r");
98             }
99             HashSet<String> outs = new HashSet<String>();
100             if (output != null) 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                 if (!bad) System.out.println(input);
106                 System.out.print("\033[33m");
107                 System.out.println("     GOT: " + s);
108                 bad = true;
109             }
110             for(String s : outs) {
111                 if (!bad) System.out.println(input);
112                 System.out.print("\033[31m");
113                 System.out.println("EXPECTED: " + s);
114                 bad = true;
115             }
116             if (bad) {
117                 System.out.println("\033[0m");
118                 return true;
119             }             
120             System.out.println("\r\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                     boolean tib = "tibcase".equals(tree.head());
135                     MetaGrammar gram = tib ? new Tib.Grammar() : new MetaGrammar();
136                     Union grammar = (Union)((MetaGrammar)(gram.walk(tree, tree.numChildren()-1))).done("s");
137                     return new TestCase(input, output, grammar, tib);
138                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
139                 else return super.walk(tree);
140             } catch (Exception e) {
141                 throw new Error(e);
142             }
143         }
144     }
145
146     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
147 }
148