xref: /libCEED/tests/t407-qfunction.c (revision d8dd9a912dc9d585dc5c0667639a4157f4b711a3)
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;
16cdf32b93SJeremy L Thompson   TestContext ctx_data = {
17cdf32b93SJeremy L Thompson     .time = 1.0,
18cdf32b93SJeremy L Thompson     .count = 42
19cdf32b93SJeremy L Thompson   };
20cdf32b93SJeremy L Thompson 
21cdf32b93SJeremy L Thompson   CeedInit(argv[1], &ceed);
22cdf32b93SJeremy L Thompson 
23cdf32b93SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
24cdf32b93SJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER,
25cdf32b93SJeremy L Thompson                               sizeof(TestContext), &ctx_data);
26cdf32b93SJeremy L Thompson 
27cdf32b93SJeremy L Thompson   CeedQFunctionContextRegisterDouble(ctx, "time", offsetof(TestContext, time),
28cdf32b93SJeremy L Thompson                                      "current time");
29cdf32b93SJeremy L Thompson   CeedQFunctionContextRegisterInt32(ctx, "count", offsetof(TestContext, count),
30cdf32b93SJeremy L Thompson                                     "some sort of counter");
31cdf32b93SJeremy L Thompson 
32cdf32b93SJeremy L Thompson   const CeedQFunctionContextFieldDescription *field_descriptions;
33cdf32b93SJeremy L Thompson   CeedInt num_fields;
34cdf32b93SJeremy L Thompson   CeedQFunctionContextGetFieldDescriptions(ctx, &field_descriptions, &num_fields);
35cdf32b93SJeremy L Thompson   if (num_fields != 2)
36cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
37cdf32b93SJeremy L Thompson     printf("Incorrect number of fields set: %d != 2", num_fields);
38cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
39cdf32b93SJeremy L Thompson   if (strcmp(field_descriptions[0].name, "time"))
40cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
41cdf32b93SJeremy L Thompson     printf("Incorrect context field description for time: \"%s\" != \"time\"",
42cdf32b93SJeremy L Thompson            field_descriptions[0].name);
43cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
44cdf32b93SJeremy L Thompson   if (strcmp(field_descriptions[1].name, "count"))
45cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
46cdf32b93SJeremy L Thompson     printf("Incorrect context field description for time: \"%s\" != \"count\"",
47cdf32b93SJeremy L Thompson            field_descriptions[1].name);
48cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
49cdf32b93SJeremy L Thompson 
50cdf32b93SJeremy L Thompson   CeedQFunctionContextSetDouble(ctx, "time", 2.0);
51cdf32b93SJeremy L Thompson   if (ctx_data.time != 2.0)
52cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
53cdf32b93SJeremy L Thompson     printf("Incorrect context data for time: %f != 2.0", ctx_data.time);
54cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
55cdf32b93SJeremy L Thompson 
56cdf32b93SJeremy L Thompson   CeedQFunctionContextSetInt32(ctx, "count", 43);
57cdf32b93SJeremy L Thompson   if (ctx_data.count != 43)
58cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
59*d8dd9a91SJeremy L Thompson     printf("Incorrect context data for count: %d != 43", ctx_data.count);
60cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
61cdf32b93SJeremy L Thompson 
62cdf32b93SJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
63cdf32b93SJeremy L Thompson   CeedDestroy(&ceed);
64cdf32b93SJeremy L Thompson   return 0;
65cdf32b93SJeremy L Thompson }
66