Skip to content

Commit

Permalink
Merge pull request #2190 from lf-lang/cleanup-pass
Browse files Browse the repository at this point in the history
Replace use of deprecated APIs in C tests
  • Loading branch information
edwardalee authored Feb 13, 2024
2 parents 1d0deeb + c1a5ab8 commit af69dd7
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public static List<String> getCTargetSrc() {
}

public static List<String> getCTargetHeader() {
return List.of("include/api/api.h");
return List.of("include/api/schedule.h");
}

public static String getCTargetSetHeader() {
return "include/api/set.h";
return "include/api/reaction_macros.h";
}

public static String getCTargetSetUndefHeader() {
return "include/api/set_undef.h";
return "include/api/reaction_macros_undef.h";
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ protected void generateReactorClassHeaders(
header.pr("extern \"C\" {");
}
header.pr("#include \"include/core/reactor.h\"");
src.pr("#include \"include/api/api.h\"");
src.pr("#include \"include/api/schedule.h\"");
src.pr("#include \"include/core/platform.h\"");
generateIncludes(tpr);
if (cppMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static void appendPoundIncludes(CodeBuilder builder) {
#ifdef __cplusplus
extern "C" {
#endif
#include "../include/api/api.h"
#include "../include/api/schedule.h"
#include "../include/core/reactor.h"
#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion test/C/c/sendreceive.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "../include/IntPrint/Print.h"
#include "../include/IntPrint/Check.h"
#include "../include/api/set.h"
#include "../include/api/reaction_macros.h"

void sender(print_self_t* self, print_out_t* out) {
lf_set(out, 42);
Expand Down
4 changes: 2 additions & 2 deletions test/C/src/AfterZero.lf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ reactor foo {
output y: int

reaction(x) -> y {=
SET(y, 2*x->value);
lf_set(y, 2*x->value);
=}
}

Expand Down Expand Up @@ -55,6 +55,6 @@ main reactor {
f.y -> p.x after 0

reaction(t) -> f.x {=
SET(f.x, 42);
lf_set(f.x, 42);
=}
}
2 changes: 1 addition & 1 deletion test/C/src/ArrayAsType.lf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ reactor Source {
out->value[0] = 0;
out->value[1] = 1;
out->value[2] = 2;
SET_PRESENT(out);
lf_set_present(out);
=}
}

Expand Down
13 changes: 7 additions & 6 deletions test/C/src/ArrayFreeMultiple.lf
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ reactor Source {

reaction(t) -> out {=
// Dynamically allocate an output array of length 3.
SET_NEW_ARRAY(out, 3);

// Above allocates the array, which then must be populated.
out->value[0] = self->c++;
out->value[1] = self->c++;
out->value[2] = self->c++;
int* array = (int*)malloc(3 * sizeof(int));
// Populate the array.
array[0] = self->c++;
array[1] = self->c++;
array[2] = self->c++;
// Set the output, specifying the array length.
lf_set_array(out, array, 3);
=}
}

Expand Down
16 changes: 7 additions & 9 deletions test/C/src/ArrayPrint.lf
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ reactor Source(size: int = 3) {

reaction(t) -> out {=
// Dynamically allocate an output array of length 3.
// Note that the receiving reactors need to know that the length is 3.
// A better design is given at:
// https://www.lf-lang.org/docs/handbook/target-language-details?target=c#dynamically-allocated-data
SET_NEW_ARRAY(out, 3);

// Above allocates the array, which then must be populated.
out->value[0] = self->count++;
out->value[1] = self->count++;
out->value[2] = self->count++;
int* array = (int*)malloc(3 * sizeof(int));
// Populate the array.
array[0] = self->count++;
array[1] = self->count++;
array[2] = self->count++;
// Set the output, specifying the array length.
lf_set_array(out, array, 3);
=}
}

Expand Down
14 changes: 7 additions & 7 deletions test/C/src/DelayArrayWithAfter.lf
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ reactor Source {

reaction(t) -> out {=
// Dynamically allocate an output array of length 3.
SET_NEW_ARRAY(out, 3);
printf("At time " PRINTF_TIME ", sending array at address %p\n", lf_time_logical_elapsed(), out->value);

// Above allocates the array, which then must be populated.
out->value[0] = 1 * self->iteration;
out->value[1] = 2 * self->iteration;
out->value[2] = 3 * self->iteration;
int* array = (int*)malloc(3 * sizeof(int));
// Populate the array.
array[0] = 1 * self->iteration;
array[1] = 2 * self->iteration;
array[2] = 3 * self->iteration;
// Set the output, specifying the array length.
lf_set_array(out, array, 3);
self->iteration++;
=}
}
Expand Down
21 changes: 17 additions & 4 deletions test/C/src/DelayStruct.lf
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,38 @@ reactor Source {

reaction(startup) -> out {=
// Dynamically allocate an output struct.
SET_NEW(out);
// Note that the default destructor and copy constructor are sufficient here.
hello_t* hello = (hello_t*)malloc(sizeof(hello_t));

// Above allocates a struct, which then must be populated.
out->value->name = "Earth";
out->value->value = 42;
// Populate the struct.
hello->name = "Earth";
hello->value = 42;

// Set the output.
lf_set(out, hello);
=}
}

// expected parameter is for testing.
reactor Print(expected: int = 42) {
input in: hello_t*
state invoked: bool = false

reaction(in) {=
self->invoked = true;
printf("Received: name = %s, value = %d\n", in->value->name, in->value->value);
if (in->value->value != self->expected) {
printf("ERROR: Expected value to be %d.\n", self->expected);
exit(1);
}
=}

reaction(shutdown) {=
if (self->invoked == false) {
fprintf(stderr, "ERROR: No data received.\n");
exit(2);
}
=}
}

main reactor DelayStruct {
Expand Down
28 changes: 2 additions & 26 deletions test/C/src/DelayStructWithAfter.lf
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,12 @@ target C {
files: include/hello.h
}

import Source, Print from "DelayStruct.lf"

preamble {=
#include "hello.h"
=}

reactor Source {
output out: hello_t*

reaction(startup) -> out {=
// Dynamically allocate an output struct.
SET_NEW(out);

// Above allocates a struct, which then must be populated.
out->value->name = "Earth";
out->value->value = 42;
=}
}

// expected parameter is for testing.
reactor Print(expected: int = 42) {
input in: hello_t*

reaction(in) {=
printf("Received: name = %s, value = %d\n", in->value->name, in->value->value);
if (in->value->value != self->expected) {
printf("ERROR: Expected value to be %d.\n", self->expected);
exit(1);
}
=}
}

main reactor DelayStructWithAfter {
s = new Source()
p = new Print()
Expand Down
12 changes: 8 additions & 4 deletions test/C/src/DelayStructWithAfterOverlapped.lf
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ reactor Source {
reaction(t) -> out {=
self->s++;
// Dynamically allocate an output struct.
SET_NEW(out);
// Note that the default destructor and copy constructor are sufficient here.
hello_t* hello = (hello_t*)malloc(sizeof(hello_t));

// Above allocates a struct, which then must be populated.
out->value->name = "Earth";
out->value->value = 42 * self->s;
// Populate the struct.
hello->name = "Earth";
hello->value = 42 * self->s;

// Set the output.
lf_set(out, hello);
=}
}

Expand Down
9 changes: 5 additions & 4 deletions test/C/src/SetArray.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ reactor Source {
reaction(startup) -> out {=
// Dynamically allocate an output array of length 3.
int* array = (int*)malloc(3 * sizeof(int));
SET_ARRAY(out, array, sizeof(int), 3);

// Above allocates the array, which then must be populated.
out->value[0] = 0;
out->value[1] = 1;
out->value[2] = 2;
array[0] = 0;
array[1] = 1;
array[2] = 2;

lf_set_array(out, array, 3);
=}
}

Expand Down
2 changes: 1 addition & 1 deletion test/C/src/StructAsTypeDirect.lf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ reactor Source {
reaction(startup) -> out {=
out->value.name = "Earth";
out->value.value = 42;
SET_PRESENT(out);
lf_set_present(out);
=}
}

Expand Down
2 changes: 1 addition & 1 deletion test/C/src/StructParallel.lf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ target C {
files: ["include/hello.h"]
}

import Source from "StructScale.lf"
import Source from "DelayStruct.lf"

preamble {=
#include "hello.h"
Expand Down
31 changes: 4 additions & 27 deletions test/C/src/StructPrint.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,14 @@ target C {
files: ["include/hello.h"]
}

import Source, Print from "DelayStruct.lf"

preamble {=
#include "hello.h"
=}

reactor Print {
output out: hello_t*

reaction(startup) -> out {=
// Dynamically allocate an output struct.
SET_NEW(out);
// Above allocates a struct, which then must be populated.
out->value->name = "Earth";
out->value->value = 42;
=}
}

// expected parameter is for testing.
reactor Check(expected: int = 42) {
input in: hello_t*

reaction(in) {=
printf("Received: name = %s, value = %d\n", in->value->name, in->value->value);
if (in->value->value != self->expected) {
printf("ERROR: Expected value to be %d.\n", self->expected);
exit(1);
}
=}
}

main reactor {
s = new Print()
p = new Check()
s = new Source()
p = new Print()
s.out -> p.in
}
42 changes: 5 additions & 37 deletions test/C/src/StructScale.lf
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,13 @@ target C {
files: ["include/hello.h"]
}

import Source, Print from "DelayStruct.lf"

preamble {=
#include "hello.h"
=}

reactor Source {
output out: hello_t*

reaction(startup) -> out {=
// Dynamically allocate an output struct.
SET_NEW(out);
// Above allocates a struct, which then must be populated.
out->value->name = "Earth";
out->value->value = 42;
=}
}

// expected parameter is for testing.
reactor TestInput(expected: int = 42) {
input in: hello_t*
state invoked: bool = false

reaction(in) {=
printf("Received: name = %s, value = %d\n", in->value->name, in->value->value);
if (in->value->value != self->expected) {
printf("ERROR: Expected value to be %d.\n", self->expected);
exit(1);
}
self->invoked = true;
=}

reaction(shutdown) {=
if (self->invoked == false) {
fprintf(stderr, "ERROR: No data received.\n");
exit(2);
}
=}
}

reactor Print(scale: int = 2) {
reactor Scale(scale: int = 2) {
// Mutable keyword indicates that this reactor wants a writable copy of the input.
mutable input in: hello_t*

Expand All @@ -57,8 +25,8 @@ reactor Print(scale: int = 2) {

main reactor StructScale {
s = new Source()
c = new Print()
p = new TestInput(expected=84)
c = new Scale()
p = new Print(expected=84)
s.out -> c.in
c.out -> p.in
}
Loading

0 comments on commit af69dd7

Please sign in to comment.