-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1987 from lf-lang/test-lf-set-array
Test lf_set_array and persistent inputs
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |