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