checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
1 package edu.berkeley.sbp.misc;
2 import java.io.*;
3 import java.util.*;
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.*;
11
12 public class RegressionTests {
13
14     public static boolean yes = false;
15     public static boolean graph = false;
16
17     public static void main(String[] s) throws Exception {
18         try {
19             boolean profile = false;
20             if (s[0].equals("-graph")) {
21                 graph = true;
22                 String[] s2 = new String[s.length-1];
23                 System.arraycopy(s, 1, s2, 0, s2.length);
24                 s = s2;
25             }
26             if (s[0].equals("-profile")) {
27                 profile = true;
28                 String[] s2 = new String[s.length-1];
29                 System.arraycopy(s, 1, s2, 0, s2.length);
30                 s = s2;
31             }
32
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();
39
40             Union testcasegrammar = Demo.make(res, "ts", new TestCaseMaker());
41             if (testcasegrammar==null) return;
42             CharParser parser = new CharParser(testcasegrammar);
43
44             if (profile) {
45                 System.out.println("\nready...");
46                 System.in.read();
47             }
48             System.gc();
49             long now = System.currentTimeMillis();
50             System.err.println("parsing " + s[2]);
51             Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
52             System.out.println();
53             System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
54             if (profile) {
55                 System.out.println("\ndone");
56                 System.in.read();
57                 System.exit(0);
58             }
59             System.err.println("expanding...");
60
61             Tree t = r2.expand1();
62             TestCase[] expanded = (TestCase[])((Demo.Reducer)t.head()).reduce(t);
63             System.err.println("executing...");
64             for(TestCase tc : expanded) {
65                 tc.execute();
66                 /*
67                 String st = "a";
68                 for(int i=0; i<12; i++) {
69                     //System.out.println("length " + st.length());
70                     tc.input = st;
71                     long nowy = System.currentTimeMillis();
72                     GSS.shifts = 0;
73                     GSS.reductions = 0;
74                     tc.execute();
75                     System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
76                     st = st+st;
77                 }
78                 */
79             }
80
81         } catch (Throwable t) {
82             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
83             System.err.println(t);
84             System.err.println();
85             t.printStackTrace();
86             System.err.println();
87         }
88     }
89
90     public static class TestCaseMaker extends ReflectiveMeta {
91         public TestCaseMaker() {
92             super(TestCaseMakerHelper.class, new Class[] {
93                      MG.Grammar.class,
94                      MG.NonTerminal.class,
95                      MG.AnonUn.class,
96                      MG.Range.class,
97                      MG.El.class,
98                      MG.Seq.class,
99                      MG.NonTerminalReference.class,
100                      MG.StringLiteral.class,
101                      MG.XTree.class,
102                      MG.CharClass.class
103             });
104         }
105         public static class TestCaseMakerHelper extends MG {
106             public static @tag("grammaro") @raw Object grammaro(Tree t) {
107                 System.out.println("working on " + t);
108                 return Demo.make(t.child(0), "s", new ReflectiveMetaPlain());
109             }
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; }
118         }
119     }
120
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;
127
128         public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
129             this.tib = tib;
130             this.jav = jav;
131             this.input = input;
132             this.output = output==null ? new String[0] : output;
133             this.grammar = grammar;
134         }
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";
139             return ret;
140         }
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;
146             try {
147                 res = tib 
148                     ? /*new CharParser(grammar).parse(new Tib(input))*/ null
149                 : parser.parse(new StringReader(input));
150             } catch (ParseFailed pf) {
151                 pfe = pf;
152             }
153             //ystem.out.println("res=="+res);
154
155             if (graph) {
156                 FileOutputStream fos = new FileOutputStream("out.dot");
157                 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
158                 GraphViz gv = new GraphViz();
159                 res.toGraphViz(gv);
160                 gv.dump(p);
161                 p.flush();
162                 p.close();
163                 System.out.println(parser);
164             }
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();
172             } else {
173                 System.out.print("\r                                                                                                              \r");
174             }
175             HashSet<String> outs = new HashSet<String>();
176             if (output != null) for(String s : output) outs.add(s.trim());
177             boolean bad = false;
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);
184                 bad = true;
185             }
186             for(String s : outs) {
187                 if (!bad) System.out.println(input);
188                 System.out.print("\033[31m");
189                 System.out.println("EXPECTED: " + s);
190                 bad = true;
191             }
192             if (bad) {
193                 System.out.println("\033[0m");
194                 return true;
195             }             
196             System.out.println("\r\033[32mPASS\033[0m                                                                              ");
197
198             return false;
199         }
200     }
201
202     public static class TestCaseBuilder extends StringWalker {
203         public Object walk(Tree<String> tree) {
204             try {
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);
213                     return tc;
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);
220                 }
221                 return super.walk(tree);
222             } catch (Exception e) {
223                 throw new Error(e);
224             }
225         }
226     }
227     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
228 }
229