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