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.meta.*;
8 import edu.berkeley.sbp.bind.*;
9 import edu.berkeley.sbp.tib.*;
10 import edu.berkeley.sbp.chr.*;
11 import edu.berkeley.sbp.util.*;
12 import static edu.berkeley.sbp.meta.MetaGrammar.*;
14 public class RegressionTests {
16 public static boolean yes = false;
17 public static boolean graph = false;
19 public static void main(String[] s) throws Exception {
21 boolean profile = false;
22 if (s[0].equals("-graph")) {
24 String[] s2 = new String[s.length-1];
25 System.arraycopy(s, 1, s2, 0, s2.length);
28 if (s[0].equals("-profile")) {
30 String[] s2 = new String[s.length-1];
31 System.arraycopy(s, 1, s2, 0, s2.length);
35 System.err.println("parsing " + s[0]);
36 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
37 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(new FileInputStream(s[1])).expand1();
43 Union testcasegrammar = MetaGrammar.make(res, "ts", new TestCaseMaker());
44 if (testcasegrammar==null) return;
45 CharParser parser = new CharParser(testcasegrammar);
48 System.out.println("\nready...");
52 long now = System.currentTimeMillis();
53 System.err.println("parsing " + s[2]);
54 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
56 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
58 System.out.println("\ndone");
62 System.err.println("expanding...");
64 Tree t = r2.expand1();
65 TestCase[] expanded = (TestCase[])((Functor)t.head()).invoke(t);
66 System.err.println("executing...");
67 for(TestCase tc : expanded) {
71 for(int i=0; i<12; i++) {
72 //System.out.println("length " + st.length());
74 long nowy = System.currentTimeMillis();
78 System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
84 } catch (Throwable t) {
85 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
86 System.err.println(t);
93 public static class TestCaseMaker extends AnnotationGrammarBindingResolver {
94 public TestCaseMaker() {
95 super(TestCaseMakerHelper.class);
96 //add(MetaGrammarBindings.class, "grammar.");
97 add(MetaGrammarBindings.class, "");
99 public static class TestCaseMakerHelper {
100 public static @bind.as("grammaro") @bind.raw Object grammaro(Iterable<Tree> t) {
101 return MetaGrammar.make(t.iterator().next(), "s", new TaggingGrammarBindingResolver());
103 //public static @bind.as("tca") Object tca(Object[] o) throws IOException {
104 //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
105 public static @bind.as("tca") Object tca(String input, String[] output, Union u) throws IOException {
106 return new TestCase(input, output, u, false, false); }
107 public static @bind.as("tcb") Object tca(String input, Union u) throws IOException {
108 return new TestCase(input, new String[0], u, false, false); }
109 public static @bind.as("ts") TestCase[] go(TestCase[] cases) { return cases; }
110 public static @bind.as("o") Object o(Object[] s) { return s; }
114 public static class TestCase {
115 private final boolean tib;
116 private final boolean jav;
117 public /*final*/ String input;
118 public final String[] output;
119 public final Union grammar;
121 public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
125 this.output = output==null ? new String[0] : output;
126 this.grammar = grammar;
128 public String toString() {
129 String ret = "testcase {\n" + " input \""+input+"\";\n";
130 for(String s : output) ret += " output \""+s+"\";\n";
131 ret += grammar +"\n}\n";
134 public boolean execute() throws Exception {
135 Forest<String> res = null;
136 ParseFailed pfe = null;
137 CharParser parser = new CharParser(grammar);
138 //parser.helpgc = false;
141 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
142 : parser.parse(new StringReader(input));
143 } catch (ParseFailed pf) {
146 //ystem.out.println("res=="+res);
149 FileOutputStream fos = new FileOutputStream("out.dot");
150 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
151 GraphViz gv = new GraphViz();
156 System.out.println(parser);
158 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
159 System.out.print("\r");
160 if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
161 System.out.print("\033[31m");
162 System.out.println("PARSE FAILED");
163 System.out.print("\033[0m");
164 if (pfe != null) pfe.printStackTrace();
166 System.out.print("\r \r");
168 HashSet<String> outs = new HashSet<String>();
169 if (output != null) for(String s : output) outs.add(s.trim());
171 for (Tree<String> r : results) {
172 String s = r.toString().trim();
173 if (outs.contains(s)) { outs.remove(s); continue; }
174 if (!bad) System.out.println(input);
175 System.out.print("\033[33m");
176 System.out.println(" GOT: " + s);
179 for(String s : outs) {
180 if (!bad) System.out.println(input);
181 System.out.print("\033[31m");
182 System.out.println("EXPECTED: " + s);
186 System.out.println("\033[0m");
189 System.out.println("\r\033[32mPASS\033[0m ");
195 public static class TestCaseBuilder extends StringWalker {
196 public Object walk(Tree<String> tree) {
198 if ("grammaro".equals(tree.head())) return MetaGrammar.make(tree, "s");
199 else if ("output".equals(tree.head())) return string(tree.children());
200 else if ("input".equals(tree.head())) return string(tree.children());
201 else if ("testcase".equals(tree.head())) {
202 String input = string(tree.child(0));
203 String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
204 Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
205 TestCase tc = new TestCase(input, output, grammar, false, false);
207 } else if ("ts".equals(tree.head())) return walk(tree, 0);
208 else if (tree.head() == null) {
209 Object[] ret = new Object[tree.numChildren()];
210 for(int i=0; i<ret.length; i++)
211 ret[i] = walk(tree.child(i));
212 return Reflection.lub(ret);
214 return super.walk(tree);
215 } catch (Exception e) {
220 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
221 public static String string(Tree<String> tree) {
223 if (tree.head()!=null) ret += tree.head();
224 ret += string(tree.children());
227 public static String string(Iterable<Tree<String>> children) {
229 for(Tree<String> t : children) ret += string(t);