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