MAJOR: complete rewrite of edu.berkeley.sbp.meta
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.misc;
4 import java.io.*;
5 import java.util.*;
6 import java.lang.reflect.*;
7 import edu.berkeley.sbp.*;
8 import edu.berkeley.sbp.misc.*;
9 import edu.berkeley.sbp.meta.*;
10 import edu.berkeley.sbp.bind.*;
11 import edu.berkeley.sbp.tib.*;
12 import edu.berkeley.sbp.chr.*;
13 import edu.berkeley.sbp.util.*;
14
15 public class RegressionTests {
16
17     public static boolean yes = false;
18     public static boolean graph = false;
19
20     public static void main() throws Exception {
21       main(new String[] { "tests/meta.g", "tests/testcase.g", "tests/regression.tc" });
22     }
23
24     public static void main(String[] s) throws Exception {
25         try {
26             boolean profile = false;
27             if (s[0].equals("-graph")) {
28                 graph = true;
29                 String[] s2 = new String[s.length-1];
30                 System.arraycopy(s, 1, s2, 0, s2.length);
31                 s = s2;
32             }
33             if (s[0].equals("-profile")) {
34                 profile = true;
35                 String[] s2 = new String[s.length-1];
36                 System.arraycopy(s, 1, s2, 0, s2.length);
37                 s = s2;
38             }
39
40             System.err.println("parsing " + s[0]);
41             Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(s[0])).expand1();
42             Union meta = Grammar.create(res, "s", new MetaGrammarBindings());
43
44             System.err.println("parsing " + s[1]);
45             //SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
46             res = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
47
48             Union testcasegrammar = Grammar.create(res, "ts", new TestCaseMaker());
49             if (testcasegrammar==null) return;
50             CharParser parser = new CharParser(testcasegrammar);
51
52             if (profile) {
53                 System.out.println("\nready...");
54                 System.in.read();
55             }
56             System.gc();
57             long now = System.currentTimeMillis();
58             System.err.println("parsing " + s[2]);
59             Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
60             System.out.println();
61             System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
62             if (profile) {
63                 System.out.println("\ndone");
64                 System.in.read();
65                 System.exit(0);
66             }
67             System.err.println("expanding...");
68
69             Tree t = r2.expand1();
70             TestCase[] expanded = (TestCase[])((Functor)t.head()).invoke(t);
71             System.err.println("executing...");
72             for(TestCase tc : expanded) {
73                 tc.execute();
74                 /*
75                 String st = "a";
76                 for(int i=0; i<12; i++) {
77                     //System.out.println("length " + st.length());
78                     tc.input = st;
79                     long nowy = System.currentTimeMillis();
80                     GSS.shifts = 0;
81                     GSS.reductions = 0;
82                     tc.execute();
83                     System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
84                     st = st+st;
85                 }
86                 */
87             }
88
89         } catch (Throwable t) {
90             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
91             System.err.println(t);
92             System.err.println();
93             t.printStackTrace();
94             System.err.println();
95         }
96     }
97
98     public static class TestCaseMaker extends AnnotationGrammarBindings {
99         public TestCaseMaker() {
100             super(TestCaseMakerHelper.class);
101             add(MetaGrammarBindings.class, "grammar");
102             //add(MetaGrammarBindings.class, "");
103         }
104         public static class TestCaseMakerHelper {
105             public static @bind.as("grammaro") @bind.raw Object grammaro(Iterable<Tree> t) {
106                 return Grammar.create(t.iterator().next(), "s", new Grammar.Bindings());
107             }
108             //public static @bind.as("tca")           Object tca(Object[] o) throws IOException {
109             //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
110             public static @bind.as("tca")           Object tca(String input, String[] output, Union u) throws IOException {
111                 return new TestCase(input, output, u, false, false); }
112             public static @bind.as("tcb")           Object tca(String input, Union u) throws IOException {
113                 return new TestCase(input, new String[0], u, false, false); }
114             public static @bind.as("ts") TestCase[] go(TestCase[] cases) { return cases; }
115             public static @bind.as("o") Object o(Object[] s) { return s; }
116         }
117     }
118
119     public static class TestCase {
120         private final boolean tib;
121         private final boolean jav;
122         public /*final*/ String input;        
123         public final String[] output;
124         public final Union grammar;
125
126         public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
127             this.tib = tib;
128             this.jav = jav;
129             this.input = input;
130             this.output = output==null ? new String[0] : output;
131             this.grammar = grammar;
132         }
133         public String toString() {
134             String ret = "testcase {\n" + "  input \""+input+"\";\n";
135             for(String s : output) ret += "  output \""+s+"\";\n";
136             ret += grammar +"\n}\n";
137             return ret;
138         }
139         public boolean execute() throws Exception {
140             Forest<String> res = null;
141             ParseFailed pfe = null;
142             CharParser parser = new CharParser(grammar);
143             //parser.helpgc = false;
144             try {
145                 res = tib 
146                     ? /*new CharParser(grammar).parse(new Tib(input))*/ null
147                 : parser.parse(new StringReader(input));
148             } catch (ParseFailed pf) {
149                 pfe = pf;
150             }
151             //ystem.out.println("res=="+res);
152
153             if (graph) {
154                 FileOutputStream fos = new FileOutputStream("out.dot");
155                 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
156                 GraphViz gv = new GraphViz();
157                 res.toGraphViz(gv);
158                 gv.dump(p);
159                 p.flush();
160                 p.close();
161                 System.out.println(parser);
162             }
163
164             HashSet<Tree<String>> results = new HashSet<Tree<String>>();
165             if (res != null) res.expand(results);
166
167             System.out.print("\r");
168             if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
169                 System.out.print("\033[31m");
170                 System.out.println("PARSE FAILED");
171                 System.out.print("\033[0m");
172                 if (pfe != null) pfe.printStackTrace();
173             } else {
174                 System.out.print("\r                                                                                                              \r");
175             }
176             HashSet<String> outs = new HashSet<String>();
177             if (output != null) for(String s : output) outs.add(s.trim());
178             boolean bad = false;
179             for (Tree<String> r : results) {
180                 String s = r.toString().trim();
181                 if (outs.contains(s)) { outs.remove(s); continue; }
182                 if (!bad) System.out.println(input);
183                 System.out.print("\033[33m");
184                 System.out.println("     GOT: " + s);
185                 bad = true;
186             }
187             for(String s : outs) {
188                 if (!bad) System.out.println(input);
189                 System.out.print("\033[31m");
190                 System.out.println("EXPECTED: " + s);
191                 bad = true;
192             }
193             if (bad) {
194                 System.out.println("\033[0m");
195                 return true;
196             }             
197             System.out.println("\r\033[32mPASS\033[0m                                                                              ");
198
199             return false;
200         }
201     }
202
203     public static class TestCaseBuilder extends StringWalker {
204         public Object walk(Tree<String> tree) {
205             try {
206                 if ("grammaro".equals(tree.head())) return Grammar.create(tree, "s", new MetaGrammarBindings());
207                 else if ("output".equals(tree.head())) return string(tree.children());
208                 else if ("input".equals(tree.head())) return string(tree.children());
209                 else if ("testcase".equals(tree.head())) {
210                     String input = string(tree.child(0));
211                     String[] output = tree.size()>2 ? ((String[])walk(tree, 1)) : new String[0];
212                     Union grammar = Grammar.create(tree.child(tree.size()-1), "s", new MetaGrammarBindings());
213                     TestCase tc = new TestCase(input, output, grammar, false, false);
214                     return tc;
215                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
216                 else if (tree.head() == null) {
217                     Object[] ret = new Object[tree.size()];
218                     for(int i=0; i<ret.length; i++)
219                         ret[i] = walk(tree.child(i));
220                     return Reflection.lub(ret);
221                 }
222                 return super.walk(tree);
223             } catch (Exception e) {
224                 throw new Error(e);
225             }
226         }
227     }
228     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
229     public static String string(Tree<String> tree) {
230         String ret = "";
231         if (tree.head()!=null) ret += tree.head();
232         ret += string(tree.children());
233         return ret;
234     }
235     public static String string(Iterable<Tree<String>> children) {
236         String ret = "";
237         for(Tree<String> t : children) ret += string(t);
238         return ret;
239     }
240 }