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