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;
14 public static boolean graph = false;
16 public static void main(String[] s) throws Exception {
18 boolean profile = false;
19 if (s[0].equals("-graph")) {
21 String[] s2 = new String[s.length-1];
22 System.arraycopy(s, 1, s2, 0, s2.length);
25 if (s[0].equals("-profile")) {
27 String[] s2 = new String[s.length-1];
28 System.arraycopy(s, 1, s2, 0, s2.length);
32 System.err.println("parsing " + s[0]);
33 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
34 Union meta = MetaGrammar.make(res, "s");
35 System.err.println("parsing " + s[1]);
36 SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
37 res = new CharParser(meta).parse(sis).expand1();
38 Union testcasegrammar = MetaGrammar.make(res, "ts");
39 if (testcasegrammar==null) return;
40 CharParser parser = new CharParser(testcasegrammar);
43 System.out.println("\nready...");
47 long now = System.currentTimeMillis();
48 System.err.println("parsing " + s[2]);
49 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
51 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
53 System.out.println("\ndone");
57 System.err.println("expanding...");
59 TestCase[] expanded = (TestCase[])new TestCaseBuilder().walk(r2.expand1());
60 System.err.println("executing...");
61 for(TestCase tc : expanded) {
65 for(int i=0; i<12; i++) {
66 //System.out.println("length " + st.length());
68 long nowy = System.currentTimeMillis();
72 System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
78 } catch (Throwable t) {
79 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
80 System.err.println(t);
87 public static class TestCase {
88 private final boolean tib;
89 private final boolean jav;
90 public /*final*/ String input;
91 public final String[] output;
92 public final Union grammar;
93 public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
98 this.grammar = grammar;
100 public String toString() {
101 String ret = "testcase {\n" + " input \""+input+"\";\n";
102 for(String s : output) ret += " output \""+s+"\";\n";
103 ret += grammar +"\n}\n";
106 public boolean execute() throws Exception {
107 Forest<String> res = null;
108 ParseFailed pfe = null;
109 CharParser parser = new CharParser(grammar);
110 //parser.helpgc = false;
113 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
114 : parser.parse(new StringReader(input));
115 } catch (ParseFailed pf) {
118 //ystem.out.println("res=="+res);
121 FileOutputStream fos = new FileOutputStream("out.dot");
122 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
123 GraphViz gv = new GraphViz();
128 System.out.println(parser);
130 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
131 System.out.print("\r");
132 if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
133 System.out.print("\033[31m");
134 System.out.println("PARSE FAILED");
135 System.out.print("\033[0m");
136 if (pfe != null) pfe.printStackTrace();
138 System.out.print("\r \r");
140 HashSet<String> outs = new HashSet<String>();
141 if (output != null) for(String s : output) outs.add(s.trim());
143 for (Tree<String> r : results) {
144 String s = r.toString().trim();
145 if (outs.contains(s)) { outs.remove(s); continue; }
146 if (!bad) System.out.println(input);
147 System.out.print("\033[33m");
148 System.out.println(" GOT: " + s);
151 for(String s : outs) {
152 if (!bad) System.out.println(input);
153 System.out.print("\033[31m");
154 System.out.println("EXPECTED: " + s);
158 System.out.println("\033[0m");
161 System.out.println("\r\033[32mPASS\033[0m ");
167 public static class TestCaseBuilder extends StringWalker {
168 public Object walk(Tree<String> tree) {
170 if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
171 else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
172 else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
173 else if ("testcase".equals(tree.head())) {
174 String input = MetaGrammar.string(tree.child(0));
175 String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
176 Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
177 TestCase tc = new TestCase(input, output, grammar, false, false);
179 } else if ("ts".equals(tree.head())) return walk(tree, 0);
180 else if (tree.head() == null) {
181 Object[] ret = new Object[tree.numChildren()];
182 for(int i=0; i<ret.length; i++)
183 ret[i] = walk(tree.child(i));
184 return Reflection.lub(ret);
186 return super.walk(tree);
187 } catch (Exception e) {
192 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }