--- /dev/null
+package org.simantics.scl.compiler.tests;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
+import org.simantics.scl.compiler.errors.CompilationErrorFormatter;
+import org.simantics.scl.compiler.module.repository.ImportFailure;
+import org.simantics.scl.compiler.module.repository.ImportFailureException;
+import org.simantics.scl.compiler.module.repository.ModuleRepository;
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.ExpressionEvaluator;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.Types;
+
+public class MemoryLeakTest {
+ ModuleRepository moduleRepository;
+
+ EnvironmentSpecification environmentSpecification;
+
+ @Before
+ public void initialize() throws Exception {
+ moduleRepository = InitialRepository.getInitialRepository();
+
+ // Environment for compiling expressions
+ environmentSpecification = new EnvironmentSpecification();
+ environmentSpecification.importModule("Builtin", "");
+ environmentSpecification.importModule("Prelude", "");
+ }
+
+ private void testExpression0(String expressionText,
+ Object expectedValue,
+ Type expectedType) throws Exception {
+
+ RuntimeEnvironment runtimeEnvironment;
+ try {
+ runtimeEnvironment = moduleRepository.createRuntimeEnvironment(environmentSpecification,
+ getClass().getClassLoader());
+ } catch(ImportFailureException e) {
+ for(ImportFailure failure : e.failures)
+ System.err.println("Failed to import " + failure.moduleName);
+ throw e;
+ }
+
+ // Compiling and running expression
+ try {
+ Object result = new ExpressionEvaluator(runtimeEnvironment, expressionText)
+ .expectedType(expectedType)
+ .interpretIfPossible(false)
+ .eval();
+ if(expectedValue != null)
+ Assert.assertEquals(expectedValue, result);
+ } catch(SCLExpressionCompilationException e) {
+ System.out.println(CompilationErrorFormatter.toString(expressionText, e.getErrors()));
+ throw e;
+ }
+ }
+
+ @Test
+ public void testIt() throws Exception {
+ for(int i=0;i<1000000;++i) {
+ testExpression0("fst (\"a\", \"b\")", "a", Types.STRING);
+ if(i % 10000 == 0 && i > 0) {
+ System.gc();
+ System.runFinalization();
+ System.gc();
+ Thread.sleep(100L);
+ double used = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
+ System.out.println(i + " " + used*1e-6 + " Mb, " + (used / i) + " b / expression");
+ }
+ }
+ }
+}