xref: /libCEED/tests/t109-vector.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
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 len = 10;
11   CeedScalar    array[len];
12 
13   CeedInit(argv[1], &ceed);
14 
15   CeedVectorCreate(ceed, len, &x);
16   for (CeedInt i = 0; i < len; i++) array[i] = 0;
17   array[3] = -3.14;
18   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, array);
19 
20   // Taking array should return a
21   {
22     CeedScalar *array;
23 
24     CeedVectorTakeArray(x, CEED_MEM_HOST, &array);
25     if (fabs(array[3] + 3.14) > 10. * CEED_EPSILON) printf("Error taking array, array[3] = %f\n", (CeedScalar)array[3]);
26   }
27 
28   // Getting array should not modify a
29   {
30     CeedScalar *writable_array;
31 
32     CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &writable_array);
33     writable_array[5] = -3.14;
34     CeedVectorRestoreArray(x, &writable_array);
35   }
36   if (fabs(array[5] + 3.14) < 10. * CEED_EPSILON) printf("Error protecting array, array[3] = %f\n", (CeedScalar)array[3]);
37 
38   CeedVectorDestroy(&x);
39 
40   // Test with a size zero vector
41   CeedVectorCreate(ceed, 0, &x);
42   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, NULL);
43   {
44     const CeedScalar *read_array;
45 
46     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
47     if (read_array) printf("CeedVectorGetArrayRead returned non-NULL for zero-sized Vector\n");
48     CeedVectorRestoreArrayRead(x, &read_array);
49   }
50   {
51     CeedScalar *array;
52 
53     CeedVectorTakeArray(x, CEED_MEM_HOST, &array);
54     if (array) printf("CeedVectorTakeArray returned non-NULL for zero-sized Vector\n");
55   }
56   CeedVectorDestroy(&x);
57 
58   CeedDestroy(&ceed);
59   return 0;
60 }
61