Skip to content

Commit

Permalink
Merge pull request #1987 from lf-lang/test-lf-set-array
Browse files Browse the repository at this point in the history
Test lf_set_array and persistent inputs
  • Loading branch information
edwardalee authored Sep 3, 2023
2 parents 279fc82 + 5ee6fec commit ca412c5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/C/src/ArraySet.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target C

reactor Source {
output out: int[]

reaction(startup) -> out {=
// Dynamically allocate an output array of length 3.
int* array = (int*)malloc(3 * sizeof(int));
// Populate the array.
array[0] = 0;
array[1] = 1;
array[2] = 2;
// Set the output, specifying the array length.
lf_set_array(out, array, 3);
=}
}

reactor Print {
input in: int[]

reaction(in) {=
printf("Received: [");
for (int i = 0; i < in->length; i++) {
if (i > 0) printf(", ");
printf("%d", in->value[i]);
if (in->value[i] != i) {
lf_print_error_and_exit("Expected %d.", i);
}
}
printf("]\n");
=}
}

main reactor {
s = new Source()
p = new Print()
s.out -> p.in
}
34 changes: 34 additions & 0 deletions test/C/src/PersistentInputs.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
target C {
timeout: 400 ms,
fast: true
}

reactor Source {
output out: int
timer t(100 ms, 200 ms)
state count: int = 1

reaction(t) -> out {= lf_set(out, self->count++); =}
}

reactor Sink {
input in: int
timer t(0, 100 ms)
state count: int = 0 // For testing, emulate the count variable of Source.
timer t2(100 ms, 200 ms)

reaction(t2) {= self->count++; =}

reaction(t) in {=
printf("Value of the input is %d at time %lld\n", in->value, lf_time_logical_elapsed());
if (in->value != self->count) {
lf_print_error_and_exit("Expected %d.", self->count);
}
=}
}

main reactor {
source = new Source()
sink = new Sink()
source.out -> sink.in
}

0 comments on commit ca412c5

Please sign in to comment.