xref: /libCEED/tests/t407-qfunction.c (revision cd88d23c377cb39cade98b31cad31ac580d0d240)
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   CeedContextFieldLabel time_label, count_label;
17 
18   TestContext ctx_data = {
19     .time = 1.0,
20     .count = 42
21   };
22 
23   CeedInit(argv[1], &ceed);
24 
25   CeedQFunctionContextCreate(ceed, &ctx);
26   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER,
27                               sizeof(TestContext), &ctx_data);
28 
29   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time),
30                                      "current time");
31   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count),
32                                     "some sort of counter");
33 
34   const CeedContextFieldLabel *field_labels;
35   CeedInt num_fields;
36   CeedQFunctionContextGetAllFieldLabels(ctx, &field_labels, &num_fields);
37   if (num_fields != 2)
38     // LCOV_EXCL_START
39     printf("Incorrect number of fields set: %d != 2", num_fields);
40   // LCOV_EXCL_STOP
41   const char *name;
42   CeedContextFieldType type;
43   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &type);
44   if (strcmp(name, "time"))
45     // LCOV_EXCL_START
46     printf("Incorrect context field description for time: \"%s\" != \"time\"",
47            name);
48   // LCOV_EXCL_STOP
49   if (type != CEED_CONTEXT_FIELD_DOUBLE)
50     // LCOV_EXCL_START
51     printf("Incorrect context field type for time: \"%s\" != \"%s\"",
52            CeedContextFieldTypes[type], CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
53   // LCOV_EXCL_STOP
54   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &type);
55   if (strcmp(name, "count"))
56     // LCOV_EXCL_START
57     printf("Incorrect context field description for count: \"%s\" != \"count\"",
58            name);
59   // LCOV_EXCL_STOP
60   if (type != CEED_CONTEXT_FIELD_INT32)
61     // LCOV_EXCL_START
62     printf("Incorrect context field type for count: \"%s\" != \"%s\"",
63            CeedContextFieldTypes[type], CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
64   // LCOV_EXCL_STOP
65 
66   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
67   CeedQFunctionContextSetDouble(ctx, time_label, 2.0);
68   if (ctx_data.time != 2.0)
69     // LCOV_EXCL_START
70     printf("Incorrect context data for time: %f != 2.0", ctx_data.time);
71   // LCOV_EXCL_STOP
72 
73   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
74   CeedQFunctionContextSetInt32(ctx, count_label, 43);
75   if (ctx_data.count != 43)
76     // LCOV_EXCL_START
77     printf("Incorrect context data for count: %d != 43", ctx_data.count);
78   // LCOV_EXCL_STOP
79 
80   CeedQFunctionContextDestroy(&ctx);
81   CeedDestroy(&ceed);
82   return 0;
83 }
84