Skip to content

Commit

Permalink
Compare IR before and after serialization test
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Nov 15, 2023
1 parent ef84e1d commit 95ff6a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ protected byte[] serialize(EnsoContext context, CachedModule entry) throws IOExc
}
}
}

var arr = Persistance.writeObject(entry.moduleIR());
return arr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected static Module parse(String code) throws IOException {
return ir;
}

static void assertIR(String msg, Module old, Module now) throws IOException {
public static void assertIR(String msg, Module old, Module now) throws IOException {
Function<IR, String> filter = f -> simplifyIR(f, true, true, false);
String ir1 = filter.apply(old);
String ir2 = filter.apply(now);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.enso.interpreter.caches;

import org.enso.compiler.CompilerTest;
import org.enso.compiler.core.IR;
import org.enso.interpreter.Constants;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.test.TestBase;
import org.enso.interpreter.util.ScalaConversions;
import org.enso.polyglot.CompilationStage;
import org.enso.polyglot.LanguageInfo;
import org.enso.polyglot.MethodNames;
import org.enso.polyglot.RuntimeOptions;
import org.graalvm.polyglot.Context;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.BeforeClass;

public class ModuleCacheTest extends TestBase {
private static Context ctx;

public ModuleCacheTest() {
}

@BeforeClass
public static void initializeContext() throws Exception {
ctx = defaultContextBuilder()
.option(RuntimeOptions.DISABLE_IR_CACHES, "true")
.build();
}

@Test
public void testCompareList() throws Exception {
var ensoCtx = (EnsoContext) ctx.getBindings(LanguageInfo.ID).invokeMember(MethodNames.TopScope.LEAK_CONTEXT).asHostObject();
var name = "Standard.Base.Data.List";

var v = ctx.eval("enso", """
import Standard.Base.Data.List
empty = List.List.Nil
""").invokeMember(MethodNames.Module.EVAL_EXPRESSION, "empty");
assertEquals("List", v.getMetaObject().getMetaSimpleName());

var option = ensoCtx.findModule(name);
assertTrue("Module found", option.isPresent());
var module = option.get();
var ir = module.getIr().duplicate(true, true, true, true);
for (var node : ScalaConversions.asJava(ir.preorder())) {
node.passData().prepareForSerialization(ensoCtx.getCompiler().context());
}
var cm = new ModuleCache.CachedModule(ir, CompilationStage.AFTER_CODEGEN, module.getSource());
byte[] arr = module.getCache().serialize(ensoCtx, cm);

var meta = new ModuleCache.Metadata("hash", "code", CompilationStage.AFTER_CODEGEN.toString());
var cachedIr = module.getCache().deserialize(ensoCtx, arr, meta, null);
assertNotNull("IR read", cachedIr);
CompilerTest.assertIR(name, module.getIr(), cachedIr.moduleIR());
}
}

0 comments on commit 95ff6a0

Please sign in to comment.