xref: /libCEED/tests/t109-vector.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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>
6*49aac155SJeremy L Thompson #include <stdio.h>
79d488d03SJeremy L Thompson 
main(int argc,char ** argv)89d488d03SJeremy L Thompson int main(int argc, char **argv) {
99d488d03SJeremy L Thompson   Ceed          ceed;
109d488d03SJeremy L Thompson   CeedVector    x;
114fee36f0SJeremy L Thompson   const CeedInt len = 10;
124fee36f0SJeremy L Thompson   CeedScalar    array[len];
139d488d03SJeremy L Thompson 
149d488d03SJeremy L Thompson   CeedInit(argv[1], &ceed);
159d488d03SJeremy L Thompson 
164fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, len, &x);
174fee36f0SJeremy L Thompson   for (CeedInt i = 0; i < len; i++) array[i] = 0;
184fee36f0SJeremy L Thompson   array[3] = -3.14;
194fee36f0SJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, array);
209d488d03SJeremy L Thompson 
219d488d03SJeremy L Thompson   // Taking array should return a
224fee36f0SJeremy L Thompson   {
234fee36f0SJeremy L Thompson     CeedScalar *array;
244fee36f0SJeremy L Thompson 
254fee36f0SJeremy L Thompson     CeedVectorTakeArray(x, CEED_MEM_HOST, &array);
264fee36f0SJeremy L Thompson     if (fabs(array[3] + 3.14) > 10. * CEED_EPSILON) printf("Error taking array, array[3] = %f\n", (CeedScalar)array[3]);
274fee36f0SJeremy L Thompson   }
289d488d03SJeremy L Thompson 
299d488d03SJeremy L Thompson   // Getting array should not modify a
304fee36f0SJeremy L Thompson   {
314fee36f0SJeremy L Thompson     CeedScalar *writable_array;
329d488d03SJeremy L Thompson 
334fee36f0SJeremy L Thompson     CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &writable_array);
344fee36f0SJeremy L Thompson     writable_array[5] = -3.14;
354fee36f0SJeremy L Thompson     CeedVectorRestoreArray(x, &writable_array);
364fee36f0SJeremy L Thompson   }
374fee36f0SJeremy L Thompson   if (fabs(array[5] + 3.14) < 10. * CEED_EPSILON) printf("Error protecting array, array[3] = %f\n", (CeedScalar)array[3]);
389d488d03SJeremy L Thompson 
399d488d03SJeremy L Thompson   CeedVectorDestroy(&x);
40e076c219SJed Brown 
41e076c219SJed Brown   // Test with a size zero vector
42e076c219SJed Brown   CeedVectorCreate(ceed, 0, &x);
43e076c219SJed Brown   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, NULL);
444fee36f0SJeremy L Thompson   {
454fee36f0SJeremy L Thompson     const CeedScalar *read_array;
464fee36f0SJeremy L Thompson 
474fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
484fee36f0SJeremy L Thompson     if (read_array) printf("CeedVectorGetArrayRead returned non-NULL for zero-sized Vector\n");
494fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(x, &read_array);
504fee36f0SJeremy L Thompson   }
514fee36f0SJeremy L Thompson   {
524fee36f0SJeremy L Thompson     CeedScalar *array;
534fee36f0SJeremy L Thompson 
544fee36f0SJeremy L Thompson     CeedVectorTakeArray(x, CEED_MEM_HOST, &array);
554fee36f0SJeremy L Thompson     if (array) printf("CeedVectorTakeArray returned non-NULL for zero-sized Vector\n");
564fee36f0SJeremy L Thompson   }
57e076c219SJed Brown   CeedVectorDestroy(&x);
58e076c219SJed Brown 
599d488d03SJeremy L Thompson   CeedDestroy(&ceed);
609d488d03SJeremy L Thompson   return 0;
619d488d03SJeremy L Thompson }
62