xref: /libCEED/tests/t109-vector.c (revision 9d488d037550e1c84877377029f08e264b1941e6)
1*9d488d03SJeremy L Thompson /// @file
2*9d488d03SJeremy L Thompson /// Test CeedVectorSetArray to remove array access
3*9d488d03SJeremy L Thompson /// \test Test CeedVectorSetArray to remove array access
4*9d488d03SJeremy L Thompson #include <ceed.h>
5*9d488d03SJeremy L Thompson #include <math.h>
6*9d488d03SJeremy L Thompson 
7*9d488d03SJeremy L Thompson int main(int argc, char **argv) {
8*9d488d03SJeremy L Thompson   Ceed ceed;
9*9d488d03SJeremy L Thompson   CeedVector x;
10*9d488d03SJeremy L Thompson   const CeedInt n = 10;
11*9d488d03SJeremy L Thompson   CeedScalar a[n];
12*9d488d03SJeremy L Thompson   CeedScalar *b, *c;
13*9d488d03SJeremy L Thompson 
14*9d488d03SJeremy L Thompson   CeedInit(argv[1], &ceed);
15*9d488d03SJeremy L Thompson 
16*9d488d03SJeremy L Thompson   CeedVectorCreate(ceed, n, &x);
17*9d488d03SJeremy L Thompson   for (CeedInt i=0; i<n; i++)
18*9d488d03SJeremy L Thompson     a[i] = 0;
19*9d488d03SJeremy L Thompson   a[3] = -3.14;
20*9d488d03SJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
21*9d488d03SJeremy L Thompson 
22*9d488d03SJeremy L Thompson   // Taking array should return a
23*9d488d03SJeremy L Thompson   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
24*9d488d03SJeremy L Thompson   if (fabs(c[3] + 3.14) > 1E-15)
25*9d488d03SJeremy L Thompson     // LCOV_EXCL_START
26*9d488d03SJeremy L Thompson     printf("Error taking array c[3] = %f", (double)c[3]);
27*9d488d03SJeremy L Thompson   // LCOV_EXCL_STOP
28*9d488d03SJeremy L Thompson 
29*9d488d03SJeremy L Thompson   // Getting array should not modify a
30*9d488d03SJeremy L Thompson   CeedVectorGetArray(x, CEED_MEM_HOST, &b);
31*9d488d03SJeremy L Thompson   b[5] = -3.14;
32*9d488d03SJeremy L Thompson   CeedVectorRestoreArray(x, &b);
33*9d488d03SJeremy L Thompson 
34*9d488d03SJeremy L Thompson   if (fabs(a[5] + 3.14) < 1E-15)
35*9d488d03SJeremy L Thompson     // LCOV_EXCL_START
36*9d488d03SJeremy L Thompson     printf("Error protecting array a[3] = %f", (double)a[3]);
37*9d488d03SJeremy L Thompson   // LCOV_EXCL_STOP
38*9d488d03SJeremy L Thompson 
39*9d488d03SJeremy L Thompson 
40*9d488d03SJeremy L Thompson 
41*9d488d03SJeremy L Thompson   CeedVectorDestroy(&x);
42*9d488d03SJeremy L Thompson   CeedDestroy(&ceed);
43*9d488d03SJeremy L Thompson   return 0;
44*9d488d03SJeremy L Thompson }
45