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.*;
10 import static edu.berkeley.sbp.misc.Demo.*;
12 public class RegressionTests {
14 public static boolean yes = false;
15 public static boolean graph = false;
17 public static void main(String[] s) throws Exception {
19 boolean profile = false;
20 if (s[0].equals("-graph")) {
22 String[] s2 = new String[s.length-1];
23 System.arraycopy(s, 1, s2, 0, s2.length);
26 if (s[0].equals("-profile")) {
28 String[] s2 = new String[s.length-1];
29 System.arraycopy(s, 1, s2, 0, s2.length);
33 System.err.println("parsing " + s[0]);
34 Tree<String> res = new CharParser(Demo.make()).parse(new FileInputStream(s[0])).expand1();
35 Union meta = Demo.make(res, "s");
36 System.err.println("parsing " + s[1]);
37 SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
38 res = new CharParser(meta).parse(sis).expand1();
40 Union testcasegrammar = Demo.make(res, "ts", new TestCaseMaker());
41 if (testcasegrammar==null) return;
42 CharParser parser = new CharParser(testcasegrammar);
45 System.out.println("\nready...");
49 long now = System.currentTimeMillis();
50 System.err.println("parsing " + s[2]);
51 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
53 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
55 System.out.println("\ndone");
59 System.err.println("expanding...");
61 Tree t = r2.expand1();
62 TestCase[] expanded = (TestCase[])((Functor)t.head()).invoke(t.children());
63 System.err.println("executing...");
64 for(TestCase tc : expanded) {
68 for(int i=0; i<12; i++) {
69 //System.out.println("length " + st.length());
71 long nowy = System.currentTimeMillis();
75 System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
81 } catch (Throwable t) {
82 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
83 System.err.println(t);
90 public static class TestCaseMaker extends ReflectiveMeta {
91 public TestCaseMaker() {
92 super(TestCaseMakerHelper.class, new Class[] {
99 MG.NonTerminalReference.class,
100 MG.StringLiteral.class,
105 public static class TestCaseMakerHelper extends MG {
106 public static @tag("grammaro") @raw Object grammaro(Iterable<Tree> t) {
107 System.out.println("working on " + t);
108 return Demo.make(t.iterator().next(), "s", new ReflectiveMetaPlain());
110 //public static @tag("tca") Object tca(Object[] o) throws IOException {
111 //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
112 public static @tag("tca") Object tca(String input, String[] output, Union u) throws IOException {
113 return new TestCase(input, output, u, false, false); }
114 public static @tag("tcb") Object tca(String input, Union u) throws IOException {
115 return new TestCase(input, new String[0], u, false, false); }
116 public static @tag("ts") TestCase[] go(TestCase[] cases) { return cases; }
117 public static @tag("o") Object o(Object[] s) { return s; }
121 public static class TestCase {
122 private final boolean tib;
123 private final boolean jav;
124 public /*final*/ String input;
125 public final String[] output;
126 public final Union grammar;
128 public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
132 this.output = output==null ? new String[0] : output;
133 this.grammar = grammar;
135 public String toString() {
136 String ret = "testcase {\n" + " input \""+input+"\";\n";
137 for(String s : output) ret += " output \""+s+"\";\n";
138 ret += grammar +"\n}\n";
141 public boolean execute() throws Exception {
142 Forest<String> res = null;
143 ParseFailed pfe = null;
144 CharParser parser = new CharParser(grammar);
145 //parser.helpgc = false;
148 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
149 : parser.parse(new StringReader(input));
150 } catch (ParseFailed pf) {
153 //ystem.out.println("res=="+res);
156 FileOutputStream fos = new FileOutputStream("out.dot");
157 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
158 GraphViz gv = new GraphViz();
163 System.out.println(parser);
165 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
166 System.out.print("\r");
167 if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
168 System.out.print("\033[31m");
169 System.out.println("PARSE FAILED");
170 System.out.print("\033[0m");
171 if (pfe != null) pfe.printStackTrace();
173 System.out.print("\r \r");
175 HashSet<String> outs = new HashSet<String>();
176 if (output != null) for(String s : output) outs.add(s.trim());
178 for (Tree<String> r : results) {
179 String s = r.toString().trim();
180 if (outs.contains(s)) { outs.remove(s); continue; }
181 if (!bad) System.out.println(input);
182 System.out.print("\033[33m");
183 System.out.println(" GOT: " + s);
186 for(String s : outs) {
187 if (!bad) System.out.println(input);
188 System.out.print("\033[31m");
189 System.out.println("EXPECTED: " + s);
193 System.out.println("\033[0m");
196 System.out.println("\r\033[32mPASS\033[0m ");
202 public static class TestCaseBuilder extends StringWalker {
203 public Object walk(Tree<String> tree) {
205 if ("grammaro".equals(tree.head())) return Demo.make(tree, "s");
206 else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
207 else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
208 else if ("testcase".equals(tree.head())) {
209 String input = MetaGrammar.string(tree.child(0));
210 String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
211 Union grammar = Demo.make(tree.child(tree.numChildren()-1), "s");
212 TestCase tc = new TestCase(input, output, grammar, false, false);
214 } else if ("ts".equals(tree.head())) return walk(tree, 0);
215 else if (tree.head() == null) {
216 Object[] ret = new Object[tree.numChildren()];
217 for(int i=0; i<ret.length; i++)
218 ret[i] = walk(tree.child(i));
219 return Reflection.lub(ret);
221 return super.walk(tree);
222 } catch (Exception e) {
227 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }