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