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