xref: /libCEED/tests/t407-qfunction.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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>
7cdf32b93SJeremy L Thompson #include <string.h>
8cdf32b93SJeremy L Thompson 
9cdf32b93SJeremy L Thompson typedef struct {
10cdf32b93SJeremy L Thompson   double time;
117bfe0f0eSJeremy L Thompson   int    count[2];
12cdf32b93SJeremy L Thompson } TestContext;
13cdf32b93SJeremy L Thompson 
14cdf32b93SJeremy L Thompson int main(int argc, char **argv) {
15cdf32b93SJeremy L Thompson   Ceed                  ceed;
16cdf32b93SJeremy L Thompson   CeedQFunctionContext  ctx;
173668ca4bSJeremy L Thompson   CeedContextFieldLabel time_label, count_label;
183668ca4bSJeremy L Thompson 
19cdf32b93SJeremy L Thompson   TestContext ctx_data = {
20cdf32b93SJeremy L Thompson       .time  = 1.0,
217bfe0f0eSJeremy L Thompson       .count = {13, 42},
22cdf32b93SJeremy L Thompson   };
23cdf32b93SJeremy L Thompson 
24cdf32b93SJeremy L Thompson   CeedInit(argv[1], &ceed);
25cdf32b93SJeremy L Thompson 
26cdf32b93SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
27*2b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext), &ctx_data);
28cdf32b93SJeremy L Thompson 
29*2b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time), 1, "current time");
30*2b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count), 2, "some sort of counter");
31cdf32b93SJeremy L Thompson 
323668ca4bSJeremy L Thompson   const CeedContextFieldLabel *field_labels;
33cdf32b93SJeremy L Thompson   CeedInt                      num_fields;
343668ca4bSJeremy L Thompson   CeedQFunctionContextGetAllFieldLabels(ctx, &field_labels, &num_fields);
35*2b730f8bSJeremy L Thompson   if (num_fields != 2) printf("Incorrect number of fields set: %" CeedInt_FMT " != 2\n", num_fields);
367bfe0f0eSJeremy L Thompson 
370f86cbe7SJeremy L Thompson   const char          *name;
387bfe0f0eSJeremy L Thompson   size_t               num_values;
390f86cbe7SJeremy L Thompson   CeedContextFieldType type;
40*2b730f8bSJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[0], &name, NULL, &num_values, &type);
41*2b730f8bSJeremy L Thompson   if (strcmp(name, "time")) printf("Incorrect context field description for time: \"%s\" != \"time\"\n", name);
42*2b730f8bSJeremy L Thompson   if (num_values != 1) printf("Incorrect context field number of values for time: \"%ld\" != 1\n", num_values);
43*2b730f8bSJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_DOUBLE) {
440f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
45*2b730f8bSJeremy L Thompson     printf("Incorrect context field type for time: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
46*2b730f8bSJeremy L Thompson            CeedContextFieldTypes[CEED_CONTEXT_FIELD_DOUBLE]);
470f86cbe7SJeremy L Thompson     // LCOV_EXCL_STOP
48*2b730f8bSJeremy L Thompson   }
497bfe0f0eSJeremy L Thompson 
50*2b730f8bSJeremy L Thompson   CeedContextFieldLabelGetDescription(field_labels[1], &name, NULL, &num_values, &type);
51*2b730f8bSJeremy L Thompson   if (strcmp(name, "count")) printf("Incorrect context field description for count: \"%s\" != \"count\"\n", name);
52*2b730f8bSJeremy L Thompson   if (num_values != 2) printf("Incorrect context field number of values for count: \"%ld\" != 2\n", num_values);
53*2b730f8bSJeremy L Thompson   if (type != CEED_CONTEXT_FIELD_INT32) {
540f86cbe7SJeremy L Thompson     // LCOV_EXCL_START
55*2b730f8bSJeremy L Thompson     printf("Incorrect context field type for count: \"%s\" != \"%s\"\n", CeedContextFieldTypes[type],
56*2b730f8bSJeremy L Thompson            CeedContextFieldTypes[CEED_CONTEXT_FIELD_INT32]);
570f86cbe7SJeremy L Thompson     // LCOV_EXCL_STOP
58*2b730f8bSJeremy L Thompson   }
59cdf32b93SJeremy L Thompson 
603668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "time", &time_label);
617bfe0f0eSJeremy L Thompson   double value_time = 2.0;
627bfe0f0eSJeremy L Thompson   CeedQFunctionContextSetDouble(ctx, time_label, &value_time);
63*2b730f8bSJeremy L Thompson   if (ctx_data.time != 2.0) printf("Incorrect context data for time: %f != 2.0\n", ctx_data.time);
64cdf32b93SJeremy L Thompson 
653668ca4bSJeremy L Thompson   CeedQFunctionContextGetFieldLabel(ctx, "count", &count_label);
667bfe0f0eSJeremy L Thompson   int values_count[2] = {14, 43};
677bfe0f0eSJeremy L Thompson   CeedQFunctionContextSetInt32(ctx, count_label, (int *)&values_count);
68*2b730f8bSJeremy L Thompson   if (ctx_data.count[0] != 14) printf("Incorrect context data for count[0]: %" CeedInt_FMT " != 14\n", ctx_data.count[0]);
69*2b730f8bSJeremy L Thompson   if (ctx_data.count[1] != 43) printf("Incorrect context data for count[1]: %" CeedInt_FMT " != 43\n", ctx_data.count[1]);
70cdf32b93SJeremy L Thompson 
71cdf32b93SJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
72cdf32b93SJeremy L Thompson   CeedDestroy(&ceed);
73cdf32b93SJeremy L Thompson   return 0;
74cdf32b93SJeremy L Thompson }
75