Skip to content

Commit

Permalink
Use Natural instead of Int
Browse files Browse the repository at this point in the history
  • Loading branch information
asmirnov-backend committed Feb 9, 2025
1 parent 5300996 commit 2f3fe54
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public final class EOmalloc$EOof$EOallocated$EOread extends PhDefault implements
public Phi lambda() {
return new Data.ToPhi(
Heaps.INSTANCE.read(
new Expect.Int(Expect.at(this.take(Attr.RHO), "id")).it(),
new Expect.Int(Expect.at(this, "offset")).it(),
new Expect.Int(Expect.at(this, "length")).it()
new Expect.Natural(Expect.at(this.take(Attr.RHO), "id")).it(),
new Expect.Natural(Expect.at(this, "offset")).it(),
new Expect.Natural(Expect.at(this, "length")).it()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public final class EOmalloc$EOof$EOallocated$EOresized extends PhDefault impleme
@Override
public Phi lambda() {
final Phi rho = this.take(Attr.RHO);
final int id = new Expect.Int(Expect.at(rho, "id")).it();
final int size = new Expect.Int(Expect.at(this, "new-size")).it();
final int id = new Expect.Natural(Expect.at(rho, "id")).it();
final int size = new Expect.Natural(Expect.at(this, "new-size")).it();
Heaps.INSTANCE.resize(id, size);
return rho;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class EOmalloc$EOof$EOallocated$EOsize extends PhDefault implements
public Phi lambda() {
return new Data.ToPhi(
Heaps.INSTANCE.size(
new Expect.Int(Expect.at(this.take(Attr.RHO), "id")).it()
new Expect.Natural(Expect.at(this.take(Attr.RHO), "id")).it()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public final class EOmalloc$EOof$EOallocated$EOwrite extends PhDefault implement
@Override
public Phi lambda() {
Heaps.INSTANCE.write(
new Expect.Int(Expect.at(this.take(Attr.RHO), "id")).it(),
new Expect.Int(Expect.at(this, "offset")).it(),
new Expect.Natural(Expect.at(this.take(Attr.RHO), "id")).it(),
new Expect.Natural(Expect.at(this, "offset")).it(),
new Dataized(this.take("data")).take()
);
return new Data.ToPhi(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class EOmalloc$EOof$EOφ extends PhDefault implements Atom {
public Phi lambda() {
final Phi rho = this.take(Attr.RHO);
final int identifier = Heaps.INSTANCE.malloc(
this, new Expect.Int(Expect.at(rho, "size")).it()
this, new Expect.Natural(Expect.at(rho, "size")).it()
);
final Phi res;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ void throwsCorrectErrorForLengthAttrNaN() {
() -> new Dataized(
new PhWith(
new PhWith(
new EOmallocTest.PhiWithDummyId(new EOmalloc$EOof$EOallocated$EOread()).it(),
new EOmallocTest.PhiWithIdDummy(
new EOmalloc$EOof$EOallocated$EOread()
).it(),
"offset",
new Data.ToPhi(42)
),
Expand All @@ -78,7 +80,9 @@ void throwsCorrectErrorForLengthAttrNotAnInt() {
() -> new Dataized(
new PhWith(
new PhWith(
new EOmallocTest.PhiWithDummyId(new EOmalloc$EOof$EOallocated$EOread()).it(),
new EOmallocTest.PhiWithIdDummy(
new EOmalloc$EOof$EOallocated$EOread()
).it(),
"offset",
new Data.ToPhi(42)
),
Expand All @@ -92,4 +96,29 @@ void throwsCorrectErrorForLengthAttrNotAnInt() {
);
}

@Test
void throwsCorrectErrorForLengthAttrNotNatural() {
MatcherAssert.assertThat(
"the message in the error is correct",
Assertions.assertThrows(
ExAbstract.class,
() -> new Dataized(
new PhWith(
new PhWith(
new EOmallocTest.PhiWithIdDummy(
new EOmalloc$EOof$EOallocated$EOread()
).it(),
"offset",
new Data.ToPhi(42)
),
"length",
new Data.ToPhi(-42)
)
).take(),
"put negative int in natural attr fails with a proper message that explains what happened"
).getMessage(),
Matchers.equalTo("the 'length' attribute (-42) must be greater or equal to zero")
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ void throwsCorrectErrorForNewSizeAttrNaN() {
ExAbstract.class,
() -> new Dataized(
new PhWith(
new EOmallocTest.PhiWithDummyId(new EOmalloc$EOof$EOallocated$EOresized()).it(),
new EOmallocTest.PhiWithIdDummy(
new EOmalloc$EOof$EOallocated$EOresized()
).it(),
"new-size",
new Data.ToPhi(true)
)
Expand All @@ -73,7 +75,9 @@ void throwsCorrectErrorForNewSizeAttrNotAnInt() {
ExAbstract.class,
() -> new Dataized(
new PhWith(
new EOmallocTest.PhiWithDummyId(new EOmalloc$EOof$EOallocated$EOresized()).it(),
new EOmallocTest.PhiWithIdDummy(
new EOmalloc$EOof$EOallocated$EOresized()
).it(),
"new-size",
new Data.ToPhi(42.42)
)
Expand All @@ -84,4 +88,25 @@ void throwsCorrectErrorForNewSizeAttrNotAnInt() {
);
}

@Test
void throwsCorrectErrorForNewSizeAttrNotNatural() {
MatcherAssert.assertThat(
"the message in the error is correct",
Assertions.assertThrows(
ExAbstract.class,
() -> new Dataized(
new PhWith(
new EOmallocTest.PhiWithIdDummy(
new EOmalloc$EOof$EOallocated$EOresized()
).it(),
"new-size",
new Data.ToPhi(-42)
)
).take(),
"put negative int in natural attr fails with a proper message that explains what happened"
).getMessage(),
Matchers.equalTo("the 'new-size' attribute (-42) must be greater or equal to zero")
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

// @checkstyle MissingDeprecatedCheck (10 lines)
// @checkstyle AtclauseOrderCheck (10 lines)

/**
* Test case for {@link EOmalloc$EOof$EOφ}.
*
Expand Down Expand Up @@ -95,8 +98,33 @@ void throwsCorrectErrorForSizeAttrNotAnInt() {
);
}

@Test
void throwsCorrectErrorForSizeAttrNotNatural() {
MatcherAssert.assertThat(
"the message in the error is correct",
Assertions.assertThrows(
ExAbstract.class,
() -> new Dataized(
new PhWith(
new EOmalloc$EOof$EOφ(),
Attr.RHO,
new PhWith(
new EOmalloc$EOof$EOφTest.SizeDummy(),
"size",
new Data.ToPhi(-42)
)
)
).take(),
"put negative int in natural attr fails with a proper message that explains what happened"
).getMessage(),
Matchers.equalTo("the 'size' attribute (-42) must be greater or equal to zero")
);
}

/**
* Dummy with size attr.
*
* @since 0.52
*/
private static class SizeDummy extends PhDefault {
/**
Expand Down
42 changes: 36 additions & 6 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOmallocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ void throwsCorrectErrorForOffsetAttrNaN(final Class<?> cls) {
ExAbstract.class,
() -> new Dataized(
new PhWith(
new PhiWithDummyId((Phi) cls.getDeclaredConstructor().newInstance()).it(),
new PhiWithIdDummy(
(Phi) cls.getDeclaredConstructor().newInstance()
).it(),
"offset",
new Data.ToPhi(true)
)
Expand All @@ -182,7 +184,9 @@ void throwsCorrectErrorForOffsetAttrNotAnInt(final Class<?> cls) {
ExAbstract.class,
() -> new Dataized(
new PhWith(
new PhiWithDummyId((Phi) cls.getDeclaredConstructor().newInstance()).it(),
new PhiWithIdDummy(
(Phi) cls.getDeclaredConstructor().newInstance()
).it(),
"offset",
new Data.ToPhi(42.42)
)
Expand All @@ -193,6 +197,31 @@ void throwsCorrectErrorForOffsetAttrNotAnInt(final Class<?> cls) {
);
}

@ParameterizedTest
@ValueSource(classes = {
EOmalloc$EOof$EOallocated$EOread.class,
EOmalloc$EOof$EOallocated$EOwrite.class
})
void throwsCorrectErrorForOffsetAttrNotNatural(final Class<?> cls) {
MatcherAssert.assertThat(
"the message in the error is correct",
Assertions.assertThrows(
ExAbstract.class,
() -> new Dataized(
new PhWith(
new PhiWithIdDummy(
(Phi) cls.getDeclaredConstructor().newInstance()
).it(),
"offset",
new Data.ToPhi(-42)
)
).take(),
"put negative int in natural attr fails with a proper message that explains what happened"
).getMessage(),
Matchers.equalTo("the 'offset' attribute (-42) must be greater or equal to zero")
);
}

/**
* Allocated data.
* @param obj Init object
Expand Down Expand Up @@ -242,17 +271,18 @@ private static class Dummy extends PhDefault {
*
* @since 0.52
*/
static class PhiWithDummyId {
@SuppressWarnings("PMD.ShortMethodName")
static class PhiWithIdDummy {
/**
* Phi
* Phi.
*/
private final Phi phi;

/**
* Ctor.
* @param phi Phi
*/
PhiWithDummyId(Phi phi) {
PhiWithIdDummy(final Phi phi) {
this.phi = phi;
}

Expand All @@ -262,7 +292,7 @@ static class PhiWithDummyId {
*/
public Phi it() {
return new PhWith(
phi,
this.phi,
Attr.RHO,
new PhWith(
new EOmallocTest.IdDummy(),
Expand Down

0 comments on commit 2f3fe54

Please sign in to comment.