From 95ff6a06b0fa0067a6d03b63b5be133874d2682d Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 15 Nov 2023 11:28:20 +0100 Subject: [PATCH] Compare IR before and after serialization test --- .../enso/interpreter/caches/ModuleCache.java | 1 - .../java/org/enso/compiler/CompilerTest.java | 2 +- .../interpreter/caches/ModuleCacheTest.java | 61 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 engine/runtime/src/test/java/org/enso/interpreter/caches/ModuleCacheTest.java diff --git a/engine/runtime/src/main/java/org/enso/interpreter/caches/ModuleCache.java b/engine/runtime/src/main/java/org/enso/interpreter/caches/ModuleCache.java index 2d1c6d02f75c..3db90aeeeebc 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/caches/ModuleCache.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/caches/ModuleCache.java @@ -147,7 +147,6 @@ protected byte[] serialize(EnsoContext context, CachedModule entry) throws IOExc } } } - var arr = Persistance.writeObject(entry.moduleIR()); return arr; } diff --git a/engine/runtime/src/test/java/org/enso/compiler/CompilerTest.java b/engine/runtime/src/test/java/org/enso/compiler/CompilerTest.java index 7a426deb68ed..b4d3e0850e00 100644 --- a/engine/runtime/src/test/java/org/enso/compiler/CompilerTest.java +++ b/engine/runtime/src/test/java/org/enso/compiler/CompilerTest.java @@ -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 filter = f -> simplifyIR(f, true, true, false); String ir1 = filter.apply(old); String ir2 = filter.apply(now); diff --git a/engine/runtime/src/test/java/org/enso/interpreter/caches/ModuleCacheTest.java b/engine/runtime/src/test/java/org/enso/interpreter/caches/ModuleCacheTest.java new file mode 100644 index 000000000000..9bdb97db6fe1 --- /dev/null +++ b/engine/runtime/src/test/java/org/enso/interpreter/caches/ModuleCacheTest.java @@ -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()); + } +} \ No newline at end of file