xref: /libCEED/tests/t109-vector.c (revision e076c21946486f886b23b7fb4620ec99c3252fd7)
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;
13*e076c219SJed Brown   const CeedScalar *d;
149d488d03SJeremy L Thompson 
159d488d03SJeremy L Thompson   CeedInit(argv[1], &ceed);
169d488d03SJeremy L Thompson 
179d488d03SJeremy L Thompson   CeedVectorCreate(ceed, n, &x);
189d488d03SJeremy L Thompson   for (CeedInt i=0; i<n; i++)
199d488d03SJeremy L Thompson     a[i] = 0;
209d488d03SJeremy L Thompson   a[3] = -3.14;
219d488d03SJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
229d488d03SJeremy L Thompson 
239d488d03SJeremy L Thompson   // Taking array should return a
249d488d03SJeremy L Thompson   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
2580a9ef05SNatalie Beams   if (fabs(c[3] + 3.14) > 10.*CEED_EPSILON)
269d488d03SJeremy L Thompson     // LCOV_EXCL_START
2780a9ef05SNatalie Beams     printf("Error taking array c[3] = %f", (CeedScalar)c[3]);
289d488d03SJeremy L Thompson   // LCOV_EXCL_STOP
299d488d03SJeremy L Thompson 
309d488d03SJeremy L Thompson   // Getting array should not modify a
319c774eddSJeremy L Thompson   CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &b);
329d488d03SJeremy L Thompson   b[5] = -3.14;
339d488d03SJeremy L Thompson   CeedVectorRestoreArray(x, &b);
349d488d03SJeremy L Thompson 
3580a9ef05SNatalie Beams   if (fabs(a[5] + 3.14) < 10.*CEED_EPSILON)
369d488d03SJeremy L Thompson     // LCOV_EXCL_START
3780a9ef05SNatalie Beams     printf("Error protecting array a[3] = %f", (CeedScalar)a[3]);
389d488d03SJeremy L Thompson   // LCOV_EXCL_STOP
399d488d03SJeremy L Thompson 
40f2d6a716SJeremy L Thompson // Note: We do not need to free c because c == a was stack allocated.
419d488d03SJeremy L Thompson   CeedVectorDestroy(&x);
42*e076c219SJed Brown 
43*e076c219SJed Brown   // Test with a size zero vector
44*e076c219SJed Brown   CeedVectorCreate(ceed, 0, &x);
45*e076c219SJed Brown   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, NULL);
46*e076c219SJed Brown   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &d);
47*e076c219SJed Brown   if (b) printf("CeedVectorGetArrayRead returned non-NULL for zero-sized Vector");
48*e076c219SJed Brown   CeedVectorRestoreArrayRead(x, &d);
49*e076c219SJed Brown   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
50*e076c219SJed Brown   if (c) printf("CeedVectorTakeArray returned non-NULL for zero-sized Vector");
51*e076c219SJed Brown   CeedVectorDestroy(&x);
52*e076c219SJed Brown 
539d488d03SJeremy L Thompson   CeedDestroy(&ceed);
549d488d03SJeremy L Thompson   return 0;
559d488d03SJeremy L Thompson }
56