QMCPACK
test_template_types.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) 2019 QMCPACK developers
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
8 //
9 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include <complex>
13 #include "catch.hpp"
15 
16 
17 namespace qmcplusplus
18 {
19 
20 TEST_CASE("makeRefVector", "[type_traits]")
21 {
22  struct Dummy
23  {
24  double d;
25  std::string s;
26  };
27 
28  struct DerivedDummy : Dummy
29  {
30  float f;
31  };
32 
33  std::vector<DerivedDummy> ddvec;
34  for (int i = 0; i < 3; ++i)
35  ddvec.push_back(DerivedDummy());
36 
37  auto bdum = makeRefVector<Dummy>(ddvec);
38  auto bdum2 = makeRefVector<Dummy>(ddvec);
39  CHECK(std::is_same<RefVector<Dummy>, decltype(bdum2)>::value);
40 
41  auto bdum3 = makeRefVector<decltype(ddvec)::value_type>(ddvec);
42 }
43 
44 TEST_CASE("convertUPtrToRefvector", "[type_traits]")
45 {
46  struct Dummy
47  {
48  double d;
49  std::string s;
50  };
51 
52  struct DerivedDummy : public Dummy
53  {
54  DerivedDummy() : e(0) {}
55  double e;
56  };
57 
58  struct OtherDummy
59  {
60  double f;
61  };
62 
63  UPtrVector<Dummy> uvec;
64  for (int i = 0; i < 3; ++i)
65  uvec.emplace_back(std::make_unique<Dummy>());
66 
68  auto rdum2 = convertUPtrToRefVector(uvec);
69  CHECK(std::is_same_v<decltype(rdum), decltype(rdum2)>);
70  // Testing to make sure meta programming stops potential ambiguous template resolution.
72  ddv.emplace_back(std::make_unique<DerivedDummy>());
73  ddv.emplace_back(std::make_unique<DerivedDummy>());
74 
75  auto dummy_ref_vec = convertUPtrToRefVector(ddv);
76  auto d_ref_vec = convertUPtrToRefVector<Dummy>(ddv);
77  auto dd_ref_vec = convertUPtrToRefVector(ddv);
78  CHECK(!std::is_same_v<decltype(d_ref_vec), decltype(dd_ref_vec)>);
79 
80  // This should cause a compilation error. a DerivedDummy cannot be converted to an OtherDummy.
81  // RefVector<OtherDummy> od_ref_vec = convertUPtrToRefVector<OtherDummy>(ddv);
82 }
83 
84 TEST_CASE("convertPtrToRefvectorSubset", "[type_traits]")
85 {
86  struct Dummy2
87  {
88  Dummy2(int j) : i(j) {}
89  int i;
90  };
91 
92  std::vector<Dummy2*> pvec;
93  for (int i = 0; i < 5; ++i)
94  pvec.push_back(new Dummy2(i));
95 
96  auto rdum = convertPtrToRefVectorSubset(pvec, 1, 4);
97 
98  CHECK(rdum.size() == 4);
99  CHECK(rdum[0].get().i == 1);
100 
101  for (int i = 0; i < 5; ++i)
102  delete pvec[i];
103 }
104 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
static RefVector< T > convertPtrToRefVectorSubset(const std::vector< T *> &ptr_list, int offset, int len)
TEST_CASE("complex_helper", "[type_traits]")
bool is_same(const xmlChar *a, const char *b)
std::vector< std::unique_ptr< T > > UPtrVector
static RefVector< T > convertUPtrToRefVector(const UPtrVector< T > &ptr_list)
convert a vector of std::unique_ptrs<T> to a refvector<T>
std::vector< std::reference_wrapper< T > > RefVector
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))