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