xref: /libCEED/tests/t407-qfunction.c (revision f190906abec33cf8d9eb9776bd62dd828c8ae3fd)
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[2];
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 = {13, 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                                      1, "current time");
31   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count),
32                                     2, "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\n", num_fields);
40   // LCOV_EXCL_STOP
41 
42   const char *name;
43   size_t num_values;
44   CeedContextFieldType type;
45   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &num_values,
46                                       &type);
47   if (strcmp(name, "time"))
48     // LCOV_EXCL_START
49     printf("Incorrect context field description for time: \"%s\" != \"time\"\n",
50            name);
51   // LCOV_EXCL_STOP
52   if (num_values != 1)
53     // LCOV_EXCL_START
54     printf("Incorrect context field number of values for time: \"%ld\" != 1\n",
55            num_values);
56   // LCOV_EXCL_STOP
57   if (type != CEED_CONTEXT_FIELD_DOUBLE)
58     // LCOV_EXCL_START
59     printf("Incorrect context field type for time: \"%s\" != \"%s\"\n",
60            CeedContextFieldTypes[type], CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
61   // LCOV_EXCL_STOP
62 
63   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &num_values,
64                                       &type);
65   if (strcmp(name, "count"))
66     // LCOV_EXCL_START
67     printf("Incorrect context field description for count: \"%s\" != \"count\"\n",
68            name);
69   // LCOV_EXCL_STOP
70   if (num_values != 2)
71     // LCOV_EXCL_START
72     printf("Incorrect context field number of values for count: \"%ld\" != 2\n",
73            num_values);
74   // LCOV_EXCL_STOP
75   if (type != CEED_CONTEXT_FIELD_INT32)
76     // LCOV_EXCL_START
77     printf("Incorrect context field type for count: \"%s\" != \"%s\"\n",
78            CeedContextFieldTypes[type], CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
79   // LCOV_EXCL_STOP
80 
81   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
82   double value_time = 2.0;
83   CeedQFunctionContextSetDouble(ctx, time_label, &value_time);
84   if (ctx_data.time != 2.0)
85     // LCOV_EXCL_START
86     printf("Incorrect context data for time: %f != 2.0\n", ctx_data.time);
87   // LCOV_EXCL_STOP
88 
89   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
90   int values_count[2] = {14, 43};
91   CeedQFunctionContextSetInt32(ctx, count_label, (int *)&values_count);
92   if (ctx_data.count[0] != 14)
93     // LCOV_EXCL_START
94     printf("Incorrect context data for count[0]: %d != 14\n", ctx_data.count[0]);
95   // LCOV_EXCL_STOP
96   if (ctx_data.count[1] != 43)
97     // LCOV_EXCL_START
98     printf("Incorrect context data for count[1]: %d != 43\n", ctx_data.count[1]);
99   // LCOV_EXCL_STOP
100 
101   CeedQFunctionContextDestroy(&ctx);
102   CeedDestroy(&ceed);
103   return 0;
104 }
105