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.*;
13 public class RegressionTests {
15 public static boolean yes = false;
16 public static boolean graph = false;
18 public static void main(String[] s) throws Exception {
20 boolean profile = false;
21 if (s[0].equals("-graph")) {
23 String[] s2 = new String[s.length-1];
24 System.arraycopy(s, 1, s2, 0, s2.length);
27 if (s[0].equals("-profile")) {
29 String[] s2 = new String[s.length-1];
30 System.arraycopy(s, 1, s2, 0, s2.length);
34 System.err.println("parsing " + s[0]);
35 Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(s[0])).expand1();
36 Union meta = Grammar.create(res, "s", new MetaGrammarBindings());
38 System.err.println("parsing " + s[1]);
39 //SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
40 res = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
42 Union testcasegrammar = Grammar.create(res, "ts", new TestCaseMaker());
43 if (testcasegrammar==null) return;
44 CharParser parser = new CharParser(testcasegrammar);
47 System.out.println("\nready...");
51 long now = System.currentTimeMillis();
52 System.err.println("parsing " + s[2]);
53 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
55 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
57 System.out.println("\ndone");
61 System.err.println("expanding...");
63 Tree t = r2.expand1();
64 TestCase[] expanded = (TestCase[])((Functor)t.head()).invoke(t);
65 System.err.println("executing...");
66 for(TestCase tc : expanded) {
70 for(int i=0; i<12; i++) {
71 //System.out.println("length " + st.length());
73 long nowy = System.currentTimeMillis();
77 System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
83 } catch (Throwable t) {
84 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
85 System.err.println(t);
92 public static class TestCaseMaker extends AnnotationGrammarBindings {
93 public TestCaseMaker() {
94 super(TestCaseMakerHelper.class);
95 add(MetaGrammarBindings.class, "grammar");
96 //add(MetaGrammarBindings.class, "");
98 public static class TestCaseMakerHelper {
99 public static @bind.as("grammaro") @bind.raw Object grammaro(Iterable<Tree> t) {
100 return Grammar.create(t.iterator().next(), "s", new Grammar.Bindings());
102 //public static @bind.as("tca") Object tca(Object[] o) throws IOException {
103 //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
104 public static @bind.as("tca") Object tca(String input, String[] output, Union u) throws IOException {
105 return new TestCase(input, output, u, false, false); }
106 public static @bind.as("tcb") Object tca(String input, Union u) throws IOException {
107 return new TestCase(input, new String[0], u, false, false); }
108 public static @bind.as("ts") TestCase[] go(TestCase[] cases) { return cases; }
109 public static @bind.as("o") Object o(Object[] s) { return s; }
113 public static class TestCase {
114 private final boolean tib;
115 private final boolean jav;
116 public /*final*/ String input;
117 public final String[] output;
118 public final Union grammar;
120 public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
124 this.output = output==null ? new String[0] : output;
125 this.grammar = grammar;
127 public String toString() {
128 String ret = "testcase {\n" + " input \""+input+"\";\n";
129 for(String s : output) ret += " output \""+s+"\";\n";
130 ret += grammar +"\n}\n";
133 public boolean execute() throws Exception {
134 Forest<String> res = null;
135 ParseFailed pfe = null;
136 CharParser parser = new CharParser(grammar);
137 //parser.helpgc = false;
140 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
141 : parser.parse(new StringReader(input));
142 } catch (ParseFailed pf) {
145 //ystem.out.println("res=="+res);
148 FileOutputStream fos = new FileOutputStream("out.dot");
149 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
150 GraphViz gv = new GraphViz();
155 System.out.println(parser);
158 HashSet<Tree<String>> results = new HashSet<Tree<String>>();
159 if (res != null) res.expand(results);
161 System.out.print("\r");
162 if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
163 System.out.print("\033[31m");
164 System.out.println("PARSE FAILED");
165 System.out.print("\033[0m");
166 if (pfe != null) pfe.printStackTrace();
168 System.out.print("\r \r");
170 HashSet<String> outs = new HashSet<String>();
171 if (output != null) for(String s : output) outs.add(s.trim());
173 for (Tree<String> r : results) {
174 String s = r.toString().trim();
175 if (outs.contains(s)) { outs.remove(s); continue; }
176 if (!bad) System.out.println(input);
177 System.out.print("\033[33m");
178 System.out.println(" GOT: " + s);
181 for(String s : outs) {
182 if (!bad) System.out.println(input);
183 System.out.print("\033[31m");
184 System.out.println("EXPECTED: " + s);
188 System.out.println("\033[0m");
191 System.out.println("\r\033[32mPASS\033[0m ");
197 public static class TestCaseBuilder extends StringWalker {
198 public Object walk(Tree<String> tree) {
200 if ("grammaro".equals(tree.head())) return Grammar.create(tree, "s", new MetaGrammarBindings());
201 else if ("output".equals(tree.head())) return string(tree.children());
202 else if ("input".equals(tree.head())) return string(tree.children());
203 else if ("testcase".equals(tree.head())) {
204 String input = string(tree.child(0));
205 String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
206 Union grammar = Grammar.create(tree.child(tree.numChildren()-1), "s", new MetaGrammarBindings());
207 TestCase tc = new TestCase(input, output, grammar, false, false);
209 } else if ("ts".equals(tree.head())) return walk(tree, 0);
210 else if (tree.head() == null) {
211 Object[] ret = new Object[tree.numChildren()];
212 for(int i=0; i<ret.length; i++)
213 ret[i] = walk(tree.child(i));
214 return Reflection.lub(ret);
216 return super.walk(tree);
217 } catch (Exception e) {
222 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
223 public static String string(Tree<String> tree) {
225 if (tree.head()!=null) ret += tree.head();
226 ret += string(tree.children());
229 public static String string(Iterable<Tree<String>> children) {
231 for(Tree<String> t : children) ret += string(t);