xref: /libCEED/tests/t109-vector.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1 /// @file
2 /// Test CeedVectorSetArray to remove array access
3 /// \test Test CeedVectorSetArray to remove array access
4 #include <ceed.h>
5 #include <math.h>
6 
7 int main(int argc, char **argv) {
8   Ceed ceed;
9   CeedVector x;
10   const CeedInt n = 10;
11   CeedScalar a[n];
12   CeedScalar *b, *c;
13   const CeedScalar *d;
14 
15   CeedInit(argv[1], &ceed);
16 
17   CeedVectorCreate(ceed, n, &x);
18   for (CeedInt i=0; i<n; i++)
19     a[i] = 0;
20   a[3] = -3.14;
21   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
22 
23   // Taking array should return a
24   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
25   if (fabs(c[3] + 3.14) > 10.*CEED_EPSILON)
26     // LCOV_EXCL_START
27     printf("Error taking array c[3] = %f", (CeedScalar)c[3]);
28   // LCOV_EXCL_STOP
29 
30   // Getting array should not modify a
31   CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &b);
32   b[5] = -3.14;
33   CeedVectorRestoreArray(x, &b);
34 
35   if (fabs(a[5] + 3.14) < 10.*CEED_EPSILON)
36     // LCOV_EXCL_START
37     printf("Error protecting array a[3] = %f", (CeedScalar)a[3]);
38   // LCOV_EXCL_STOP
39 
40 // Note: We do not need to free c because c == a was stack allocated.
41   CeedVectorDestroy(&x);
42 
43   // Test with a size zero vector
44   CeedVectorCreate(ceed, 0, &x);
45   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, NULL);
46   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &d);
47   if (b) printf("CeedVectorGetArrayRead returned non-NULL for zero-sized Vector");
48   CeedVectorRestoreArrayRead(x, &d);
49   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
50   if (c) printf("CeedVectorTakeArray returned non-NULL for zero-sized Vector");
51   CeedVectorDestroy(&x);
52 
53   CeedDestroy(&ceed);
54   return 0;
55 }
56