xref: /libCEED/tests/t407-qfunction.c (revision cdf32b9369b06315737c6f9df3317771bd8e77c7)
1*cdf32b93SJeremy L Thompson /// @file
2*cdf32b93SJeremy L Thompson /// Test registering and setting QFunctionContext fields
3*cdf32b93SJeremy L Thompson /// \test Test registering and setting QFunctionContext fields
4*cdf32b93SJeremy L Thompson #include <ceed.h>
5*cdf32b93SJeremy L Thompson #include <stddef.h>
6*cdf32b93SJeremy L Thompson #include <string.h>
7*cdf32b93SJeremy L Thompson 
8*cdf32b93SJeremy L Thompson typedef struct {
9*cdf32b93SJeremy L Thompson   double time;
10*cdf32b93SJeremy L Thompson   int count;
11*cdf32b93SJeremy L Thompson } TestContext;
12*cdf32b93SJeremy L Thompson 
13*cdf32b93SJeremy L Thompson int main(int argc, char **argv) {
14*cdf32b93SJeremy L Thompson   Ceed ceed;
15*cdf32b93SJeremy L Thompson   CeedQFunctionContext ctx;
16*cdf32b93SJeremy L Thompson   TestContext ctx_data = {
17*cdf32b93SJeremy L Thompson     .time = 1.0,
18*cdf32b93SJeremy L Thompson     .count = 42
19*cdf32b93SJeremy L Thompson   };
20*cdf32b93SJeremy L Thompson 
21*cdf32b93SJeremy L Thompson   CeedInit(argv[1], &ceed);
22*cdf32b93SJeremy L Thompson 
23*cdf32b93SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
24*cdf32b93SJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER,
25*cdf32b93SJeremy L Thompson                               sizeof(TestContext), &ctx_data);
26*cdf32b93SJeremy L Thompson 
27*cdf32b93SJeremy L Thompson   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time),
28*cdf32b93SJeremy L Thompson                                      "current time");
29*cdf32b93SJeremy L Thompson   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count),
30*cdf32b93SJeremy L Thompson                                     "some sort of counter");
31*cdf32b93SJeremy L Thompson 
32*cdf32b93SJeremy L Thompson   const CeedQFunctionContextFieldDescription *field_descriptions;
33*cdf32b93SJeremy L Thompson   CeedInt num_fields;
34*cdf32b93SJeremy L Thompson   CeedQFunctionContextGetFieldDescriptions(ctx, &field_descriptions, &num_fields);
35*cdf32b93SJeremy L Thompson   if (num_fields != 2)
36*cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
37*cdf32b93SJeremy L Thompson     printf("Incorrect number of fields set: %d != 2", num_fields);
38*cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
39*cdf32b93SJeremy L Thompson   if (strcmp(field_descriptions[0].name, "time"))
40*cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
41*cdf32b93SJeremy L Thompson     printf("Incorrect context field description for time: \"%s\" != \"time\"",
42*cdf32b93SJeremy L Thompson            field_descriptions[0].name);
43*cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
44*cdf32b93SJeremy L Thompson   if (strcmp(field_descriptions[1].name, "count"))
45*cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
46*cdf32b93SJeremy L Thompson     printf("Incorrect context field description for time: \"%s\" != \"count\"",
47*cdf32b93SJeremy L Thompson            field_descriptions[1].name);
48*cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
49*cdf32b93SJeremy L Thompson 
50*cdf32b93SJeremy L Thompson   CeedQFunctionContextSetDouble(ctx, "time", 2.0);
51*cdf32b93SJeremy L Thompson   if (ctx_data.time != 2.0)
52*cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
53*cdf32b93SJeremy L Thompson     printf("Incorrect context data for time: %f != 2.0", ctx_data.time);
54*cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
55*cdf32b93SJeremy L Thompson 
56*cdf32b93SJeremy L Thompson   CeedQFunctionContextSetInt32(ctx, "count", 43);
57*cdf32b93SJeremy L Thompson   if (ctx_data.count != 43)
58*cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
59*cdf32b93SJeremy L Thompson     printf("Incorrect context data for time: %d != 43", ctx_data.count);
60*cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
61*cdf32b93SJeremy L Thompson 
62*cdf32b93SJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
63*cdf32b93SJeremy L Thompson   CeedDestroy(&ceed);
64*cdf32b93SJeremy L Thompson   return 0;
65*cdf32b93SJeremy L Thompson }
66