1 // Copyright 2006-2007 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.meta.*;
9 import edu.berkeley.sbp.chr.*;
10 import edu.berkeley.sbp.util.*;
12 public class RegressionTests {
14 public static boolean yes = false;
15 public static boolean graph = false;
16 public static GrammarAST.ImportResolver resolver = new GrammarAST.ImportResolver() {
17 public InputStream getImportStream(String importname) {
19 return new FileInputStream("tests/"+importname);
20 } catch (IOException e) {
21 throw new RuntimeException(e);
26 public static void main() throws Exception {
27 main(new String[] { null, "tests/testcase.g", "tests/regression.tc" });
30 public static void main(String[] s) throws Exception {
32 boolean profile = false;
33 if (s[0].equals("-graph")) {
35 String[] s2 = new String[s.length-1];
36 System.arraycopy(s, 1, s2, 0, s2.length);
39 if (s[0].equals("-profile")) {
41 String[] s2 = new String[s.length-1];
42 System.arraycopy(s, 1, s2, 0, s2.length);
46 InputStream metaGrammarStream =
48 ? RegressionTests.class.getClassLoader().getResourceAsStream("edu/berkeley/sbp/meta/meta.g")
49 : new FileInputStream(s[0]);
50 Tree<String> res = new CharParser(GrammarAST.getMetaGrammar()).parse(metaGrammarStream).expand1();
51 Union meta = GrammarAST.buildFromAST(res, "s", resolver);
52 CharParser cp = new CharParser(meta);
54 System.err.println("serializing grammar to grammar.ser...");
55 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("grammar.ser"));
58 System.err.println("deserializing grammar from grammar.ser...");
59 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("grammar.ser"));
60 cp = (CharParser)ois.readObject();
63 System.err.println("parsing " + s[1]);
64 res = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
66 Union testcasegrammar = GrammarAST.buildFromAST(res, "s", resolver);
67 if (testcasegrammar==null) return;
68 CharParser parser = new CharParser(testcasegrammar);
71 System.out.println("\nready...");
75 long now = System.currentTimeMillis();
76 System.err.println("parsing " + s[2]);
77 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
79 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
81 System.out.println("\ndone");
85 System.err.println("expanding...");
87 ArrayList<TestCase> cases = new ArrayList<TestCase>();
88 Tree tt = r2.expand1();
89 for(int i=0; i<tt.size(); i++) {
91 String[] expect = new String[t.child(2).size()];
92 for(int j=0; j<t.child(2).size(); j++)
93 expect[j] = stringifyChildren(t.child(2).child(j));
94 cases.add(new TestCase(stringifyChildren(t.child(0)),
95 stringifyChildren(t.child(1)),
97 GrammarAST.buildFromAST(t.child(3), "s", resolver),
102 TestCase[] expanded = new TestCase[cases.size()];
103 for(int i=0; i<expanded.length; i++)
104 expanded[i] = cases.get(i);
105 for(TestCase tc : expanded)
108 } catch (Throwable t) {
109 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
110 System.err.println(t);
111 System.err.println();
113 System.err.println();
117 private static String stringifyChildren(Tree t) {
118 StringBuffer sb = new StringBuffer();
119 for(int i=0; i<t.size(); i++) {
120 sb.append(t.child(i).head());
121 sb.append(stringifyChildren(t.child(i)));
123 return sb.toString();
126 public static class TestCase {
127 private final boolean jav;
128 public /*final*/ String input;
129 public final String[] output;
130 public final Union grammar;
131 public final String name;
133 public TestCase(String name, String input, String[] output,
134 Union grammar, boolean tib, boolean jav) {
138 this.output = output==null ? new String[0] : output;
139 this.grammar = grammar;
141 public String toString() {
142 String ret = "testcase {\n" + " input \""+input+"\";\n";
143 for(String s : output) ret += " output \""+s+"\";\n";
144 ret += grammar +"\n}\n";
147 public boolean execute() throws Exception {
148 Forest<String> res = null;
149 ParseFailed pfe = null;
150 CharParser parser = new CharParser(grammar);
151 System.out.print(" "+name+"\r");
153 res = parser.parse(new StringReader(input));
154 } catch (ParseFailed pf) { pfe = pf; }
157 FileOutputStream fos = new FileOutputStream("out.dot");
158 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
159 GraphViz gv = new GraphViz();
164 System.out.println(parser);
167 Iterable<Tree<String>> results =
168 res==null ? new HashSet<Tree<String>>() : res.expand();
170 System.out.print("\r");
171 if (results == null || (!results.iterator().hasNext() && (output!=null && output.length > 0))) {
172 System.out.print("\033[31m");
173 System.out.print("FAIL ");
174 System.out.println("\033[0m "+name);
175 if (pfe != null) pfe.printStackTrace();
177 System.out.print("\r \r");
179 HashSet<String> outs = new HashSet<String>();
180 if (output != null) for(String s : output) outs.add(s.trim());
182 for (Tree<String> r : results) {
183 String s = r.toString().trim();
184 if (outs.contains(s)) { outs.remove(s); continue; }
185 if (!bad) System.out.println(input);
186 System.out.print("\033[33m");
187 System.out.println(" GOT: " + s);
190 for(String s : outs) {
191 if (!bad) System.out.println(input);
192 System.out.print("\033[31m");
193 System.out.println("EXPECTED: " + s);
197 System.out.println("\033[0m");
200 System.out.println("\r\033[32mPASS\033[0m "+name);
206 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
207 public static String string(Tree<String> tree) {
209 if (tree.head()!=null) ret += tree.head();
210 ret += string(tree.children());
213 public static String string(Iterable<Tree<String>> children) {
215 for(Tree<String> t : children) ret += string(t);