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