Skip to content

Commit

Permalink
#623 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 25, 2022
1 parent 5cb98fd commit 5cc3b2c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 86 deletions.
19 changes: 14 additions & 5 deletions eo-runtime/src/main/java/org/eolang/PhDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,20 @@ public final Attr attr(final int pos) {
)
);
}
final int idx;
if (pos >= this.order.size()) {
idx = this.order.size() - 1;
} else {
idx = pos;
int idx;
for (idx = 0; idx < pos; ++idx) {
if (idx >= this.order.size()) {
throw new ExFailure(
String.format(
"There are just %d attributes here, can't read the %d-th one",
this.order.size(), pos
)
);
}
final String name = this.order.get(idx);
if (this.attrs.get(name) instanceof AtVararg) {
break;
}
}
return this.attr(this.order.get(idx));
}
Expand Down
89 changes: 89 additions & 0 deletions eo-runtime/src/test/java/org/eolang/AtVarargTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,93 @@ public void appendsElements() {
);
}

@Test
public void injectsVarargs() {
final Phi bar = new AtVarargTest.Bar(Phi.);
bar.attr(0).put(new Data.ToPhi(1L));
bar.attr(1).put(new Data.ToPhi(1L));
MatcherAssert.assertThat(
new Dataized(
bar.attr("a").get()
).take(Long.class),
Matchers.is(1L)
);
}

@Test
public void datarizesEOappCallingVarargsFunc() {
MatcherAssert.assertThat(
new Dataized(
new AtVarargTest.Foo(Phi.)
).take(Boolean.class),
Matchers.is(true)
);
}

/**
* Main program sample.
*
* {@code
* [] > foo
* (bar 1 2 3).eq 2 > @
* }
*
* @since 0.22
*/
private static class Foo extends PhDefault {
Foo(final Phi sigma) {
super(sigma);
this.add(
"蠁",
new AtOnce(
new AtComposite(
this,
rho -> {
Phi phi = new PhWith(
new PhCopy(new AtVarargTest.Bar(rho)),
0, new Data.ToPhi(1L)
);
phi = new PhWith(phi, 1, new Data.ToPhi(2L));
phi = new PhWith(phi, 2, new Data.ToPhi(3L));
return new PhWith(
new PhCopy(new PhMethod(phi, "eq")),
0, new Data.ToPhi(2L)
);
}
)
)
);
}
}

/**
* Varargs function sample.
*
* {@code
* [args...] > bar
* 1 > a
* 2 > @
* }
*
* @since 0.22
*/
private static class Bar extends PhDefault {
Bar(final Phi sigma) {
super(sigma);
this.add("args", new AtVararg());
this.add(
"a",
new AtOnce(
new AtComposite(this, rho -> new Data.ToPhi(1L))
)
);
this.add(
"蠁",
new AtOnce(
new AtComposite(this, rho -> new Data.ToPhi(2L))
)
);
}
}

}
81 changes: 0 additions & 81 deletions eo-runtime/src/test/java/org/eolang/DataizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.logging.Logger;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
Expand Down Expand Up @@ -77,84 +76,4 @@ public void close() throws SecurityException {
);
}

/**
* Try to datarize an EO app that calls a varargs func.
* @todo #414:30min Fix bug execute an EO program calling a varargs func.
* We reproduced by a test bug during execution with exception `You can't overwrite X`
* when EO app try to call a function that uses varargs as parameter. Now, we must fix it
* and disable the test (test below).
*/
@Test
@Disabled
public void datarizesEOappCallingVarargsFunc() {
MatcherAssert.assertThat(
new Dataized(
new EOappCallingVarargsFunc(Phi.)
).take(Long.class),
Matchers.is(2L)
);
}

/**
* Main program sample.
* {@code
* [] > app
* (f 1 2 3).eq 2 > @
* }
* @since 0.22
*/
private static class EOappCallingVarargsFunc extends PhDefault {
public EOappCallingVarargsFunc(Phi sigma) {
super(sigma);
this.add(
"蠁",
new AtOnce(
new AtComposite(
this,
(rho) -> {
Phi fvar = new PhCopy(new DataizedTest.Fvarargs(rho));
Phi var1 = new Data.ToPhi(1L);
Phi var2 = new Data.ToPhi(2L);
Phi var3 = new Data.ToPhi(3L);
Phi fvard = new PhWith(fvar, 0, var1);
fvard = new PhWith(fvard, 1, var2);
fvard = new PhWith(fvard, 2, var3);
return new PhWith(
new PhCopy(new PhMethod(fvard, "eq")), 0, new Data.ToPhi(2L)
);
}
)
)
);
}
}

/**
* Varargs function sample.
* {@code
* [args] > f
* 1 > a
* 2 > @
* }
* @since 0.22
*/
@SuppressWarnings("unchecked")
private static class Fvarargs extends PhDefault {
public Fvarargs(final Phi sigma) {
super(sigma);
this.add("args", new AtVararg());
this.add(
"a",
new AtOnce(
new AtComposite(this, (rho) -> new Data.ToPhi(1L))
)
);
this.add(
"蠁",
new AtOnce(
new AtComposite(this, (rho) -> new Data.ToPhi(2L))
)
);
}
}
}

1 comment on commit 5cc3b2c

@0pdd
Copy link

@0pdd 0pdd commented on 5cc3b2c May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 414-aa62591c disappeared from eo-runtime/src/test/java/org/eolang/DataizedTest.java, that's why I closed #623. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.