value_ptr_test.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // test value_ptr
4 //
5 // ======================================================================
6 
7 #define BOOST_TEST_MODULE (value_ptr test)
8 #include "boost/test/unit_test.hpp"
9 #include <ostream>
10 #include <string>
11 
12 #include "cetlib/value_ptr.h"
13 
14 namespace cet {
15  std::ostream&
16  boost_test_print_type(std::ostream& os, value_ptr<int> const p)
17  {
18  return os << p.get();
19  }
20 }
21 
22 using cet::default_clone;
23 using cet::default_copy;
24 using cet::value_ptr;
25 
26 struct Base {
27  virtual std::string
28  who() const
29  {
30  return "Base";
31  }
32  virtual ~Base() noexcept = default;
33 };
34 
35 struct Derived : public Base {
37  who() const override
38  {
39  return "Derived";
40  }
41 };
42 
43 BOOST_AUTO_TEST_SUITE(value_ptr_test)
44 
45 BOOST_AUTO_TEST_CASE(nullptr_test)
46 {
48  BOOST_TEST(!p);
49  BOOST_TEST(p == nullptr);
50  BOOST_TEST(nullptr == p);
51 }
52 
54 {
55  value_ptr<int> p(new int(16));
56  BOOST_TEST(static_cast<bool>(p));
57  BOOST_TEST(*p == 16);
58 
59  int* p_addr = p.get();
60  value_ptr<int> q(p);
61  BOOST_TEST(*p == *q);
62  BOOST_TEST(p != q);
63  BOOST_TEST(p_addr != q.get());
64 
65  p.reset(new int(0));
66  BOOST_TEST(*p == 0);
67  BOOST_TEST(p_addr != p.get());
68 }
69 
70 BOOST_AUTO_TEST_CASE(compile_failure_test)
71 {
72 #if 0
73  value_ptr<double> p( new int(16) ); // unrelated types
74  value_ptr<Base> b( new Derived ); // polymorphic, but no clone()
75  value_ptr<Derived> d( new Base ); // no conversion in this direction
76 #endif
77 }
78 
79 BOOST_AUTO_TEST_CASE(polymorphism_test)
80 {
82  BOOST_TEST(bool(b));
83  BOOST_TEST(b->who() == std::string("Base"));
84 
85  value_ptr<Base, default_copy<Base> // request slicing
86  >
87  d(new Derived);
88  BOOST_TEST(bool(d));
89  BOOST_TEST(d->who() == std::string("Derived"));
90 }
91 
92 BOOST_AUTO_TEST_SUITE_END()
std::string string
Definition: nybbler.cc:12
BOOST_AUTO_TEST_CASE(nullptr_test)
virtual std::string who() const
std::ostream & boost_test_print_type(std::ostream &os, value_ptr< int > const p)
p
Definition: test.py:223
std::string who() const override
static bool * b
Definition: config.cpp:1043
void reset(pointer t=pointer()) noexcept
Definition: value_ptr.h:304
pointer get() const noexcept
Definition: value_ptr.h:288