xref: /libCEED/tests/t407-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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>
7*49aac155SJeremy 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];
13cdf32b93SJeremy L Thompson } TestContext;
14cdf32b93SJeremy L Thompson 
15cdf32b93SJeremy L Thompson int main(int argc, char **argv) {
16cdf32b93SJeremy L Thompson   Ceed                  ceed;
17cdf32b93SJeremy L Thompson   CeedQFunctionContext  ctx;
183668ca4bSJeremy L Thompson   CeedContextFieldLabel time_label, count_label;
193668ca4bSJeremy L Thompson 
20cdf32b93SJeremy L Thompson   TestContext ctx_data = {
21cdf32b93SJeremy L Thompson       .time  = 1.0,
227bfe0f0eSJeremy L Thompson       .count = {13, 42},
23cdf32b93SJeremy L Thompson   };
24cdf32b93SJeremy L Thompson 
25cdf32b93SJeremy L Thompson   CeedInit(argv[1], &ceed);
26cdf32b93SJeremy L Thompson 
27cdf32b93SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
282b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext), &ctx_data);
29cdf32b93SJeremy L Thompson 
302b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time), 1, "current time");
312b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count), 2, "some sort of counter");
32cdf32b93SJeremy L Thompson 
333668ca4bSJeremy L Thompson   const CeedContextFieldLabel *field_labels;
34cdf32b93SJeremy L Thompson   CeedInt                      num_fields;
353668ca4bSJeremy L Thompson   CeedQFunctionContextGetAllFieldLabels(ctx, &field_labels, &num_fields);
362b730f8bSJeremy L Thompson   if (num_fields != 2) printf("Incorrect number of fields set: %" CeedInt_FMT " != 2\n", num_fields);
377bfe0f0eSJeremy L Thompson 
380f86cbe7SJeremy L Thompson   const char          *name;
397bfe0f0eSJeremy L Thompson   size_t               num_values;
400f86cbe7SJeremy L Thompson   CeedContextFieldType type;
412b730f8bSJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &num_values, &type);
422b730f8bSJeremy L Thompson   if (strcmp(name, "time")) printf("Incorrect context field description for time: \"%s\" != \"time\"\n", name);
432b730f8bSJeremy L Thompson   if (num_values != 1) printf("Incorrect context field number of values for time: \"%ld\" != 1\n", num_values);
442b730f8bSJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_DOUBLE) {
450f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
462b730f8bSJeremy L Thompson     printf("Incorrect context field type for time: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
472b730f8bSJeremy L Thompson            CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
480f86cbe7SJeremy L Thompson     // LCOV_EXCL_STOP
492b730f8bSJeremy L Thompson   }
507bfe0f0eSJeremy L Thompson 
512b730f8bSJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &num_values, &type);
522b730f8bSJeremy L Thompson   if (strcmp(name, "count")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
532b730f8bSJeremy L Thompson   if (num_values != 2) printf("Incorrect context field number of values for count: \"%ld\" != 2\n", num_values);
542b730f8bSJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_INT32) {
550f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
562b730f8bSJeremy L Thompson     printf("Incorrect context field type for count: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
572b730f8bSJeremy L Thompson            CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
580f86cbe7SJeremy L Thompson     // LCOV_EXCL_STOP
592b730f8bSJeremy L Thompson   }
60cdf32b93SJeremy L Thompson 
613668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
627bfe0f0eSJeremy L Thompson   double value_time = 2.0;
637bfe0f0eSJeremy L Thompson   CeedQFunctionContextSetDouble(ctx, time_label, &value_time);
642b730f8bSJeremy L Thompson   if (ctx_data.time != 2.0) printf("Incorrect context data for time: %f != 2.0\n", ctx_data.time);
65cdf32b93SJeremy L Thompson 
663668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
677bfe0f0eSJeremy L Thompson   int values_count[2] = {14, 43};
687bfe0f0eSJeremy L Thompson   CeedQFunctionContextSetInt32(ctx, count_label, (int *)&values_count);
692b730f8bSJeremy L Thompson   if (ctx_data.count[0] != 14) printf("Incorrect context data for count[0]: %" CeedInt_FMT " != 14\n", ctx_data.count[0]);
702b730f8bSJeremy L Thompson   if (ctx_data.count[1] != 43) printf("Incorrect context data for count[1]: %" CeedInt_FMT " != 43\n", ctx_data.count[1]);
71cdf32b93SJeremy L Thompson 
72cdf32b93SJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
73cdf32b93SJeremy L Thompson   CeedDestroy(&ceed);
74cdf32b93SJeremy L Thompson   return 0;
75cdf32b93SJeremy L Thompson }
76