xref: /libCEED/tests/t101-vector.c (revision d36c159f42e9222d4587decadab914cf55859f56)
1 /// @file
2 /// Test CeedVectorSetValue
3 /// \test Test CeedVectorSetValue
4 #include <ceed.h>
5 
6 static int CheckValues(Ceed ceed, CeedVector x, CeedScalar value) {
7   const CeedScalar *b;
8   CeedInt n;
9   CeedVectorGetLength(x, &n);
10   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &b);
11   for (CeedInt i=0; i<n; i++) {
12     if (b[i] != value)
13       // LCOV_EXCL_START
14       printf("Error reading array b[%d] = %f",i,
15              (double)b[i]);
16     // LCOV_EXCL_STOP
17   }
18   CeedVectorRestoreArrayRead(x, &b);
19   return 0;
20 }
21 
22 int main(int argc, char **argv) {
23   Ceed ceed;
24   CeedVector x;
25   CeedInt n;
26   CeedScalar a[10];
27   const CeedScalar *b;
28 
29   CeedInit(argv[1], &ceed);
30   n = 10;
31   CeedVectorCreate(ceed, n, &x);
32   for (CeedInt i=0; i<n; i++)
33     a[i] = 10 + i;
34   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
35 
36   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &b);
37   for (CeedInt i=0; i<n; i++)
38     if (b[i] != 10+i)
39       // LCOV_EXCL_START
40       printf("Error reading array b[%d] = %f",i,
41              (double)b[i]);
42   // LCOV_EXCL_STOP
43   CeedVectorRestoreArrayRead(x, &b);
44 
45   CeedVectorSetValue(x, 3.0);
46   CheckValues(ceed, x, 3.0);
47   CeedVectorDestroy(&x);
48 
49   CeedVectorCreate(ceed, n, &x);
50   // Set value before setting or getting the array
51   CeedVectorSetValue(x, 5.0);
52   CheckValues(ceed, x, 5.0);
53   CeedVectorDestroy(&x);
54 
55   CeedDestroy(&ceed);
56   return 0;
57 }
58