Skip to content

Commit c220498

Browse files
committed
Add unit tests for defer, drop, replace, and update action policies
1 parent a984fc9 commit c220498

6 files changed

Lines changed: 167 additions & 4 deletions

File tree

test/unit/action_defer_test.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "unity.h"
2+
3+
#define ACTION_LIB_POLICY defer
4+
#define ACTION_LIB_MIN_SPACING MSEC(5)
5+
#include "action_lib.h"
6+
7+
LF_DEFINE_REACTION_BODY(ActionLib, reaction) {
8+
LF_SCOPE_SELF(ActionLib);
9+
LF_SCOPE_ENV();
10+
LF_SCOPE_ACTION(ActionLib, act);
11+
12+
if (self->cnt == 0) {
13+
// Startup: schedule two events that will violate min_spacing
14+
TEST_ASSERT_EQUAL(false, lf_is_present(act));
15+
lf_schedule(act, MSEC(1), 41);
16+
lf_schedule(act, MSEC(2), 42); // Should be deferred to MSEC(6)
17+
} else if (self->cnt == 1) {
18+
// First event fires at MSEC(1) with value 41
19+
TEST_ASSERT_EQUAL(true, lf_is_present(act));
20+
TEST_ASSERT_EQUAL(env->scheduler->start_time + MSEC(1),
21+
env->scheduler->current_tag(env->scheduler).time);
22+
TEST_ASSERT_EQUAL(41, act->value);
23+
} else if (self->cnt == 2) {
24+
// Deferred event fires at MSEC(6) with value 42
25+
TEST_ASSERT_EQUAL(true, lf_is_present(act));
26+
TEST_ASSERT_EQUAL(env->scheduler->start_time + MSEC(6),
27+
env->scheduler->current_tag(env->scheduler).time);
28+
TEST_ASSERT_EQUAL(42, act->value);
29+
}
30+
self->cnt++;
31+
}
32+
33+
LF_DEFINE_REACTION_BODY(ActionLib, r_shutdown) {
34+
LF_SCOPE_SELF(ActionLib);
35+
TEST_ASSERT_EQUAL(3, self->cnt);
36+
}
37+
38+
void test_run() { lf_start(); }
39+
int main() {
40+
UNITY_BEGIN();
41+
RUN_TEST(test_run);
42+
return UNITY_END();
43+
}

test/unit/action_drop_test.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "unity.h"
2+
3+
#define ACTION_LIB_POLICY drop
4+
#define ACTION_LIB_MIN_SPACING MSEC(5)
5+
#include "action_lib.h"
6+
7+
LF_DEFINE_REACTION_BODY(ActionLib, reaction) {
8+
LF_SCOPE_SELF(ActionLib);
9+
LF_SCOPE_ENV();
10+
LF_SCOPE_ACTION(ActionLib, act);
11+
12+
if (self->cnt == 0) {
13+
// Startup: schedule two events that will violate min_spacing
14+
TEST_ASSERT_EQUAL(false, lf_is_present(act));
15+
lf_schedule(act, MSEC(1), 41);
16+
lf_schedule(act, MSEC(2), 42); // Should be dropped
17+
} else if (self->cnt >= 1) {
18+
// Only the first event fires at MSEC(1) with value 41
19+
TEST_ASSERT_EQUAL(true, lf_is_present(act));
20+
TEST_ASSERT_EQUAL(env->scheduler->start_time + MSEC(1),
21+
env->scheduler->current_tag(env->scheduler).time);
22+
TEST_ASSERT_EQUAL(41, act->value);
23+
}
24+
self->cnt++;
25+
}
26+
27+
LF_DEFINE_REACTION_BODY(ActionLib, r_shutdown) {
28+
LF_SCOPE_SELF(ActionLib);
29+
TEST_ASSERT_EQUAL(2, self->cnt);
30+
}
31+
32+
void test_run() { lf_start(); }
33+
int main() {
34+
UNITY_BEGIN();
35+
RUN_TEST(test_run);
36+
return UNITY_END();
37+
}

test/unit/action_lib.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
#include "reactor-uc/reactor-uc.h"
44
#include "reactor-uc/schedulers/dynamic/scheduler.h"
55

6+
#ifndef ACTION_LIB_POLICY
7+
#define ACTION_LIB_POLICY defer
8+
#endif
9+
10+
#ifndef ACTION_LIB_MIN_SPACING
11+
#define ACTION_LIB_MIN_SPACING MSEC(0)
12+
#endif
13+
614
#ifdef ACTION_LIB_VOID_TYPE
715
LF_DEFINE_ACTION_STRUCT_VOID(ActionLib, act, LogicalAction, 1, 1, 0, 2);
8-
LF_DEFINE_ACTION_CTOR_VOID(ActionLib, act, LogicalAction, defer, 1, 1, 0, 2);
16+
LF_DEFINE_ACTION_CTOR_VOID(ActionLib, act, LogicalAction, ACTION_LIB_POLICY, 1, 1, 0, 2);
917
#else
1018
LF_DEFINE_ACTION_STRUCT(ActionLib, act, LogicalAction, 1, 1, 0, 10, int);
11-
LF_DEFINE_ACTION_CTOR(ActionLib, act, LogicalAction, defer, 1, 1, 0, 10, int);
19+
LF_DEFINE_ACTION_CTOR(ActionLib, act, LogicalAction, ACTION_LIB_POLICY, 1, 1, 0, 10, int);
1220
#endif
1321

