Skip to content

Commit

Permalink
Added ExternalTransitionTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
clnhlzmn committed Mar 27, 2021
1 parent 9b59057 commit b830af2
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 2 deletions.
4 changes: 2 additions & 2 deletions makina-compiler/src/xyz/colinholzman/makina/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CodeGenerator(val machine: Machine,
private fun generateExitActions(handler: Handler.Event, sourceState: State, activeLeafState: State, output: PrintWriter) {
if (handler.target != null) output.apply {
val target = handler.getTargetState(sourceState, machine)
val transition = Transition(activeLeafState, sourceState, target)
val transition = Transition(activeLeafState, sourceState, target, handler.target.kind)
val exitSet = transition.getExitSet()
for (stateToExit in exitSet) {
for (exit in stateToExit.handlers.filterIsInstance<Handler.Exit>()) {
Expand All @@ -70,7 +70,7 @@ class CodeGenerator(val machine: Machine,
private fun generateEntryActions(handler: Handler.Event, sourceState: State, activeLeafState: State, output: PrintWriter) {
if (handler.target != null) output.apply {
val target = handler.getTargetState(sourceState, machine)
val transition = Transition(activeLeafState, sourceState, target)
val transition = Transition(activeLeafState, sourceState, target, handler.target.kind)
val entrySet = transition.getEntrySet()
val targetLeafState = entrySet.last()
println("\t\t\tself->state = ${machine.id}_${targetLeafState.getFullyQualifiedIdString()};")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package xyz.colinholzman.makina

import org.junit.jupiter.api.Test

class ExternalTransitionTest {
@Test
fun externalTransition() {
externalMakefileTest("./../test/external_transition")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal class TransitionTest {
assertEquals(listOf(s111, s11, s1), Transition(s111, s1, s121, Target.Kind.EXTERNAL).getExitSet())
assertEquals(listOf(s111), Transition(s111, s111, s111, Target.Kind.EXTERNAL).getExitSet())
assertEquals(listOf(s111, s11, s1), Transition(s111, s1, s1, Target.Kind.EXTERNAL).getExitSet())
assertEquals(listOf(s21, s2), Transition(s21, s2, s21, Target.Kind.EXTERNAL).getExitSet())
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions test/external_transition/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
.vscode
16 changes: 16 additions & 0 deletions test/external_transition/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: test
test: out/test
./out/test

out/test: test_driver.c out/test.c out/test.h out
gcc test_driver.c out/test.c -I "." -std=c99 -Wall -Wextra -Werror -o out/test -g

out/test.c:
java -jar ../../makina-compiler/out/artifacts/makina_compiler_jar/makina-compiler.jar test_source.mkna -o out

out:
mkdir out

.PHONY: clean
clean:
rm -rf out
99 changes: 99 additions & 0 deletions test/external_transition/test_driver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "out/test.h"

static const char *expected_output[] = {
"s1_entry",
"s1_s2_entry",
"s1_s2_e1_guard",
"s1_s2_exit",
"s1_exit",
"s1_entry",
"s1_s2_entry",
"s1_s2_e1_guard",
"s1_s2_exit",
"s1_exit",
"s1_entry",
"s1_s2_entry",
"s1_s2_e2_guard",
"s1_s2_exit",
"s1_exit",
"s1_entry",
"s1_s2_entry",
"s1_s2_e2_guard",
"s1_s2_exit",
"s1_s2_entry"
};

#define N_EVENTS (sizeof(expected_output) / sizeof(const char *))

static const char *actual_output[N_EVENTS];

static size_t output_index = 0;

#define PUSH_EVENT(e) do { \
assert(output_index < N_EVENTS); \
actual_output[output_index++] = e; \
} while (0)

int s1_s2_entry(struct test *self, struct test_event *event) {
(void)self; (void)event;
PUSH_EVENT("s1_s2_entry");
return 0;
}

static int guard = 0;

int s1_s2_e1_guard(struct test *self, struct test_event *event) {
(void)self; (void)event;
PUSH_EVENT("s1_s2_e1_guard");
return guard;
}

int s1_s2_e2_guard(struct test *self, struct test_event *event) {
(void)self; (void)event;
PUSH_EVENT("s1_s2_e2_guard");
return guard;
}

int s1_s2_e1_action(struct test *self, struct test_event *event) {
(void)self; (void)event;
PUSH_EVENT("s1_s2_e1_action");
return 0;
}

int s1_s2_exit(struct test *self, struct test_event *event) {
(void)self; (void)event;
PUSH_EVENT("s1_s2_exit");
return 0;
}

int s1_entry(struct test *self, struct test_event *event) {
(void)self; (void)event;
PUSH_EVENT("s1_entry");
return 0;
}

int s1_exit(struct test *self, struct test_event *event) {
(void)self; (void)event;
PUSH_EVENT("s1_exit");
return 0;
}

int main (void) {
struct test instance;
test_init(&instance);
test_process_event(&instance, &(struct test_event){.id = test_event_e1});
guard = 1;
test_process_event(&instance, &(struct test_event){.id = test_event_e1});
guard = 0;
test_process_event(&instance, &(struct test_event){.id = test_event_e2});
guard = 1;
test_process_event(&instance, &(struct test_event){.id = test_event_e2});
for (size_t i = 0; i < N_EVENTS; ++i) {
assert(strcmp(expected_output[i], actual_output[i]) == 0);
}
return 0;
}
14 changes: 14 additions & 0 deletions test/external_transition/test_source.mkna
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
machine test;

state s1 {
on e1 --> s1;
on e2 --> s2;
entry s1_entry;
state s2 {
entry s1_s2_entry;
on e1 (s1_s2_e1_guard) --> s1;
on e2 (s1_s2_e2_guard) --> s2;
exit s1_s2_exit;
}
exit s1_exit;
}

0 comments on commit b830af2

Please sign in to comment.