1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.misc;
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.*;
14 public class RegressionTests {
16 public static boolean yes = false;
17 public static boolean graph = false;
19 public static void main() throws Exception {
20 main(new String[] { "tests/meta.g", "tests/testcase.g", "tests/regression.tc" });
23 public static void main(String[] s) throws Exception {
25 boolean profile = false;
26 if (s[0].equals("-graph")) {
28 String[] s2 = new String[s.length-1];
29 System.arraycopy(s, 1, s2, 0, s2.length);
32 if (s[0].equals("-profile")) {
34 String[] s2 = new String[s.length-1];
35 System.arraycopy(s, 1, s2, 0, s2.length);
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");
43 System.err.println("parsing " + s[1]);
44 res = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
46 Union testcasegrammar = Grammar.create(res, "s");
47 if (testcasegrammar==null) return;
48 CharParser parser = new CharParser(testcasegrammar);
51 System.out.println("\nready...");
55 long now = System.currentTimeMillis();
56 System.err.println("parsing " + s[2]);
57 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
59 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
61 System.out.println("\ndone");
65 System.err.println("expanding...");
67 TestCase[] expanded = (TestCase[])new GrammarBuilder("tests/", "").walkChildren(r2.expand1());
68 for(TestCase tc : expanded)
71 } catch (Throwable t) {
72 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
73 System.err.println(t);
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;
88 public TestCase(String name, String input, String[] output,
89 Union grammar, boolean tib, boolean jav) {
94 this.output = output==null ? new String[0] : output;
95 this.grammar = grammar;
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";
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;
111 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
112 : parser.parse(new StringReader(input));
113 } catch (ParseFailed pf) {
116 //ystem.out.println("res=="+res);
119 FileOutputStream fos = new FileOutputStream("out.dot");
120 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
121 GraphViz gv = new GraphViz();
126 System.out.println(parser);
129 HashSet<Tree<String>> results = new HashSet<Tree<String>>();
130 if (res != null) res.expand(results);
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();
139 System.out.print("\r \r");
141 HashSet<String> outs = new HashSet<String>();
142 if (output != null) for(String s : output) outs.add(s.trim());
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);
152 for(String s : outs) {
153 if (!bad) System.out.println(input);
154 System.out.print("\033[31m");
155 System.out.println("EXPECTED: " + s);
159 System.out.println("\033[0m");
162 System.out.println("\r\033[32mPASS\033[0m "+name);
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) {
171 if (tree.head()!=null) ret += tree.head();
172 ret += string(tree.children());
175 public static String string(Iterable<Tree<String>> children) {
177 for(Tree<String> t : children) ret += string(t);