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