Skip to content

Commit

Permalink
Testing infinite list generator
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Apr 4, 2023
1 parent cb89ab6 commit d76cb6e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.NodeFactory;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.atom.LayoutSpec;
import org.enso.interpreter.node.callable.InvokeCallableNode;
import org.enso.interpreter.node.callable.dispatch.InvokeFunctionNode;
import org.enso.interpreter.node.expression.atom.InstantiateNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.callable.argument.ArgumentDefinition;
import org.enso.interpreter.runtime.callable.argument.CallArgumentInfo;
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.state.State;

/**
* This class mediates the use of {@link UnboxingAtom} instances. It is responsible for describing
Expand Down Expand Up @@ -165,7 +158,8 @@ public UnboxingAtom.FieldGetterNode[] buildGetters() {
for (int i = 0; i < fieldGetterFactories.length; i++) {
getters[i] = fieldGetterFactories[i].createNode();
if (args[i].isSuspended()) {
getters[i] = SuspendedFieldGetterNode.build(getters[i], buildSetter(i));
var setterOrNull = buildSetter(i);
getters[i] = SuspendedFieldGetterNode.build(getters[i], setterOrNull);
}
}
return getters;
Expand All @@ -188,7 +182,8 @@ public UnboxingAtom.FieldSetterNode getUncachedFieldSetter(int index) {
}

public UnboxingAtom.FieldSetterNode buildSetter(int index) {
return fieldSetterFactories[index].createNode();
var fieldSetterFactory = fieldSetterFactories[index];
return fieldSetterFactory == null ? null : fieldSetterFactory.createNode();
}

public boolean isDoubleAt(int fieldIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ public void evaluation() throws Exception {
assertEquals(log, 2, hellos);
}

@Test
public void testInfiniteListGenerator() throws Exception {
final String code = """
import Standard.Base.IO
type Lazy
Nil
Cons ~x ~xs
take self n = if n == 0 then Lazy.Nil else case self of
Lazy.Nil -> Lazy.Nil
Lazy.Cons x xs -> Lazy.Cons x (xs.take n-1)
sum self acc = case self of
Lazy.Nil -> acc
Lazy.Cons x xs -> @Tail_Call xs.sum acc+x
generator n = Lazy.Cons n (Lazy.generator n+1)
both n =
g = Lazy.generator 1
// IO.println "Generator is computed"
t = g.take n
// IO.println "Generator is taken"
t . sum 0
""";

var both = evalCode(code, "both");
var sum = both.execute(100);
String log = out.toString(StandardCharsets.UTF_8);
assertEquals(log, 5050, sum.asLong());
}

private Value evalCode(final String code, final String methodName) throws URISyntaxException {
final var testName = "test.enso";
final URI testUri = new URI("memory://" + testName);
Expand Down

0 comments on commit d76cb6e

Please sign in to comment.