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 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);
38 System.out.println("\nready...");
42 long now = System.currentTimeMillis();
43 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
45 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
47 System.out.println("\ndone");
51 for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
53 } catch (Throwable t) {
54 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
55 System.err.println(t);
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 {
73 this.grammar = grammar;
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";
81 public boolean execute() throws Exception {
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();
93 Forest<String> res = null;
94 ParseFailed pfe = null;
97 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
98 : new CharParser(grammar).parse(new StringReader(input));
99 } catch (ParseFailed pf) {
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();
111 System.out.print("\r \r");
113 HashSet<String> outs = new HashSet<String>();
114 if (output != null) for(String s : output) outs.add(s.trim());
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);
124 for(String s : outs) {
125 if (!bad) System.out.println(input);
126 System.out.print("\033[31m");
127 System.out.println("EXPECTED: " + s);
131 System.out.println("\033[0m");
134 System.out.println("\r\033[32mPASS\033[0m ");
139 public static class TestCaseBuilder extends StringWalker {
140 public Object walk(Tree<String> tree) {
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) {
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); }
164 private static class FieldMark {
165 public final String field;
166 public FieldMark(String field) { this.field = field; }
167 public String toString() { return "."+field; }
169 private static class ClassMark {
170 public final String clazz;
171 public ClassMark(String clazz) { this.clazz = clazz; }
172 public String toString() { return clazz+"$"; }
175 public static Object build(Tree<Object> t, Class c) throws Exception {
176 System.out.println("** build " + c.getName() + " " + t.toPrettyString());
178 if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
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);
185 throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
187 return Reflection.rebuild(o, c);
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;
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()));
220 System.out.println("returning a " + o.getClass().getName());
223 public static Object build(Tree<Object> t) throws Exception {
225 if (h instanceof ClassMark) {
226 ClassMark cm = (ClassMark)h;
227 Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
231 return h==null ? null : h.toString();
234 public static interface Expr {
235 public static Class[] subclasses = new Class[] { _plus_.class, num.class };
237 public static class _plus_ implements Expr {
240 public String toString() { return left + "+" + right; }
242 public static class num implements Expr {
244 public String toString() { return "["+val+"]"; }
249 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }