1 package edu.berkeley.sbp.misc;
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.*;
11 public class RegressionTests {
13 public static boolean yes = false;
15 public static void main(String[] s) throws Exception {
17 boolean profile = false;
18 if (s[0].equals("-profile")) {
20 String[] s2 = new String[s.length-1];
21 System.arraycopy(s, 1, s2, 0, s2.length);
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);
40 System.out.println("\nready...");
44 long now = System.currentTimeMillis();
45 System.err.println("parsing " + s[2]);
46 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
48 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
50 System.out.println("\ndone");
54 System.err.println("expanding...");
55 GraphViz gv = new GraphViz();
57 FileOutputStream fox = new FileOutputStream("out.dot");
61 TestCase[] expanded = (TestCase[])new TestCaseBuilder().walk(r2.expand1());
62 System.err.println("executing...");
63 for(TestCase tc : expanded) tc.execute();
65 } catch (Throwable t) {
66 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
67 System.err.println(t);
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 {
85 this.grammar = grammar;
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";
93 public boolean execute() throws Exception {
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();
105 Forest<String> res = null;
106 ParseFailed pfe = null;
109 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
110 : new CharParser(grammar).parse(new StringReader(input));
111 } catch (ParseFailed pf) {
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();
123 System.out.print("\r \r");
125 HashSet<String> outs = new HashSet<String>();
126 if (output != null) for(String s : output) outs.add(s.trim());
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);
136 for(String s : outs) {
137 if (!bad) System.out.println(input);
138 System.out.print("\033[31m");
139 System.out.println("EXPECTED: " + s);
143 System.out.println("\033[0m");
146 System.out.println("\r\033[32mPASS\033[0m ");
151 public static class TestCaseBuilder extends StringWalker {
152 public Object walk(Tree<String> tree) {
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);
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);
170 return super.walk(tree);
171 } catch (Exception e) {
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); }
181 private static class FieldMark {
182 public final String field;
183 public FieldMark(String field) { this.field = field; }
184 public String toString() { return "."+field; }
186 private static class ClassMark {
187 public final String clazz;
188 public ClassMark(String clazz) { this.clazz = clazz; }
189 public String toString() { return clazz+"$"; }
192 public static Object build(Tree<Object> t, Class c) throws Exception {
193 System.out.println("** build " + c.getName() + " " + t.toPrettyString());
195 if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
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);
202 throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
204 return Reflection.rebuild(o, c);
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;
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()));
237 System.out.println("returning a " + o.getClass().getName());
240 public static Object build(Tree<Object> t) throws Exception {
242 if (h instanceof ClassMark) {
243 ClassMark cm = (ClassMark)h;
244 Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
248 return h==null ? null : h.toString();
251 public static interface Expr {
252 public static Class[] subclasses = new Class[] { _plus_.class, num.class };
254 public static class _plus_ implements Expr {
257 public String toString() { return left + "+" + right; }
259 public static class num implements Expr {
261 public String toString() { return "["+val+"]"; }
266 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }