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