Skip to content

Commit

Permalink
Implement split tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Aug 4, 2022
1 parent 054ab45 commit 76013d4
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gov.nasa.jpl.aerie.constraints.json;

import gov.nasa.jpl.aerie.constraints.time.Spans;
import gov.nasa.jpl.aerie.constraints.tree.All;
import gov.nasa.jpl.aerie.constraints.tree.Changes;
import gov.nasa.jpl.aerie.constraints.tree.DiscreteParameter;
Expand All @@ -23,6 +22,7 @@
import gov.nasa.jpl.aerie.constraints.tree.RealParameter;
import gov.nasa.jpl.aerie.constraints.tree.RealResource;
import gov.nasa.jpl.aerie.constraints.tree.RealValue;
import gov.nasa.jpl.aerie.constraints.tree.Split;
import gov.nasa.jpl.aerie.constraints.tree.SpansFromWindows;
import gov.nasa.jpl.aerie.constraints.tree.StartOf;
import gov.nasa.jpl.aerie.constraints.tree.Times;
Expand Down Expand Up @@ -493,6 +493,57 @@ public void testParseInvert() {
assertEquivalent(expected, result);
}

@Test
public void testParseSplitWindows() {
final var json = Json
.createObjectBuilder()
.add("kind", "IntervalsExpressionSplit")
.add("intervals", Json
.createObjectBuilder()
.add("kind", "WindowsExpressionActivityWindow")
.add("alias", "A"))
.add("numberOfSubIntervals", 3)
.build();

final var result = windowsExpressionP.parse(json).getSuccessOrThrow();

final var expected =
new Split<>(
new ActivityWindow("A"),
3
);

assertEquivalent(expected, result);
}

@Test
public void testParseSplitSpans() {
final var json = Json
.createObjectBuilder()
.add("kind", "IntervalsExpressionSplit")
.add("intervals", Json.createObjectBuilder()
.add("kind", "SpansExpressionFromWindows")
.add("windowsExpression", Json
.createObjectBuilder()
.add("kind", "WindowsExpressionActivityWindow")
.add("alias", "A"))
)
.add("numberOfSubIntervals", 3)
.build();

final var result = spansExpressionP.parse(json).getSuccessOrThrow();

final var expected =
new Split<>(
new SpansFromWindows(
new ActivityWindow("A")
),
3
);

assertEquivalent(expected, result);
}