1422
LF_DEFINE_STARTUP_STRUCT(ActionLib, 1, 0);
@@ -39,7 +47,7 @@ LF_REACTOR_CTOR_SIGNATURE(ActionLib) {
3947

4048
LF_INITIALIZE_REACTION(ActionLib, reaction, NEVER);
4149
LF_INITIALIZE_REACTION(ActionLib, r_shutdown, NEVER);
42-
LF_INITIALIZE_ACTION(ActionLib, act, MSEC(0), MSEC(0));
50+
LF_INITIALIZE_ACTION(ActionLib, act, MSEC(0), ACTION_LIB_MIN_SPACING);
4351
LF_INITIALIZE_STARTUP(ActionLib);
4452
LF_INITIALIZE_SHUTDOWN(ActionLib);
4553
LF_ACTION_REGISTER_EFFECT(self->act, self->reaction);

test/unit/action_replace_test.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "unity.h"
2+
3+
#define ACTION_LIB_POLICY replace
4+
#define ACTION_LIB_MIN_SPACING MSEC(5)
5+
#include "action_lib.h"
6+
7+
LF_DEFINE_REACTION_BODY(ActionLib, reaction) {
8+
LF_SCOPE_SELF(ActionLib);
9+
LF_SCOPE_ENV();
10+
LF_SCOPE_ACTION(ActionLib, act);
11+
12+
if (self->cnt == 0) {
13+
// Startup: schedule two events that will violate min_spacing
14+
TEST_ASSERT_EQUAL(false, lf_is_present(act));
15+
lf_schedule(act, MSEC(1), 41);
16+
lf_schedule(act, MSEC(2), 42); // Should replace payload at MSEC(1)
17+
} else if (self->cnt >= 1) {
18+
// Event fires at MSEC(1) but with replaced value 42
19+
TEST_ASSERT_EQUAL(true, lf_is_present(act));
20+
TEST_ASSERT_EQUAL(env->scheduler->start_time + MSEC(1),
21+
env->scheduler->current_tag(env->scheduler).time);
22+
TEST_ASSERT_EQUAL(42, act->value);
23+
}
24+
self->cnt++;
25+
}
26+
27+
LF_DEFINE_REACTION_BODY(ActionLib, r_shutdown) {
28+
LF_SCOPE_SELF(ActionLib);
29+
TEST_ASSERT_EQUAL(2, self->cnt);
30+
}
31+
32+
void test_run() { lf_start(); }
33+
int main() {
34+
UNITY_BEGIN();
35+
RUN_TEST(test_run);
36+
return UNITY_END();
37+
}

test/unit/action_update_test.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "unity.h"
2+
3+
#define ACTION_LIB_POLICY update
4+
#define ACTION_LIB_MIN_SPACING MSEC(5)
5+
#include "action_lib.h"
6+
7+
LF_DEFINE_REACTION_BODY(ActionLib, reaction) {
8+
LF_SCOPE_SELF(ActionLib);
9+
LF_SCOPE_ENV();
10+
LF_SCOPE_ACTION(ActionLib, act);
11+
printf("Reaction executed at time " PRINTF_TIME ", with value %i\n", env->scheduler->current_tag(env->scheduler).time, act->value);
12+
13+
if (self->cnt == 0) {
14+
// Startup: schedule two events that will violate min_spacing
15+
TEST_ASSERT_EQUAL(false, lf_is_present(act));
16+
lf_schedule(act, MSEC(1), 41);
17+
lf_schedule(act, MSEC(2), 42); // Should cancel old, schedule at MSEC(2)
18+
} else if (self->cnt >= 1) {
19+
// Event fires at MSEC(2) with value 42 (old event cancelled)
20+
TEST_ASSERT_EQUAL(true, lf_is_present(act));
21+
TEST_ASSERT_EQUAL(env->scheduler->start_time + MSEC(2),
22+
env->scheduler->current_tag(env->scheduler).time);
23+
TEST_ASSERT_EQUAL(42, act->value);
24+
}
25+
self->cnt++;
26+
}
27+
28+
LF_DEFINE_REACTION_BODY(ActionLib, r_shutdown) {
29+
LF_SCOPE_SELF(ActionLib);
30+
TEST_ASSERT_EQUAL(2, self->cnt);
31+
}
32+
33+
void test_run() { lf_start(); }
34+
int main() {
35+
UNITY_BEGIN();
36+
RUN_TEST(test_run);
37+
return UNITY_END();
38+
}

test/unit/event_queue_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void test_remove(void) {
219219

220220
// Remove non-existent event (should do nothing)
221221
Event t2 = EVENT_INIT(((tag_t){.time = 999}), &trigger_b, NULL);
222-
TEST_ASSERT_EQUAL(LF_OK, q.remove(&q, &t2.super));
222+
TEST_ASSERT_EQUAL(LF_NOT_FOUND, q.remove(&q, &t2.super));
223223
TEST_ASSERT_EQUAL(2, q.size);
224224

225225
// Remove event e1 (root)

0 commit comments

Comments
 (0)