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