xref: /libCEED/tests/t407-qfunction.c (revision 134a5f7b65b073eb60c36722678fe68c89a9b9b6)
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>
5e6a0ab89SJeremy L Thompson #include <ceed/backend.h>
6cdf32b93SJeremy L Thompson #include <stddef.h>
749aac155SJeremy L Thompson #include <stdio.h>
8cdf32b93SJeremy L Thompson #include <string.h>
9cdf32b93SJeremy L Thompson 
10cdf32b93SJeremy L Thompson typedef struct {
11cdf32b93SJeremy L Thompson   double time;
127bfe0f0eSJeremy L Thompson   int    count[2];
135b6ec284SJeremy L Thompson   bool   is_set;
14cdf32b93SJeremy L Thompson } TestContext;
15cdf32b93SJeremy L Thompson 
main(int argc,char ** argv)16cdf32b93SJeremy L Thompson int main(int argc, char **argv) {
17cdf32b93SJeremy L Thompson   Ceed                  ceed;
18cdf32b93SJeremy L Thompson   CeedQFunctionContext  ctx;
195b6ec284SJeremy L Thompson   CeedContextFieldLabel time_label, count_label, is_set_label;
203668ca4bSJeremy L Thompson 
21cdf32b93SJeremy L Thompson   TestContext ctx_data = {
22cdf32b93SJeremy L Thompson       .time   = 1.0,
237bfe0f0eSJeremy L Thompson       .count  = {13, 42},
245b6ec284SJeremy L Thompson       .is_set = true,
25cdf32b93SJeremy L Thompson   };
26cdf32b93SJeremy L Thompson 
27cdf32b93SJeremy L Thompson   CeedInit(argv[1], &ceed);
28cdf32b93SJeremy L Thompson 
29cdf32b93SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
302b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext), &ctx_data);
31cdf32b93SJeremy L Thompson 
322b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time), 1, "current time");
332b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count), 2, "some sort of counter");
345b6ec284SJeremy L Thompson   CeedQFunctionContextRegisterBoolean(ctx, "is set", offsetof(TestContext, is_set), 1, "some boolean flag");
35cdf32b93SJeremy L Thompson 
363668ca4bSJeremy L Thompson   const CeedContextFieldLabel *field_labels;
37cdf32b93SJeremy L Thompson   CeedInt                      num_fields;
383668ca4bSJeremy L Thompson   CeedQFunctionContextGetAllFieldLabels(ctx, &field_labels, &num_fields);
395b6ec284SJeremy L Thompson   if (num_fields != 3) printf("Incorrect number of fields set: %" CeedInt_FMT " != 2\n", num_fields);
407bfe0f0eSJeremy L Thompson 
410f86cbe7SJeremy L Thompson   const char          *name;
427bfe0f0eSJeremy L Thompson   size_t               num_values;
430f86cbe7SJeremy L Thompson   CeedContextFieldType type;
44*249f8407SJeremy L Thompson 
451ff07f3dSJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &num_values, NULL, &type);
462b730f8bSJeremy L Thompson   if (strcmp(name, "time")) printf("Incorrect context field description for time: \"%s\" != \"time\"\n", name);
47*249f8407SJeremy L Thompson   if (num_values != 1) printf("Incorrect context field number of values for time: \"%zu\" != 1\n", num_values);
482b730f8bSJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_DOUBLE) {
490f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
502b730f8bSJeremy L Thompson     printf("Incorrect context field type for time: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
512b730f8bSJeremy L Thompson            CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
520f86cbe7SJeremy L Thompson     // LCOV_EXCL_STOP
532b730f8bSJeremy L Thompson   }
547bfe0f0eSJeremy L Thompson 
551ff07f3dSJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &num_values, NULL, &type);
562b730f8bSJeremy L Thompson   if (strcmp(name, "count")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
57*249f8407SJeremy L Thompson   if (num_values != 2) printf("Incorrect context field number of values for count: \"%zu\" != 2\n", num_values);
582b730f8bSJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_INT32) {
590f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
602b730f8bSJeremy L Thompson     printf("Incorrect context field type for count: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
612b730f8bSJeremy L Thompson            CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
620f86cbe7SJeremy L Thompson     // LCOV_EXCL_STOP
632b730f8bSJeremy L Thompson   }
64cdf32b93SJeremy L Thompson 
655b6ec284SJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[2], &name, NULL, &num_values, NULL, &type);
665b6ec284SJeremy L Thompson   if (strcmp(name, "is set")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
67*249f8407SJeremy L Thompson   if (num_values != 1) printf("Incorrect context field number of values for count: \"%zu\" != 2\n", num_values);
685b6ec284SJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_BOOL) {
695b6ec284SJeremy L Thompson     // LCOV_EXCL_START
705b6ec284SJeremy L Thompson     printf("Incorrect context field type for is_set: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
715b6ec284SJeremy L Thompson            CeedContextFieldTypes[CEED_CONTEXT_FIELD_BOOL]);
725b6ec284SJeremy L Thompson     // LCOV_EXCL_STOP
735b6ec284SJeremy L Thompson   }
745b6ec284SJeremy L Thompson 
753668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
767bfe0f0eSJeremy L Thompson   double value_time = 2.0;
77*249f8407SJeremy L Thompson 
787bfe0f0eSJeremy L Thompson   CeedQFunctionContextSetDouble(ctx, time_label, &value_time);
792b730f8bSJeremy L Thompson   if (ctx_data.time != 2.0) printf("Incorrect context data for time: %f != 2.0\n", ctx_data.time);
80cdf32b93SJeremy L Thompson 
813668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
827bfe0f0eSJeremy L Thompson   int values_count[2] = {14, 43};
83*249f8407SJeremy L Thompson 
847bfe0f0eSJeremy L Thompson   CeedQFunctionContextSetInt32(ctx, count_label, (int *)&values_count);
852b730f8bSJeremy L Thompson   if (ctx_data.count[0] != 14) printf("Incorrect context data for count[0]: %" CeedInt_FMT " != 14\n", ctx_data.count[0]);
862b730f8bSJeremy L Thompson   if (ctx_data.count[1] != 43) printf("Incorrect context data for count[1]: %" CeedInt_FMT " != 43\n", ctx_data.count[1]);
87cdf32b93SJeremy L Thompson 
885b6ec284SJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "is set", &is_set_label);
895b6ec284SJeremy L Thompson   bool value_is_set = false;
90*249f8407SJeremy L Thompson 
915b6ec284SJeremy L Thompson   CeedQFunctionContextSetBoolean(ctx, is_set_label, &value_is_set);
925b6ec284SJeremy L Thompson   if (ctx_data.is_set != false) printf("Incorrect context data for is_set: %d != true\n", ctx_data.is_set);
935b6ec284SJeremy L Thompson 
94cdf32b93SJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
95cdf32b93SJeremy L Thompson   CeedDestroy(&ceed);
96cdf32b93SJeremy L Thompson   return 0;
97cdf32b93SJeremy L Thompson }
98