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