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