preliminary support for serialization of parse tables
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
index 33e17ac..dfe30b6 100644 (file)
@@ -13,7 +13,15 @@ public class RegressionTests {
 
     public static boolean yes = false;
     public static boolean graph = false;
-    public static File[] includes = new File[] { new File("tests") };
+    public static GrammarAST.ImportResolver resolver = new GrammarAST.ImportResolver() {
+            public InputStream getImportStream(String importname) {
+                try {
+                    return new FileInputStream("tests/"+importname);
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        };
 
     public static void main() throws Exception {
         main(new String[] { null, "tests/testcase.g", "tests/regression.tc" });
@@ -35,17 +43,31 @@ public class RegressionTests {
                 s = s2;
             }
 
+            CharParser cp;
+            Tree<String> res;
+
             InputStream metaGrammarStream =
                 s[0] == null
                 ? RegressionTests.class.getClassLoader().getResourceAsStream("edu/berkeley/sbp/meta/meta.g")
                 : new FileInputStream(s[0]);
-            Tree<String> res = new CharParser(GrammarAST.getMetaGrammar()).parse(metaGrammarStream).expand1();
-            Union meta = GrammarAST.buildFromAST(res, "s", includes);
+            res = new CharParser(GrammarAST.getMetaGrammar()).parse(metaGrammarStream).expand1();
+            Union meta = GrammarAST.buildFromAST(res, "s", resolver);
+            cp = new CharParser(meta);
+
+            System.err.println("serializing grammar to grammar.ser...");
+            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("grammar.ser"));
+            oos.writeObject(cp);
+            oos.close();
+
+            System.err.println("deserializing grammar from grammar.ser...");
+            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("grammar.ser"));
+            cp = (CharParser)ois.readObject();
+            ois.close();
 
             System.err.println("parsing " + s[1]);
             res = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
 
-            Union testcasegrammar = GrammarAST.buildFromAST(res, "s", includes);
+            Union testcasegrammar = GrammarAST.buildFromAST(res, "s", resolver);
             if (testcasegrammar==null) return;
             CharParser parser = new CharParser(testcasegrammar);
 
@@ -70,13 +92,14 @@ public class RegressionTests {
             Tree tt = r2.expand1();
             for(int i=0; i<tt.size(); i++) {
                 Tree t = tt.child(i);
-                String[] expect = new String[t.child(2).size()];
-                for(int j=0; j<t.child(2).size(); j++)
-                    expect[j] = stringifyChildren(t.child(2).child(j));
+                String[] expect = !"ignore output;".equals(t.child(2).head()) ? new String[t.child(2).size()] : null;
+                if (expect != null)
+                    for(int j=0; j<t.child(2).size(); j++)
+                        expect[j] = stringifyChildren(t.child(2).child(j));
                 cases.add(new TestCase(stringifyChildren(t.child(0)),
                                        stringifyChildren(t.child(1)),
                                        expect,
-                                       GrammarAST.buildFromAST(t.child(3), "s", includes),
+                                       GrammarAST.buildFromAST(t.child(3), "s", resolver),
                                        false,
                                        false));
                 
@@ -117,12 +140,13 @@ public class RegressionTests {
             this.name = name;
             this.jav = jav;
             this.input = input;
-            this.output = output==null ? new String[0] : output;
+            this.output = output;
             this.grammar = grammar;
         }
         public String toString() {
             String ret = "testcase {\n" + "  input \""+input+"\";\n";
-            for(String s : output) ret += "  output \""+s+"\";\n";
+            if (output != null)
+                for(String s : output) ret += "  output \""+s+"\";\n";
             ret += grammar +"\n}\n";
             return ret;
         }
@@ -146,6 +170,10 @@ public class RegressionTests {
                 System.out.println(parser);
             }
 
+            if (output==null) {
+                System.out.println("\r\033[32mDONE\033[0m "+name);
+                return true;
+            }
             Iterable<Tree<String>> results =
                 res==null ? new HashSet<Tree<String>>() : res.expand();