xref: /libCEED/tests/t404-qfunction.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1891038deSjeremylt /// @file
2891038deSjeremylt /// Test creation, setting, and taking data for QFunctionContext
3891038deSjeremylt /// \test Test creation, setting, and taking data for QFunctionContext
4891038deSjeremylt #include <ceed.h>
5891038deSjeremylt #include <math.h>
6891038deSjeremylt 
7891038deSjeremylt int main(int argc, char **argv) {
8891038deSjeremylt   Ceed                 ceed;
9891038deSjeremylt   CeedQFunctionContext ctx;
10*4fee36f0SJeremy L Thompson   CeedScalar           ctx_data[5] = {1, 2, 3, 4, 5}, *ctx_data_copy;
11891038deSjeremylt 
12891038deSjeremylt   CeedInit(argv[1], &ceed);
13891038deSjeremylt 
140f58c348SJeremy L Thompson   // Set borrowed pointer
15891038deSjeremylt   CeedQFunctionContextCreate(ceed, &ctx);
16*4fee36f0SJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(ctx_data), &ctx_data);
17891038deSjeremylt 
180f58c348SJeremy L Thompson   // Update borrowed pointer
19*4fee36f0SJeremy L Thompson   CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_copy);
20*4fee36f0SJeremy L Thompson   ctx_data_copy[4] = 6;
21*4fee36f0SJeremy L Thompson   CeedQFunctionContextRestoreData(ctx, &ctx_data_copy);
22*4fee36f0SJeremy L Thompson   if (ctx_data[4] != 6) printf("error modifying data: %f != 6.0\n", ctx_data[4]);
23891038deSjeremylt 
240f58c348SJeremy L Thompson   // Take back borrowed pointer
25*4fee36f0SJeremy L Thompson   CeedQFunctionContextTakeData(ctx, CEED_MEM_HOST, &ctx_data_copy);
26*4fee36f0SJeremy L Thompson   if (ctx_data_copy[4] != 6) printf("error accessing borrowed data: %f != 6.0\n", ctx_data_copy[4]);
270f58c348SJeremy L Thompson 
280f58c348SJeremy L Thompson   // Set copied data
29*4fee36f0SJeremy L Thompson   ctx_data[4] = 6;
30*4fee36f0SJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES, sizeof(ctx_data), &ctx_data);
310f58c348SJeremy L Thompson 
320f58c348SJeremy L Thompson   // Check copied data
33*4fee36f0SJeremy L Thompson   CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_copy);
34*4fee36f0SJeremy L Thompson   if (ctx_data_copy[4] != 6) printf("error accessing copied data: %f != 6.0\n", ctx_data_copy[4]);
35*4fee36f0SJeremy L Thompson   CeedQFunctionContextRestoreData(ctx, &ctx_data_copy);
360f58c348SJeremy L Thompson 
37891038deSjeremylt   CeedQFunctionContextDestroy(&ctx);
38891038deSjeremylt   CeedDestroy(&ceed);
39891038deSjeremylt   return 0;
40891038deSjeremylt }
41