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