@Test
public void testForEachActivity() {
final var json = Json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gov.nasa.jpl.aerie.constraints.tree;

import gov.nasa.jpl.aerie.constraints.InputMismatchException;
import gov.nasa.jpl.aerie.constraints.UnsplittableIntervalException;
import gov.nasa.jpl.aerie.constraints.model.ActivityInstance;
import gov.nasa.jpl.aerie.constraints.model.DiscreteProfile;
import gov.nasa.jpl.aerie.constraints.model.DiscreteProfilePiece;
import gov.nasa.jpl.aerie.constraints.model.LinearProfile;
import gov.nasa.jpl.aerie.constraints.model.LinearProfilePiece;
import gov.nasa.jpl.aerie.constraints.model.SimulationResults;
import gov.nasa.jpl.aerie.constraints.model.Violation;
import gov.nasa.jpl.aerie.constraints.time.Spans;
import gov.nasa.jpl.aerie.constraints.time.Window;
import gov.nasa.jpl.aerie.constraints.time.Windows;
import gov.nasa.jpl.aerie.merlin.protocol.types.Duration;
Expand All @@ -21,8 +23,9 @@
import static gov.nasa.jpl.aerie.constraints.Assertions.assertEquivalent;
import static gov.nasa.jpl.aerie.constraints.time.Window.Inclusivity.Exclusive;
import static gov.nasa.jpl.aerie.constraints.time.Window.Inclusivity.Inclusive;
import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.MICROSECOND;
import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.MICROSECONDS;
import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.SECONDS;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

public class ASTTests {
Expand Down Expand Up @@ -50,6 +53,76 @@ public void testNot() {
assertEquivalent(expected, result);
}

@Test
public void testSplitWindows() {
final var simResults = new SimulationResults(
Window.between(0, 20, SECONDS),
List.of(),
Map.of(),
Map.of()
);

final var windows = new Windows();
windows.add(Window.between(0, Inclusive, 5, Exclusive, SECONDS));
windows.add(Window.between(10000000, Exclusive, 15000001, Exclusive, MICROSECONDS));

final var result = new Split<Windows>(Supplier.of(windows), 3).evaluate(simResults, Map.of());

final var expected = new Windows();
expected.add(Window.between(0, Inclusive, 1666666, Exclusive, MICROSECONDS));
expected.add(Window.between(1666666, Exclusive, 3333332, Exclusive, MICROSECONDS));
expected.add(Window.between(3333332, Exclusive, 5000000, Exclusive, MICROSECONDS));

expected.add(Window.between(10000000, Exclusive, 11666667, Exclusive, MICROSECONDS));
expected.add(Window.between(11666667, Exclusive, 13333334, Exclusive, MICROSECONDS));
expected.add(Window.between(13333334, Exclusive, 15000001, Exclusive, MICROSECONDS));

assertEquivalent(expected, result);
}

@Test
public void testSplitSpans() {
final var simResults = new SimulationResults(
Window.between(0, 20, SECONDS),
List.of(),
Map.of(),
Map.of()
);

final var spans = new Spans();
spans.add(Window.between(0, Inclusive, 5, Exclusive, SECONDS));
spans.add(Window.between(0, Exclusive, 5000001, Exclusive, MICROSECONDS));

final var result = new Split<Spans>(Supplier.of(spans), 3).evaluate(simResults, Map.of());

final var expected = new Spans();
expected.add(Window.between(0, Inclusive, 1666666, Exclusive, MICROSECONDS));
expected.add(Window.between(1666666, Exclusive, 3333332, Exclusive, MICROSECONDS));
expected.add(Window.between(3333332, Exclusive, 5000000, Exclusive, MICROSECONDS));

expected.add(Window.between(0, Exclusive, 1666667, Exclusive, MICROSECONDS));
expected.add(Window.between(1666667, Exclusive, 3333334, Exclusive, MICROSECONDS));
expected.add(Window.between(3333334, Exclusive, 5000001, Exclusive, MICROSECONDS));

assertEquivalent(expected, result);
}

@Test
public void testUnsplittableInterval() {
final var simResults = new SimulationResults(
Window.between(0, 20, SECONDS),
List.of(),
Map.of(),
Map.of()
);

final var spans = new Spans(
Window.at(5, SECONDS)
);

assertThrows(UnsplittableIntervalException.class, () -> new Split<Spans>(Supplier.of(spans), 3).evaluate(simResults, Map.of()));
}

@Test
public void testAnd() {
final var simResults = new SimulationResults(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,78 @@ export default () => {
);
}

@Test
void testSplit() {
checkSuccessfulCompilation(
"""
export default () => {
return Real.Resource("state of charge").lessThan(0.3).split(4)
}
""",
new ViolationsOf(
new Split<>(
new LessThan(new RealResource("state of charge"), new RealValue(0.3)),
4
)
)
);

checkSuccessfulCompilation(
"""
export default () => {
return Real.Resource("state of charge").lessThan(0.3).spans().split(4).windows()
}
""",
new ViolationsOf(
new WindowsFromSpans(
new Split<>(
new SpansFromWindows(new LessThan(new RealResource("state of charge"), new RealValue(0.3))),
4
)
)
)
);
}

@Test
void testSplitArgumentError() {
checkFailedCompilation(
"""
export default () => {
return Real.Resource("state of charge").lessThan(0.3).split(0)
}
""",
".split numberOfSubWindows cannot be less than 1, but was: 0"
);

checkFailedCompilation(
"""
export default () => {
return Real.Resource("state of charge").lessThan(0.3).split(-2)
}
""",
".split numberOfSubWindows cannot be less than 1, but was: -2"
);

checkFailedCompilation(
"""
export default () => {
return Real.Resource("state of charge").lessThan(0.3).spans().split(0).windows()
}
""",
".split numberOfSubSpans cannot be less than 1, but was: 0"
);

checkFailedCompilation(
"""
export default () => {
return Real.Resource("state of charge").lessThan(0.3).spans().split(-2).windows()
}
""",
".split numberOfSubSpans cannot be less than 1, but was: -2"
);
}

@Test
void testViolations() {
checkSuccessfulCompilation(
Expand Down

0 comments on commit 76013d4

Please sign in to comment.