xref: /libCEED/tests/t407-qfunction.c (revision 0f86cbe7e90c9366c73a141bde379916ec0231bc)
1cdf32b93SJeremy L Thompson /// @file
2cdf32b93SJeremy L Thompson /// Test registering and setting QFunctionContext fields
3cdf32b93SJeremy L Thompson /// \test Test registering and setting QFunctionContext fields
4cdf32b93SJeremy L Thompson #include <ceed.h>
5cdf32b93SJeremy L Thompson #include <stddef.h>
6cdf32b93SJeremy L Thompson #include <string.h>
7cdf32b93SJeremy L Thompson 
8cdf32b93SJeremy L Thompson typedef struct {
9cdf32b93SJeremy L Thompson   double time;
10cdf32b93SJeremy L Thompson   int count;
11cdf32b93SJeremy L Thompson } TestContext;
12cdf32b93SJeremy L Thompson 
13cdf32b93SJeremy L Thompson int main(int argc, char **argv) {
14cdf32b93SJeremy L Thompson   Ceed ceed;
15cdf32b93SJeremy L Thompson   CeedQFunctionContext ctx;
163668ca4bSJeremy L Thompson   CeedContextFieldLabel time_label, count_label;
173668ca4bSJeremy L Thompson 
18cdf32b93SJeremy L Thompson   TestContext ctx_data = {
19cdf32b93SJeremy L Thompson     .time = 1.0,
20cdf32b93SJeremy L Thompson     .count = 42
21cdf32b93SJeremy L Thompson   };
22cdf32b93SJeremy L Thompson 
23cdf32b93SJeremy L Thompson   CeedInit(argv[1], &ceed);
24cdf32b93SJeremy L Thompson 
25cdf32b93SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
26cdf32b93SJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER,
27cdf32b93SJeremy L Thompson                               sizeof(TestContext), &ctx_data);
28cdf32b93SJeremy L Thompson 
29cdf32b93SJeremy L Thompson   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time),
30cdf32b93SJeremy L Thompson                                      "current time");
31cdf32b93SJeremy L Thompson   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count),
32cdf32b93SJeremy L Thompson                                     "some sort of counter");
33cdf32b93SJeremy L Thompson 
343668ca4bSJeremy L Thompson   const CeedContextFieldLabel *field_labels;
35cdf32b93SJeremy L Thompson   CeedInt num_fields;
363668ca4bSJeremy L Thompson   CeedQFunctionContextGetAllFieldLabels(ctx, &field_labels, &num_fields);
37cdf32b93SJeremy L Thompson   if (num_fields != 2)
38cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
39cdf32b93SJeremy L Thompson     printf("Incorrect number of fields set: %d != 2", num_fields);
40cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
41*0f86cbe7SJeremy L Thompson   const char *name;
42*0f86cbe7SJeremy L Thompson   CeedContextFieldType type;
43*0f86cbe7SJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &type);
44*0f86cbe7SJeremy L Thompson   if (strcmp(name, "time"))
45*0f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
46*0f86cbe7SJeremy L Thompson     printf("Incorrect context field description for time: \"%s\" != \"time\"",
47*0f86cbe7SJeremy L Thompson            name);
48*0f86cbe7SJeremy L Thompson   // LCOV_EXCL_STOP
49*0f86cbe7SJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_DOUBLE)
50*0f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
51*0f86cbe7SJeremy L Thompson     printf("Incorrect context field type for time: \"%s\" != \"%s\"",
52*0f86cbe7SJeremy L Thompson            CeedContextFieldTypes[type], CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
53*0f86cbe7SJeremy L Thompson   // LCOV_EXCL_STOP
54*0f86cbe7SJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &type);
55*0f86cbe7SJeremy L Thompson   if (strcmp(name, "count"))
56*0f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
57*0f86cbe7SJeremy L Thompson     printf("Incorrect context field description for count: \"%s\" != \"count\"",
58*0f86cbe7SJeremy L Thompson            name);
59*0f86cbe7SJeremy L Thompson   // LCOV_EXCL_STOP
60*0f86cbe7SJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_INT32)
61*0f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
62*0f86cbe7SJeremy L Thompson     printf("Incorrect context field type for count: \"%s\" != \"%s\"",
63*0f86cbe7SJeremy L Thompson            CeedContextFieldTypes[type], CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
64*0f86cbe7SJeremy L Thompson   // LCOV_EXCL_STOP
65cdf32b93SJeremy L Thompson 
663668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
673668ca4bSJeremy L Thompson   CeedQFunctionContextSetDouble(ctx, time_label, 2.0);
68cdf32b93SJeremy L Thompson   if (ctx_data.time != 2.0)
69cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
70cdf32b93SJeremy L Thompson     printf("Incorrect context data for time: %f != 2.0", ctx_data.time);
71cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
72cdf32b93SJeremy L Thompson 
733668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
743668ca4bSJeremy L Thompson   CeedQFunctionContextSetInt32(ctx, count_label, 43);
75cdf32b93SJeremy L Thompson   if (ctx_data.count != 43)
76cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
77d8dd9a91SJeremy L Thompson     printf("Incorrect context data for count: %d != 43", ctx_data.count);
78cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
79cdf32b93SJeremy L Thompson 
80cdf32b93SJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
81cdf32b93SJeremy L Thompson   CeedDestroy(&ceed);
82cdf32b93SJeremy L Thompson   return 0;
83cdf32b93SJeremy L Thompson }
84