1 package edu.berkeley.sbp.misc;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.tib.*;
8 public class RegressionTests {
10 public static boolean yes = false;
11 public static class MyWalker extends ReflectiveWalker {
12 public String top(Object[] o) { return "top("+join(o)+")"; }
13 public String str(String[] s) { String ret = ""; for(String st : s) ret += st; return ret; }
14 public String join(Object[] o) { String ret = ""; for(Object st : o) ret += st; return ret; }
15 public String whilex(Object s, Object y) { return "while("+s+") " + y; }
16 public String seq(Object[] statements) {
18 for(Object s : statements) ret += s + ";\n";
22 public String bl(String s) { return "{" + s + "}"; }
26 public static void main(String[] s) throws Exception {
28 boolean profile = false;
29 if (s[0].equals("-profile")) {
31 String[] s2 = new String[s.length-1];
32 System.arraycopy(s, 1, s2, 0, s2.length);
36 //MetaGrammar mg0 = new MetaGrammar();
37 //mg0.walk(MetaGrammar.meta);
38 //System.out.println(mg0);
39 Tree<String> res = new Parser(MetaGrammar.make(), CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(s[0]))));
40 MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res);
41 //System.out.println(mg);
42 Union meta = mg.done();
43 SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
44 res = new Parser(meta, CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(sis), "parsing " + s[1] + " using " + s[0]));
45 Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
46 if (testcasegrammar==null) return;
47 CharToken.Stream cs = new CharToken.Stream(new InputStreamReader(new FileInputStream(s[2])), "parsing " + s[2] + " using " + s[1]);
48 Parser parser = new Parser(testcasegrammar, CharToken.top());
51 System.out.println("\nready...");
54 Forest<String> r2 = parser.parse(cs);
56 System.out.println("\ndone");
60 for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
62 } catch (Throwable t) {
63 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
64 System.err.println(t);
71 public static class TestCase {
72 private final Token.Stream inp;
73 public final String input;
74 public final String[] output;
75 public final Union grammar;
76 public TestCase(String input, String[] output, Union grammar, boolean tib) throws IOException {
79 : new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":"");
82 this.grammar = grammar;
84 public String toString() {
85 String ret = "testcase {\n" + " input \""+input+"\";\n";
86 for(String s : output) ret += " output \""+s+"\";\n";
87 ret += grammar +"\n}\n";
90 public boolean execute() throws Exception {
91 Forest<String> res = new Parser(grammar, CharToken.top()).parse(inp);
92 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
93 System.out.print("\r");
94 if (results.size() == 0 && output.length > 0) {
95 System.out.print("\033[31m");
96 System.out.println("PARSE FAILED");
97 System.out.print("\033[0m");
99 System.out.println("\r \r");
101 HashSet<String> outs = new HashSet<String>();
102 if (output != null) for(String s : output) outs.add(s.trim());
104 for (Tree<String> r : results) {
105 String s = r.toString().trim();
106 if (outs.contains(s)) { outs.remove(s); continue; }
107 System.out.print("\033[33m");
108 System.out.println(" GOT: " + s);
111 for(String s : outs) {
112 System.out.print("\033[31m");
113 System.out.println("EXPECTED: " + s);
117 System.out.print("\033[0m");
120 System.out.println("\033[32mPASS\033[0m");
125 public static class TestCaseBuilder extends MetaGrammar {
126 public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
127 public TestCase[] ts(TestCase[] ts) { return ts; }
128 public TestCase testcase(String input, String[] output, Union grammar) throws IOException {
129 return new TestCase(input, output, grammar, false); }
130 public TestCase testcase(String input, Union grammar) throws IOException {
131 return new TestCase(input, new String[0], grammar, false); }
132 public TestCase tibcase(String input, String[] output, Union grammar) throws IOException {
133 return new TestCase(input, output, grammar, true); }
134 public TestCase tibcase(String input, Union grammar) throws IOException {
135 return new TestCase(input, new String[0], grammar, true); }
136 public MetaGrammar grammar(Object[] o) { return this; }
137 public Object walk(String tag, Object[] args) {
138 if ("grammar".equals(tag)) {
139 //System.out.println("\n" + this + "\n");
142 else return super.walk(tag, args);
146 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }