xref: /libCEED/tests/t407-qfunction.c (revision 3eb59678ecb8a0fe884fb9297a6048221d053835)
1 /// @file
2 /// Test registering and setting QFunctionContext fields
3 /// \test Test registering and setting QFunctionContext fields
4 #include <ceed.h>
5 #include <ceed/backend.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 
10 typedef struct {
11   double time;
12   int    count[2];
13   bool   is_set;
14 } TestContext;
15 
16 int main(int argc, char **argv) {
17   Ceed                  ceed;
18   CeedQFunctionContext  ctx;
19   CeedContextFieldLabel time_label, count_label, is_set_label;
20 
21   TestContext ctx_data = {
22       .time   = 1.0,
23       .count  = {13, 42},
24       .is_set = true,
25   };
26 
27   CeedInit(argv[1], &ceed);
28 
29   CeedQFunctionContextCreate(ceed, &ctx);
30   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext), &ctx_data);
31 
32   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time), 1, "current time");
33   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count), 2, "some sort of counter");
34   CeedQFunctionContextRegisterBoolean(ctx, "is set", offsetof(TestContext, is_set), 1, "some boolean flag");
35 
36   const CeedContextFieldLabel *field_labels;
37   CeedInt                      num_fields;
38   CeedQFunctionContextGetAllFieldLabels(ctx, &field_labels, &num_fields);
39   if (num_fields != 3) printf("Incorrect number of fields set: %" CeedInt_FMT " != 2\n", num_fields);
40 
41   const char          *name;
42   size_t               num_values;
43   CeedContextFieldType type;
44 
45   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &num_values, NULL, &type);
46   if (strcmp(name, "time")) printf("Incorrect context field description for time: \"%s\" != \"time\"\n", name);
47   if (num_values != 1) printf("Incorrect context field number of values for time: \"%zu\" != 1\n", num_values);
48   if (type != CEED_CONTEXT_FIELD_DOUBLE) {
49     // LCOV_EXCL_START
50     printf("Incorrect context field type for time: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
51            CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
52     // LCOV_EXCL_STOP
53   }
54 
55   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &num_values, NULL, &type);
56   if (strcmp(name, "count")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
57   if (num_values != 2) printf("Incorrect context field number of values for count: \"%zu\" != 2\n", num_values);
58   if (type != CEED_CONTEXT_FIELD_INT32) {
59     // LCOV_EXCL_START
60     printf("Incorrect context field type for count: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
61            CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
62     // LCOV_EXCL_STOP
63   }
64 
65   CeedContextFieldLabelGetDescription(field_labels[2], &name, NULL, &num_values, NULL, &type);
66   if (strcmp(name, "is set")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
67   if (num_values != 1) printf("Incorrect context field number of values for count: \"%zu\" != 2\n", num_values);
68   if (type != CEED_CONTEXT_FIELD_BOOL) {
69     // LCOV_EXCL_START
70     printf("Incorrect context field type for is_set: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
71            CeedContextFieldTypes[CEED_CONTEXT_FIELD_BOOL]);
72     // LCOV_EXCL_STOP
73   }
74 
75   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
76   double value_time = 2.0;
77 
78   CeedQFunctionContextSetDouble(ctx, time_label, &value_time);
79   if (ctx_data.time != 2.0) printf("Incorrect context data for time: %f != 2.0\n", ctx_data.time);
80 
81   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
82   int values_count[2] = {14, 43};
83 
84   CeedQFunctionContextSetInt32(ctx, count_label, (int *)&values_count);
85   if (ctx_data.count[0] != 14) printf("Incorrect context data for count[0]: %" CeedInt_FMT " != 14\n", ctx_data.count[0]);
86   if (ctx_data.count[1] != 43) printf("Incorrect context data for count[1]: %" CeedInt_FMT " != 43\n", ctx_data.count[1]);
87 
88   CeedQFunctionContextGetFieldLabel(ctx, "is set", &is_set_label);
89   bool value_is_set = false;
90 
91   CeedQFunctionContextSetBoolean(ctx, is_set_label, &value_is_set);
92   if (ctx_data.is_set != false) printf("Incorrect context data for is_set: %d != true\n", ctx_data.is_set);
93 
94   CeedQFunctionContextDestroy(&ctx);
95   CeedDestroy(&ceed);
96   return 0;
97 }
98