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