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 edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.tib.*;
7
8 public class RegressionTests {
9
10     public static boolean yes = false;
11
12     public static void main(String[] s) throws Exception {
13         try {
14             boolean profile = false;
15             if (s[0].equals("-profile")) {
16                 profile = true;
17                 String[] s2 = new String[s.length-1];
18                 System.arraycopy(s, 1, s2, 0, s2.length);
19                 s = s2;
20             }
21
22             //MetaGrammar mg0 = new MetaGrammar();
23             //mg0.walk(MetaGrammar.meta);
24             //System.out.println(mg0);
25             Tree<String> res = new CharToStringParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
26             MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res);
27             //System.out.println(mg);
28             Union meta = mg.done();
29             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
30             res = new CharToStringParser(meta).parse(sis).expand1();
31             Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
32             if (testcasegrammar==null) return;
33             CharToStringParser parser = new CharToStringParser(testcasegrammar);
34
35             if (profile) {
36                 System.out.println("\nready...");
37                 System.in.read();
38             }
39             System.gc();
40             long now = System.currentTimeMillis();
41             Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
42             System.out.println();
43             System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
44             if (profile) {
45                 System.out.println("\ndone");
46                 System.in.read();
47                 System.exit(0);
48             }
49             for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
50
51         } catch (Throwable t) {
52             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
53             System.err.println(t);
54             System.err.println();
55             t.printStackTrace();
56             System.err.println();
57         }
58     }
59
60     public static class TestCase {
61         private final boolean tib;
62         public final String input;        
63         public final String[] output;
64         public final Union grammar;
65         public TestCase(String input, String[] output, Union grammar, boolean tib) throws IOException {
66             this.tib = tib;
67             this.input = input;
68             this.output = output;
69             this.grammar = grammar;
70         }
71         public String toString() {
72             String ret = "testcase {\n" + "  input \""+input+"\";\n";
73             for(String s : output) ret += "  output \""+s+"\";\n";
74             ret += grammar +"\n}\n";
75             return ret;
76         }
77         public boolean execute() throws Exception {
78             Forest<String> res = null;
79             ParseFailed pfe = null;
80             try {
81                 res = tib 
82                 ? new CharToStringParser(grammar).parse(new Tib(input))
83                 : new CharToStringParser(grammar).parse(new StringReader(input));
84             } catch (ParseFailed pf) {
85                 pfe = pf;
86             }
87             //ystem.out.println("res=="+res);
88             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
89             System.out.print("\r");
90             if (results.size() == 0 && output.length > 0) {
91                 System.out.print("\033[31m");
92                 System.out.println("PARSE FAILED");
93                 System.out.print("\033[0m");
94                 if (pfe != null) pfe.printStackTrace();
95             } else {
96                 System.out.print("\r                                                                                                              \r");
97             }
98             HashSet<String> outs = new HashSet<String>();
99             if (output != null) for(String s : output) outs.add(s.trim());
100             boolean bad = false;
101             for (Tree<String> r : results) {
102                 String s = r.toString().trim();
103                 if (outs.contains(s)) { outs.remove(s); continue; }
104                 if (!bad) System.out.println(input);
105                 System.out.print("\033[33m");
106                 System.out.println("     GOT: " + s);
107                 bad = true;
108             }
109             for(String s : outs) {
110                 if (!bad) System.out.println(input);
111                 System.out.print("\033[31m");
112                 System.out.println("EXPECTED: " + s);
113                 bad = true;
114             }
115             if (bad) {
116                 System.out.println("\033[0m");
117                 return true;
118             }             
119             System.out.println("\r\033[32mPASS\033[0m                                                                              ");
120             return false;
121         }
122     }
123
124     public static class TestCaseBuilder extends MetaGrammar {
125         public Object walk(Tree<String> tree) {
126             try {
127                 if ("grammar".equals(tree.head())) { walkChildren(tree); return done("s"); }
128                 else if ("output".equals(tree.head())) return string(tree.children());
129                 else if ("input".equals(tree.head())) return string(tree.children());
130                 else if ("tibcase".equals(tree.head()) || "testcase".equals(tree.head())) {
131                     String input = string(tree.child(0));
132                     String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
133                     boolean tib = "tibcase".equals(tree.head());
134                     MetaGrammar gram = tib ? new Tib.Grammar() : new MetaGrammar();
135                     Union grammar = (Union)((MetaGrammar)(gram.walk(tree, tree.numChildren()-1))).done("s");
136                     return new TestCase(input, output, grammar, tib);
137                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
138                 else return super.walk(tree);
139             } catch (Exception e) {
140                 throw new Error(e);
141             }
142         }
143     }
144
145     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
146 }
147