353866d3d6ae8c0f51b9064bc438a00afe305353
[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 java.lang.reflect.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.tib.*;
8 import edu.berkeley.sbp.chr.*;
9 import edu.berkeley.sbp.util.*;
10
11 public class RegressionTests {
12
13     public static boolean yes = false;
14
15     public static void main(String[] s) throws Exception {
16         try {
17             boolean profile = false;
18             if (s[0].equals("-profile")) {
19                 profile = true;
20                 String[] s2 = new String[s.length-1];
21                 System.arraycopy(s, 1, s2, 0, s2.length);
22                 s = s2;
23             }
24
25             //MetaGrammar mg0 = new MetaGrammar();
26             //mg0.walk(MetaGrammar.meta);
27             //System.out.println(mg0);
28             System.err.println("parsing " + s[0]);
29             Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
30             //System.out.println(mg);
31             Union meta = MetaGrammar.make();
32             System.err.println("parsing " + s[1]);
33             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
34             res = new CharParser(meta).parse(sis).expand1();
35             Union testcasegrammar = MetaGrammar.make(res, "ts");
36             if (testcasegrammar==null) return;
37             CharParser parser = new CharParser(testcasegrammar);
38
39             if (profile) {
40                 System.out.println("\nready...");
41                 System.in.read();
42             }
43             System.gc();
44             long now = System.currentTimeMillis();
45             System.err.println("parsing " + s[2]);
46             Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
47             System.out.println();
48             System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
49             if (profile) {
50                 System.out.println("\ndone");
51                 System.in.read();
52                 System.exit(0);
53             }
54             System.err.println("expanding...");
55             GraphViz gv = new GraphViz();
56             r2.toGraphViz(gv);
57             FileOutputStream fox = new FileOutputStream("out.dot");
58             gv.dump(fox);
59             fox.close();
60
61             TestCase[] expanded = (TestCase[])new TestCaseBuilder().walk(r2.expand1());
62             System.err.println("executing...");
63             for(TestCase tc : expanded) tc.execute();
64
65         } catch (Throwable t) {
66             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
67             System.err.println(t);
68             System.err.println();
69             t.printStackTrace();
70             System.err.println();
71         }
72     }
73
74     public static class TestCase {
75         private final boolean tib;
76         private final boolean jav;
77         public final String input;        
78         public final String[] output;
79         public final Union grammar;
80         public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
81             this.tib = tib;
82             this.jav = jav;
83             this.input = input;
84             this.output = output;
85             this.grammar = grammar;
86         }
87         public String toString() {
88             String ret = "testcase {\n" + "  input \""+input+"\";\n";
89             for(String s : output) ret += "  output \""+s+"\";\n";
90             ret += grammar +"\n}\n";
91             return ret;
92         }
93         public boolean execute() throws Exception {
94             if (jav) {
95                 Forest<String> tree = new CharParser(grammar).parse(new StringReader(input));
96                 FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
97                 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
98                 GraphViz gv = new GraphViz();
99                 tree.toGraphViz(gv);
100                 gv.dump(p);
101                 p.flush();
102                 p.close();
103                 return true;
104             }
105             Forest<String> res = null;
106             ParseFailed pfe = null;
107             try {
108                 res = tib 
109                     ? /*new CharParser(grammar).parse(new Tib(input))*/ null
110                 : new CharParser(grammar).parse(new StringReader(input));
111             } catch (ParseFailed pf) {
112                 pfe = pf;
113             }
114             //ystem.out.println("res=="+res);
115             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
116             System.out.print("\r");
117             if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
118                 System.out.print("\033[31m");
119                 System.out.println("PARSE FAILED");
120                 System.out.print("\033[0m");
121                 if (pfe != null) pfe.printStackTrace();
122             } else {
123                 System.out.print("\r                                                                                                              \r");
124             }
125             HashSet<String> outs = new HashSet<String>();
126             if (output != null) for(String s : output) outs.add(s.trim());
127             boolean bad = false;
128             for (Tree<String> r : results) {
129                 String s = r.toString().trim();
130                 if (outs.contains(s)) { outs.remove(s); continue; }
131                 if (!bad) System.out.println(input);
132                 System.out.print("\033[33m");
133                 System.out.println("     GOT: " + s);
134                 bad = true;
135             }
136             for(String s : outs) {
137                 if (!bad) System.out.println(input);
138                 System.out.print("\033[31m");
139                 System.out.println("EXPECTED: " + s);
140                 bad = true;
141             }
142             if (bad) {
143                 System.out.println("\033[0m");
144                 return true;
145             }             
146             System.out.println("\r\033[32mPASS\033[0m                                                                              ");
147             return false;
148         }
149     }
150
151     public static class TestCaseBuilder extends StringWalker {
152         public Object walk(Tree<String> tree) {
153             try {
154                 if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
155                 else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
156                 else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
157                 else if ("testcase".equals(tree.head())) {
158                     String input = MetaGrammar.string(tree.child(0));
159                     String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
160                     Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
161                     TestCase tc = new TestCase(input, output, grammar, false, false);
162                     return tc;
163                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
164                 else if (tree.head() == null) {
165                     Object[] ret = new Object[tree.numChildren()];
166                     for(int i=0; i<ret.length; i++)
167                         ret[i] = walk(tree.child(i));
168                     return Reflection.lub(ret);
169                 }
170                 return super.walk(tree);
171             } catch (Exception e) {
172                 throw new Error(e);
173             }
174         }
175     }
176     /*
177     public static class JavaGrammar extends MetaGrammar {
178         public Object convertLabel(String label) { return new ClassMark(label); }
179         public Object convertFieldLabel(String label) { return new FieldMark(label); }
180
181         private static class FieldMark {
182             public final String field;
183             public FieldMark(String field) { this.field = field; }
184             public String toString() { return "."+field; }
185         }
186         private static class ClassMark {
187             public final String clazz;
188             public ClassMark(String clazz) { this.clazz = clazz; }
189             public String toString() { return clazz+"$"; }
190         }
191
192         public static Object build(Tree<Object> t, Class c) throws Exception {
193             System.out.println("** build " + c.getName() + " " + t.toPrettyString());
194             Object h = t.head();
195             if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
196             Object o = null;
197             if (t.numChildren()==0) o = t.head();
198             else if (t.head()==null) return build2(t, c);
199             else if (t.head() instanceof FieldMark) {
200                 return build2(new Tree(null, new Tree[] { t }), c);
201             } else {
202                 throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
203             }
204             return Reflection.rebuild(o, c);
205         }
206         private static Object build2(Tree<Object> t, Class c) throws Exception {
207             System.out.println("** build2 " + c.getName() + " " + t.toPrettyString());
208             if (!Reflection.isConcrete(c)) {
209                 Field f = c.getField("subclasses");
210                 if (f==null) throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
211                 Class[] subs = (Class[])f.get(null);
212                 OUTER: for(int i=0; i<subs.length; i++) {
213                     for(Tree<Object> t2 : t) {
214                         Object o2 = t2.head();
215                         System.out.println("checking  " + o2 + " in " + subs[i].getName());
216                         if (o2 instanceof FieldMark) {
217                             if (subs[i].getField(((FieldMark)o2).field)==null) continue OUTER;
218                         }
219                     }
220                     c = subs[i];
221                     break;
222                 }
223             }
224             Object o = c.newInstance();
225             for(Tree<Object> t2 : t) {
226                 Object o2 = t2.head();
227                 if (o2 instanceof FieldMark) {
228                     FieldMark f = (FieldMark)o2;
229                     Field field = c.getField(ReflectiveWalker.mangle(f.field));
230                     Object tgt = build(t2.child(0), field.getType());
231                     System.out.println("setting " + f.field +
232                                        " on a " + o.getClass().getName() +
233                                        " to " + (tgt==null?"null":tgt.getClass().getName()));
234                     field.set(o, tgt);
235                 }
236             }
237             System.out.println("returning a " + o.getClass().getName());
238             return o;
239         }
240         public static Object build(Tree<Object> t) throws Exception {
241             Object h = t.head();
242             if (h instanceof ClassMark) {
243                 ClassMark cm = (ClassMark)h;
244                 Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
245                 return build2(t, c);
246             }
247             
248             return h==null ? null : h.toString();
249         }
250         
251         public static interface Expr {
252             public static Class[] subclasses = new Class[] { _plus_.class, num.class };
253         }
254         public static class _plus_ implements Expr {
255             public Expr left;
256             public Expr right;
257             public String toString() { return left + "+" + right; }
258         }
259         public static class num implements Expr {
260             public String val;
261             public String toString() { return "["+val+"]"; }
262         }
263
264     }
265     */
266     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
267 }
268