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.bind.*;
8 import edu.berkeley.sbp.tib.*;
9 import edu.berkeley.sbp.chr.*;
10 import edu.berkeley.sbp.util.*;
11 import static edu.berkeley.sbp.misc.MetaGrammar.*;
12
13 public class RegressionTests {
14
15     public static boolean yes = false;
16     public static boolean graph = false;
17
18     public static void main(String[] s) throws Exception {
19         try {
20             boolean profile = false;
21             if (s[0].equals("-graph")) {
22                 graph = true;
23                 String[] s2 = new String[s.length-1];
24                 System.arraycopy(s, 1, s2, 0, s2.length);
25                 s = s2;
26             }
27             if (s[0].equals("-profile")) {
28                 profile = true;
29                 String[] s2 = new String[s.length-1];
30                 System.arraycopy(s, 1, s2, 0, s2.length);
31                 s = s2;
32             }
33
34             System.err.println("parsing " + s[0]);
35             Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
36             Union meta = MetaGrammar.make(res, "s");
37
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(sis).expand1();
41
42             Union testcasegrammar = MetaGrammar.make(res, "ts", new TestCaseMaker());
43             if (testcasegrammar==null) return;
44             CharParser parser = new CharParser(testcasegrammar);
45
46             if (profile) {
47                 System.out.println("\nready...");
48                 System.in.read();
49             }
50             System.gc();
51             long now = System.currentTimeMillis();
52             System.err.println("parsing " + s[2]);
53             Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
54             System.out.println();
55             System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
56             if (profile) {
57                 System.out.println("\ndone");
58                 System.in.read();
59                 System.exit(0);
60             }
61             System.err.println("expanding...");
62
63             Tree t = r2.expand1();
64             TestCase[] expanded = (TestCase[])((Functor)t.head()).invoke(t.children());
65             System.err.println("executing...");
66             for(TestCase tc : expanded) {
67                 tc.execute();
68                 /*
69                 String st = "a";
70                 for(int i=0; i<12; i++) {
71                     //System.out.println("length " + st.length());
72                     tc.input = st;
73                     long nowy = System.currentTimeMillis();
74                     GSS.shifts = 0;
75                     GSS.reductions = 0;
76                     tc.execute();
77                     System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
78                     st = st+st;
79                 }
80                 */
81             }
82
83         } catch (Throwable t) {
84             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
85             System.err.println(t);
86             System.err.println();
87             t.printStackTrace();
88             System.err.println();
89         }
90     }
91
92     public static class TestCaseMaker extends ReflectiveMeta {
93         public TestCaseMaker() {
94             super(TestCaseMakerHelper.class, new Class[] {
95                      MG.Grammar.class,
96                      MG.NonTerminal.class,
97                      MG.AnonUn.class,
98                      MG.Range.class,
99                      MG.El.class,
100                      MG.Seq.class,
101                      MG.NonTerminalReference.class,
102                      MG.StringLiteral.class,
103                      MG.XTree.class,
104                      MG.CharClass.class
105             });
106         }
107         public static class TestCaseMakerHelper extends MG {
108             public static @bind.as("grammaro") @bind.raw Object grammaro(Iterable<Tree> t) {
109                 System.out.println("working on " + t);
110                 return MetaGrammar.make(t.iterator().next(), "s", new ReflectiveMetaPlain());
111             }
112             //public static @bind.as("tca")           Object tca(Object[] o) throws IOException {
113             //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
114             public static @bind.as("tca")           Object tca(String input, String[] output, Union u) throws IOException {
115                 return new TestCase(input, output, u, false, false); }
116             public static @bind.as("tcb")           Object tca(String input, Union u) throws IOException {
117                 return new TestCase(input, new String[0], u, false, false); }
118             public static @bind.as("ts") TestCase[] go(TestCase[] cases) { return cases; }
119             public static @bind.as("o") Object o(Object[] s) { return s; }
120         }
121     }
122
123     public static class TestCase {
124         private final boolean tib;
125         private final boolean jav;
126         public /*final*/ String input;        
127         public final String[] output;
128         public final Union grammar;
129
130         public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
131             this.tib = tib;
132             this.jav = jav;
133             this.input = input;
134             this.output = output==null ? new String[0] : output;
135             this.grammar = grammar;
136         }
137         public String toString() {
138             String ret = "testcase {\n" + "  input \""+input+"\";\n";
139             for(String s : output) ret += "  output \""+s+"\";\n";
140             ret += grammar +"\n}\n";
141             return ret;
142         }
143         public boolean execute() throws Exception {
144             Forest<String> res = null;
145             ParseFailed pfe = null;
146             CharParser parser = new CharParser(grammar);
147             //parser.helpgc = false;
148             try {
149                 res = tib 
150                     ? /*new CharParser(grammar).parse(new Tib(input))*/ null
151                 : parser.parse(new StringReader(input));
152             } catch (ParseFailed pf) {
153                 pfe = pf;
154             }
155             //ystem.out.println("res=="+res);
156
157             if (graph) {
158                 FileOutputStream fos = new FileOutputStream("out.dot");
159                 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
160                 GraphViz gv = new GraphViz();
161                 res.toGraphViz(gv);
162                 gv.dump(p);
163                 p.flush();
164                 p.close();
165                 System.out.println(parser);
166             }
167             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
168             System.out.print("\r");
169             if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
170                 System.out.print("\033[31m");
171                 System.out.println("PARSE FAILED");
172                 System.out.print("\033[0m");
173                 if (pfe != null) pfe.printStackTrace();
174             } else {
175                 System.out.print("\r                                                                                                              \r");
176             }
177             HashSet<String> outs = new HashSet<String>();
178             if (output != null) for(String s : output) outs.add(s.trim());
179             boolean bad = false;
180             for (Tree<String> r : results) {
181                 String s = r.toString().trim();
182                 if (outs.contains(s)) { outs.remove(s); continue; }
183                 if (!bad) System.out.println(input);
184                 System.out.print("\033[33m");
185                 System.out.println("     GOT: " + s);
186                 bad = true;
187             }
188             for(String s : outs) {
189                 if (!bad) System.out.println(input);
190                 System.out.print("\033[31m");
191                 System.out.println("EXPECTED: " + s);
192                 bad = true;
193             }
194             if (bad) {
195                 System.out.println("\033[0m");
196                 return true;
197             }             
198             System.out.println("\r\033[32mPASS\033[0m                                                                              ");
199
200             return false;
201         }
202     }
203
204     public static class TestCaseBuilder extends StringWalker {
205         public Object walk(Tree<String> tree) {
206             try {
207                 if ("grammaro".equals(tree.head())) return MetaGrammar.make(tree, "s");
208                 else if ("output".equals(tree.head())) return string(tree.children());
209                 else if ("input".equals(tree.head())) return string(tree.children());
210                 else if ("testcase".equals(tree.head())) {
211                     String input = string(tree.child(0));
212                     String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
213                     Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
214                     TestCase tc = new TestCase(input, output, grammar, false, false);
215                     return tc;
216                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
217                 else if (tree.head() == null) {
218                     Object[] ret = new Object[tree.numChildren()];
219                     for(int i=0; i<ret.length; i++)
220                         ret[i] = walk(tree.child(i));
221                     return Reflection.lub(ret);
222                 }
223                 return super.walk(tree);
224             } catch (Exception e) {
225                 throw new Error(e);
226             }
227         }
228     }
229     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
230     public static String string(Tree<String> tree) {
231         String ret = "";
232         if (tree.head()!=null) ret += tree.head();
233         ret += string(tree.children());
234         return ret;
235     }
236     public static String string(Iterable<Tree<String>> children) {
237         String ret = "";
238         for(Tree<String> t : children) ret += string(t);
239         return ret;
240     }
241 }