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