xref: /libCEED/tests/t407-qfunction.c (revision 650a5d66e4f30da5db797426ea50232309c53955)
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   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &num_values, NULL, &type);
45   if (strcmp(name, "time")) printf("Incorrect context field description for time: \"%s\" != \"time\"\n", name);
46   if (num_values != 1) printf("Incorrect context field number of values for time: \"%ld\" != 1\n", num_values);
47   if (type != CEED_CONTEXT_FIELD_DOUBLE) {
48     // LCOV_EXCL_START
49     printf("Incorrect context field type for time: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
50            CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
51     // LCOV_EXCL_STOP
52   }
53 
54   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &num_values, NULL, &type);
55   if (strcmp(name, "count")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
56   if (num_values != 2) printf("Incorrect context field number of values for count: \"%ld\" != 2\n", num_values);
57   if (type != CEED_CONTEXT_FIELD_INT32) {
58     // LCOV_EXCL_START
59     printf("Incorrect context field type for count: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
60            CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
61     // LCOV_EXCL_STOP
62   }
63 
64   CeedContextFieldLabelGetDescription(field_labels[2], &name, NULL, &num_values, NULL, &type);
65   if (strcmp(name, "is set")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
66   if (num_values != 1) printf("Incorrect context field number of values for count: \"%ld\" != 2\n", num_values);
67   if (type != CEED_CONTEXT_FIELD_BOOL) {
68     // LCOV_EXCL_START
69     printf("Incorrect context field type for is_set: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
70            CeedContextFieldTypes[CEED_CONTEXT_FIELD_BOOL]);
71     // LCOV_EXCL_STOP
72   }
73 
74   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
75   double value_time = 2.0;
76   CeedQFunctionContextSetDouble(ctx, time_label, &value_time);
77   if (ctx_data.time != 2.0) printf("Incorrect context data for time: %f != 2.0\n", ctx_data.time);
78 
79   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
80   int values_count[2] = {14, 43};
81   CeedQFunctionContextSetInt32(ctx, count_label, (int *)&values_count);
82   if (ctx_data.count[0] != 14) printf("Incorrect context data for count[0]: %" CeedInt_FMT " != 14\n", ctx_data.count[0]);
83   if (ctx_data.count[1] != 43) printf("Incorrect context data for count[1]: %" CeedInt_FMT " != 43\n", ctx_data.count[1]);
84 
85   CeedQFunctionContextGetFieldLabel(ctx, "is set", &is_set_label);
86   bool value_is_set = false;
87   CeedQFunctionContextSetBoolean(ctx, is_set_label, &value_is_set);
88   if (ctx_data.is_set != false) printf("Incorrect context data for is_set: %d != true\n", ctx_data.is_set);
89 
90   CeedQFunctionContextDestroy(&ctx);
91   CeedDestroy(&ceed);
92   return 0;
93 }
94