xref: /libCEED/tests/t109-vector.c (revision 80a9ef0545a39c00cdcaab1ca26f8053604f3120)
19d488d03SJeremy L Thompson /// @file
29d488d03SJeremy L Thompson /// Test CeedVectorSetArray to remove array access
39d488d03SJeremy L Thompson /// \test Test CeedVectorSetArray to remove array access
49d488d03SJeremy L Thompson #include <ceed.h>
59d488d03SJeremy L Thompson #include <math.h>
69d488d03SJeremy L Thompson 
79d488d03SJeremy L Thompson int main(int argc, char **argv) {
89d488d03SJeremy L Thompson   Ceed ceed;
99d488d03SJeremy L Thompson   CeedVector x;
109d488d03SJeremy L Thompson   const CeedInt n = 10;
119d488d03SJeremy L Thompson   CeedScalar a[n];
129d488d03SJeremy L Thompson   CeedScalar *b, *c;
139d488d03SJeremy L Thompson 
149d488d03SJeremy L Thompson   CeedInit(argv[1], &ceed);
159d488d03SJeremy L Thompson 
169d488d03SJeremy L Thompson   CeedVectorCreate(ceed, n, &x);
179d488d03SJeremy L Thompson   for (CeedInt i=0; i<n; i++)
189d488d03SJeremy L Thompson     a[i] = 0;
199d488d03SJeremy L Thompson   a[3] = -3.14;
209d488d03SJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
219d488d03SJeremy L Thompson 
229d488d03SJeremy L Thompson   // Taking array should return a
239d488d03SJeremy L Thompson   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
24*80a9ef05SNatalie Beams   if (fabs(c[3] + 3.14) > 10.*CEED_EPSILON)
259d488d03SJeremy L Thompson     // LCOV_EXCL_START
26*80a9ef05SNatalie Beams     printf("Error taking array c[3] = %f", (CeedScalar)c[3]);
279d488d03SJeremy L Thompson   // LCOV_EXCL_STOP
289d488d03SJeremy L Thompson 
299d488d03SJeremy L Thompson   // Getting array should not modify a
309d488d03SJeremy L Thompson   CeedVectorGetArray(x, CEED_MEM_HOST, &b);
319d488d03SJeremy L Thompson   b[5] = -3.14;
329d488d03SJeremy L Thompson   CeedVectorRestoreArray(x, &b);
339d488d03SJeremy L Thompson 
34*80a9ef05SNatalie Beams   if (fabs(a[5] + 3.14) < 10.*CEED_EPSILON)
359d488d03SJeremy L Thompson     // LCOV_EXCL_START
36*80a9ef05SNatalie Beams     printf("Error protecting array a[3] = %f", (CeedScalar)a[3]);
379d488d03SJeremy L Thompson   // LCOV_EXCL_STOP
389d488d03SJeremy L Thompson 
39f2d6a716SJeremy L Thompson // Note: We do not need to free c because c == a was stack allocated.
40f2d6a716SJeremy L Thompson //         If libCEED allocated c, then free() would be required.
419d488d03SJeremy L Thompson   CeedVectorDestroy(&x);
429d488d03SJeremy L Thompson   CeedDestroy(&ceed);
439d488d03SJeremy L Thompson   return 0;
449d488d03SJeremy L Thompson }
45