checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
1 package edu.berkeley.sbp.misc;
2 import java.io.*;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.tib.*;
8 import edu.berkeley.sbp.chr.*;
9 import edu.berkeley.sbp.util.*;
10
11 public class RegressionTests {
12
13     public static boolean yes = false;
14     public static boolean graph = false;
15
16     public static void main(String[] s) throws Exception {
17         try {
18             boolean profile = false;
19             if (s[0].equals("-graph")) {
20                 graph = true;
21                 String[] s2 = new String[s.length-1];
22                 System.arraycopy(s, 1, s2, 0, s2.length);
23                 s = s2;
24             }
25             if (s[0].equals("-profile")) {
26                 profile = true;
27                 String[] s2 = new String[s.length-1];
28                 System.arraycopy(s, 1, s2, 0, s2.length);
29                 s = s2;
30             }
31
32             System.err.println("parsing " + s[0]);
33             Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
34             Union meta = MetaGrammar.make(res, "s");
35             System.err.println("parsing " + s[1]);
36             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
37             res = new CharParser(meta).parse(sis).expand1();
38             Union testcasegrammar = MetaGrammar.make(res, "ts");
39             if (testcasegrammar==null) return;
40             CharParser parser = new CharParser(testcasegrammar);
41
42             if (profile) {
43                 System.out.println("\nready...");
44                 System.in.read();
45             }
46             System.gc();
47             long now = System.currentTimeMillis();
48             System.err.println("parsing " + s[2]);
49             Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
50             System.out.println();
51             System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
52             if (profile) {
53                 System.out.println("\ndone");
54                 System.in.read();
55                 System.exit(0);
56             }
57             System.err.println("expanding...");
58
59             TestCase[] expanded = (TestCase[])new TestCaseBuilder().walk(r2.expand1());
60             System.err.println("executing...");
61             for(TestCase tc : expanded) {
62                 tc.execute();
63                 /*
64                 String st = "a";
65                 for(int i=0; i<12; i++) {
66                     //System.out.println("length " + st.length());
67                     tc.input = st;
68                     long nowy = System.currentTimeMillis();
69                     GSS.shifts = 0;
70                     GSS.reductions = 0;
71                     tc.execute();
72                     System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
73                     st = st+st;
74                 }
75                 */
76             }
77
78         } catch (Throwable t) {
79             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
80             System.err.println(t);
81             System.err.println();
82             t.printStackTrace();
83             System.err.println();
84         }
85     }
86
87     public static class TestCase {
88         private final boolean tib;
89         private final boolean jav;
90         public /*final*/ String input;        
91         public final String[] output;
92         public final Union grammar;
93         public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
94             this.tib = tib;
95             this.jav = jav;
96             this.input = input;
97             this.output = output;
98             this.grammar = grammar;
99         }
100         public String toString() {
101             String ret = "testcase {\n" + "  input \""+input+"\";\n";
102             for(String s : output) ret += "  output \""+s+"\";\n";
103             ret += grammar +"\n}\n";
104             return ret;
105         }
106         public boolean execute() throws Exception {
107             Forest<String> res = null;
108             ParseFailed pfe = null;
109             CharParser parser = new CharParser(grammar);
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             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
131             System.out.print("\r");
132             if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
133                 System.out.print("\033[31m");
134                 System.out.println("PARSE FAILED");
135                 System.out.print("\033[0m");
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                                                                              ");
162
163             return false;
164         }
165     }
166
167     public static class TestCaseBuilder extends StringWalker {
168         public Object walk(Tree<String> tree) {
169             try {
170                 if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
171                 else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
172                 else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
173                 else if ("testcase".equals(tree.head())) {
174                     String input = MetaGrammar.string(tree.child(0));
175                     String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
176                     Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
177                     TestCase tc = new TestCase(input, output, grammar, false, false);
178                     return tc;
179                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
180                 else if (tree.head() == null) {
181                     Object[] ret = new Object[tree.numChildren()];
182                     for(int i=0; i<ret.length; i++)
183                         ret[i] = walk(tree.child(i));
184                     return Reflection.lub(ret);
185                 }
186                 return super.walk(tree);
187             } catch (Exception e) {
188                 throw new Error(e);
189             }
190         }
191     }
192     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
193 }
194