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