Skip to content

Commit

Permalink
fix sequential and parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
GramGra07 committed Oct 12, 2024
1 parent d2e5f48 commit 3808d64
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 4 deletions.
Binary file modified .gradle/7.5/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.5/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.5/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.5/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.gentrifiedApps.statemachineftc;

import android.util.Pair;

import org.junit.jupiter.api.DisplayNameGenerator;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -83,6 +79,11 @@ public ParallelRunSM.Builder<T> stopRunning(T state, Supplier<Boolean> exitComma

public ParallelRunSM<T> build(Boolean useTimeout, Integer timeout) {
this.timeout = new AbstractMap.SimpleEntry<>(useTimeout, timeout);

if (timeout < 0) {
throw new IllegalArgumentException("Timeout must be a positive integer");
}

if (states == null || states.isEmpty()) {
throw new IllegalArgumentException("States cannot be null or empty");
}
Expand All @@ -91,6 +92,13 @@ public ParallelRunSM<T> build(Boolean useTimeout, Integer timeout) {
throw new IllegalArgumentException("States cannot have duplicates");
}

if (onEnterCommands.isEmpty()) {
throw new IllegalArgumentException("States must have corresponding onEnter commands");
}
if (onEnterCommands.size() != states.size()) {
throw new IllegalArgumentException("Not all states have corresponding onEnter commands");
}

if (onEnterCommands.get(states.get(0)) == null) {
throw new IllegalArgumentException("Initial state must have a corresponding onEnter command");
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/tests/ParallelRunSMTestCases.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,35 @@ public void testStopRunningTiming2() {
assertFalse(map[2]);
assertFalse(stateMachine.isRunning());
}
// mismatch states and onenters
@Test
public void testMismatchStatesAndOnEnters() {
ParallelRunSM.Builder<States> builder = new ParallelRunSM.Builder<>();
builder.state(States.STATE1)
.onEnter(States.STATE1, () -> System.out.println("Entering STATE1"))
.state(States.STATE2)
.stopRunning(States.STOP,()->true);
try {
ParallelRunSM<States> stateMachine = builder.build(false,100);
} catch (IllegalArgumentException e) {
assertEquals("Not all states have corresponding onEnter commands", e.getMessage());
}
}
// test a negative timeout
@Test
public void testNegativeTimeout() {
ParallelRunSM.Builder<States> builder = new ParallelRunSM.Builder<>();
builder.state(States.STATE1)
.onEnter(States.STATE1, () -> System.out.println("Entering STATE1"))
.state(States.STATE2)
.onEnter(States.STATE2, () -> System.out.println("Entering STATE2"))
.state(States.STATE3)
.onEnter(States.STATE3, () -> System.out.println("Entering STATE3"))
.stopRunning(States.STOP,()->true);
try {
ParallelRunSM<States> stateMachine = builder.build(false,-100);
} catch (IllegalArgumentException e) {
assertEquals("Timeout must be a positive integer", e.getMessage());
}
}
}

0 comments on commit 3808d64

Please sign in to comment.