preliminary support for serialization of parse tables
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
1 // Copyright 2006-2007 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.meta.*;
9 import edu.berkeley.sbp.chr.*;
10 import edu.berkeley.sbp.util.*;
11
12 public class RegressionTests {
13
14     public static boolean yes = false;
15     public static boolean graph = false;
16     public static GrammarAST.ImportResolver resolver = new GrammarAST.ImportResolver() {
17             public InputStream getImportStream(String importname) {
18                 try {
19                     return new FileInputStream("tests/"+importname);
20                 } catch (IOException e) {
21                     throw new RuntimeException(e);
22                 }
23             }
24         };
25
26     public static void main() throws Exception {
27         main(new String[] { null, "tests/testcase.g", "tests/regression.tc" });
28     }
29
30     public static void main(String[] s) throws Exception {
31         try {
32             boolean profile = false;
33             if (s[0].equals("-graph")) {
34                 graph = true;
35                 String[] s2 = new String[s.length-1];
36                 System.arraycopy(s, 1, s2, 0, s2.length);
37                 s = s2;
38             }
39             if (s[0].equals("-profile")) {
40                 profile = true;
41                 String[] s2 = new String[s.length-1];
42                 System.arraycopy(s, 1, s2, 0, s2.length);
43                 s = s2;
44             }
45
46             CharParser cp;
47             Tree<String> res;
48
49             InputStream metaGrammarStream =
50                 s[0] == null
51                 ? RegressionTests.class.getClassLoader().getResourceAsStream("edu/berkeley/sbp/meta/meta.g")
52                 : new FileInputStream(s[0]);
53             res = new CharParser(GrammarAST.getMetaGrammar()).parse(metaGrammarStream).expand1();
54             Union meta = GrammarAST.buildFromAST(res, "s", resolver);
55             cp = new CharParser(meta);
56
57             System.err.println("serializing grammar to grammar.ser...");
58             ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("grammar.ser"));
59             oos.writeObject(cp);
60             oos.close();
61
62             System.err.println("deserializing grammar from grammar.ser...");
63             ObjectInputStream ois = new ObjectInputStream(new FileInputStream("grammar.ser"));
64             cp = (CharParser)ois.readObject();
65             ois.close();
66
67             System.err.println("parsing " + s[1]);
68             res = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
69
70             Union testcasegrammar = GrammarAST.buildFromAST(res, "s", resolver);
71             if (testcasegrammar==null) return;
72             CharParser parser = new CharParser(testcasegrammar);
73
74             if (profile) {
75                 System.out.println("\nready...");
76                 System.in.read();
77             }
78             System.gc();
79             long now = System.currentTimeMillis();
80             System.err.println("parsing " + s[2]);
81             Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
82             System.out.println();
83             System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
84             if (profile) {
85                 System.out.println("\ndone");
86                 System.in.read();
87                 System.exit(0);
88             }
89             System.err.println("expanding...");
90
91             ArrayList<TestCase> cases = new ArrayList<TestCase>();
92             Tree tt = r2.expand1();
93             for(int i=0; i<tt.size(); i++) {
94                 Tree t = tt.child(i);
95                 String[] expect = !"ignore output;".equals(t.child(2).head()) ? new String[t.child(2).size()] : null;
96                 if (expect != null)
97                     for(int j=0; j<t.child(2).size(); j++)
98                         expect[j] = stringifyChildren(t.child(2).child(j));
99                 cases.add(new TestCase(stringifyChildren(t.child(0)),
100                                        stringifyChildren(t.child(1)),
101                                        expect,
102                                        GrammarAST.buildFromAST(t.child(3), "s", resolver),
103                                        false,
104                                        false));
105                 
106             }
107             TestCase[] expanded = new TestCase[cases.size()];
108             for(int i=0; i<expanded.length; i++)
109                 expanded[i] = cases.get(i);
110             for(TestCase tc : expanded)
111                 tc.execute();
112
113         } catch (Throwable t) {
114             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
115             System.err.println(t);
116             System.err.println();
117             t.printStackTrace();
118             System.err.println();
119         }
120     }
121
122     private static String stringifyChildren(Tree t) {
123         StringBuffer sb = new StringBuffer();
124         for(int i=0; i<t.size(); i++) {
125             sb.append(t.child(i).head());
126             sb.append(stringifyChildren(t.child(i)));
127         }
128         return sb.toString();
129     }
130
131     public static class TestCase {
132         private final boolean jav;
133         public /*final*/ String input;        
134         public final String[] output;
135         public final Union grammar;
136         public final String name;
137
138         public TestCase(String name, String input, String[] output,
139                         Union grammar, boolean tib, boolean jav) {
140             this.name = name;
141             this.jav = jav;
142             this.input = input;
143             this.output = output;
144             this.grammar = grammar;
145         }
146         public String toString() {
147             String ret = "testcase {\n" + "  input \""+input+"\";\n";
148             if (output != null)
149                 for(String s : output) ret += "  output \""+s+"\";\n";
150             ret += grammar +"\n}\n";
151             return ret;
152         }
153         public boolean execute() throws Exception {
154             Forest<String> res = null;
155             ParseFailed pfe = null;
156             CharParser parser = new CharParser(grammar);
157             System.out.print("     "+name+"\r");
158             try {
159                 res = parser.parse(new StringReader(input));
160             } catch (ParseFailed pf) { pfe = pf; }
161
162             if (graph) {
163                 FileOutputStream fos = new FileOutputStream("out.dot");
164                 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
165                 GraphViz gv = new GraphViz();
166                 res.toGraphViz(gv);
167                 gv.dump(p);
168                 p.flush();
169                 p.close();
170                 System.out.println(parser);
171             }
172
173             if (output==null) {
174                 System.out.println("\r\033[32mDONE\033[0m "+name);
175                 return true;
176             }
177             Iterable<Tree<String>> results =
178                 res==null ? new HashSet<Tree<String>>() : res.expand();
179
180             System.out.print("\r");
181             if (results == null || (!results.iterator().hasNext() && (output!=null && output.length > 0))) {
182                 System.out.print("\033[31m");
183                 System.out.print("FAIL ");
184                 System.out.println("\033[0m "+name);
185                 if (pfe != null) pfe.printStackTrace();
186             } else {
187                 System.out.print("\r                                                                                                              \r");
188             }
189             HashSet<String> outs = new HashSet<String>();
190             if (output != null) for(String s : output) outs.add(s.trim());
191             boolean bad = false;
192             for (Tree<String> r : results) {
193                 String s = r.toString().trim();
194                 if (outs.contains(s)) { outs.remove(s); continue; }
195                 if (!bad) System.out.println(input);
196                 System.out.print("\033[33m");
197                 System.out.println("     GOT: " + s);
198                 bad = true;
199             }
200             for(String s : outs) {
201                 if (!bad) System.out.println(input);
202                 System.out.print("\033[31m");
203                 System.out.println("EXPECTED: " + s);
204                 bad = true;
205             }
206             if (bad) {
207                 System.out.println("\033[0m");
208                 return true;
209             }             
210             System.out.println("\r\033[32mPASS\033[0m "+name);
211
212             return false;
213         }
214     }
215
216     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
217     public static String string(Tree<String> tree) {
218         String ret = "";
219         if (tree.head()!=null) ret += tree.head();
220         ret += string(tree.children());
221         return ret;
222     }
223     public static String string(Iterable<Tree<String>> children) {
224         String ret = "";
225         for(Tree<String> t : children) ret += string(t);
226         return ret;
227     }
228 }