aa12c2a14df622a181f33b3d3699269da7aea5c1
[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(MetaGrammar.newInstance()).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             TestCase[] expanded = (TestCase[])new GrammarAST(includes, "").walkChildren(r2.expand1());
70             for(TestCase tc : expanded)
71                 tc.execute();
72
73         } catch (Throwable t) {
74             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
75             System.err.println(t);
76             System.err.println();
77             t.printStackTrace();
78             System.err.println();
79         }
80     }
81
82     public static class TestCase {
83         private final boolean tib;
84         private final boolean jav;
85         public /*final*/ String input;        
86         public final String[] output;
87         public final Union grammar;
88         public final String name;
89
90         public TestCase(String name, String input, String[] output,
91                         Union grammar, boolean tib, boolean jav) {
92             this.tib = tib;
93             this.name = name;
94             this.jav = jav;
95             this.input = input;
96             this.output = output==null ? new String[0] : output;
97             this.grammar = grammar;
98         }
99         public String toString() {
100             String ret = "testcase {\n" + "  input \""+input+"\";\n";
101             for(String s : output) ret += "  output \""+s+"\";\n";
102             ret += grammar +"\n}\n";
103             return ret;
104         }
105         public boolean execute() throws Exception {
106             Forest<String> res = null;
107             ParseFailed pfe = null;
108             CharParser parser = new CharParser(grammar);
109             System.out.print("     "+name+"\r");
110             //parser.helpgc = false;
111             try {
112                 res = tib 
113                     ? /*new CharParser(grammar).parse(new Tib(input))*/ null
114                 : parser.parse(new StringReader(input));
115             } catch (ParseFailed pf) {
116                 pfe = pf;
117             }
118             //ystem.out.println("res=="+res);
119
120             if (graph) {
121                 FileOutputStream fos = new FileOutputStream("out.dot");
122                 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
123                 GraphViz gv = new GraphViz();
124                 res.toGraphViz(gv);
125                 gv.dump(p);
126                 p.flush();
127                 p.close();
128                 System.out.println(parser);
129             }
130
131             Iterable<Tree<String>> results =
132                 res==null ? new HashSet<Tree<String>>() : res.expand();
133
134             System.out.print("\r");
135             if (results == null || (!results.iterator().hasNext() && (output!=null && output.length > 0))) {
136                 System.out.print("\033[31m");
137                 System.out.print("FAIL ");
138                 System.out.println("\033[0m "+name);
139                 if (pfe != null) pfe.printStackTrace();
140             } else {
141                 System.out.print("\r                                                                                                              \r");
142             }
143             HashSet<String> outs = new HashSet<String>();
144             if (output != null) for(String s : output) outs.add(s.trim());
145             boolean bad = false;
146             for (Tree<String> r : results) {
147                 String s = r.toString().trim();
148                 if (outs.contains(s)) { outs.remove(s); continue; }
149                 if (!bad) System.out.println(input);
150                 System.out.print("\033[33m");
151                 System.out.println("     GOT: " + s);
152                 bad = true;
153             }
154             for(String s : outs) {
155                 if (!bad) System.out.println(input);
156                 System.out.print("\033[31m");
157                 System.out.println("EXPECTED: " + s);
158                 bad = true;
159             }
160             if (bad) {
161                 System.out.println("\033[0m");
162                 return true;
163             }             
164             System.out.println("\r\033[32mPASS\033[0m "+name);
165
166             return false;
167         }
168     }
169
170     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
171     public static String string(Tree<String> tree) {
172         String ret = "";
173         if (tree.head()!=null) ret += tree.head();
174         ret += string(tree.children());
175         return ret;
176     }
177     public static String string(Iterable<Tree<String>> children) {
178         String ret = "";
179         for(Tree<String> t : children) ret += string(t);
180         return ret;
181     }
182 }