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