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