QMCPACK
test_Array.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 #include <cstdio>
15 #include <vector>
16 #include <string>
17 
18 #include "OhmmsPETE/OhmmsArray.h"
19 
20 using std::string;
21 
22 namespace qmcplusplus
23 {
24 TEST_CASE("array", "[OhmmsPETE]")
25 {
26  using Array1D = Array<double, 1>;
27  Array1D A(3);
28  Array1D B(3);
29 
30  // iterator
31  auto ia = A.begin();
32  for (; ia != A.end(); ia++)
33  {
34  *ia = 1.0;
35  }
36 
37  // Assignment. This eventually generates a call to 'evaluate' in OhmmsVector.h
38  // To do: pointer to tutorial on expression template techniques
39  B = A;
40 
41  for (auto& element : B)
42  element *= 3.1;
43 
44  CHECK(B(0) == Approx(3.1));
45  CHECK(B(1) == Approx(3.1));
46  CHECK(B(2) == Approx(3.1));
47  REQUIRE(B == B);
48  REQUIRE(!(B == A));
49  REQUIRE(A != B);
50 }
51 
52 TEST_CASE("array NestedContainers", "[OhmmsPETE]")
53 {
54  Array<std::vector<int>, 1> vec_of_vecs({2});
55  vec_of_vecs(0).push_back(123);
56  vec_of_vecs.resize(5);
57  vec_of_vecs(0).clear();
58  vec_of_vecs(0).push_back(123);
59  vec_of_vecs.resize(0);
60  vec_of_vecs.resize(3);
61  vec_of_vecs(0).push_back(123);
62  CHECK(vec_of_vecs(0).back() == 123);
63 
64  Array<std::vector<int>, 1> vec_copy(vec_of_vecs);
65  REQUIRE(vec_copy.size() == 3);
66  REQUIRE(vec_copy(0).size() == 1);
67  CHECK(vec_copy(0).back() == 123);
68 
69  Array<std::vector<int>, 1> vec_assign;
70  vec_assign = vec_of_vecs;
71  REQUIRE(vec_copy.size() == 3);
72  REQUIRE(vec_copy(0).size() == 1);
73  CHECK(vec_copy(0).back() == 123);
74 }
75 
76 TEST_CASE("Array::data", "[OhmmsPETE]")
77 {
78  Array<float, 3> tensor(2, 4, 5);
79  REQUIRE(tensor.size() == 40);
80 
81  CHECK(tensor.data() + 1 * 4 * 5 + 2 * 5 + 3 == tensor.data_at(1, 2, 3));
82 
83  tensor(1, 2, 3) = 0.5f;
84  CHECK(*tensor.data_at(1, 2, 3) == 0.5f);
85 }
86 
87 TEST_CASE("Array::dimension sizes constructor", "[OhmmsPETE]")
88 {
89  const int dim = 2;
90  Array<double, 1> vec(dim);
91 
92  Array<double, 3> rank3_tensor(2, 4, 5);
93  CHECK(rank3_tensor.shape() == std::array<std::size_t, 3>{2, 4, 5});
94  // rank3_tensor.resize(5,6); this is caught at compile time.
95 }
96 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Type_t * data_at(const std::array< SIZET, D > &indices)
Definition: OhmmsArray.h:104
TEST_CASE("complex_helper", "[type_traits]")
Type_t * data()
Definition: OhmmsArray.h:87
void resize(const std::array< SIZET, D > &dims)
Resize the container.
Definition: OhmmsArray.h:65
REQUIRE(std::filesystem::exists(filename))
size_t size() const
Definition: OhmmsArray.h:57
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
double B(double x, int k, int i, const std::vector< double > &t)
const std::array< size_t, D > & shape() const
Definition: OhmmsArray.h:56