add serialize/deserialize to regression test
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
index 0848ce8..eae22d1 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" });
@@ -39,13 +47,23 @@ public class RegressionTests {
                 s[0] == null
                 ? RegressionTests.class.getClassLoader().getResourceAsStream("edu/berkeley/sbp/meta/meta.g")
                 : new FileInputStream(s[0]);
-            Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(metaGrammarStream).expand1();
-            Union meta = GrammarAST.buildFromAST(res, "s", includes);
+            Tree<String> res = new CharParser(GrammarAST.getMetaGrammar()).parse(metaGrammarStream).expand1();
+            Union meta = GrammarAST.buildFromAST(res, "s", resolver);
+            CharParser 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);
 
@@ -76,7 +94,7 @@ public class RegressionTests {
                 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));