libMesh
unique_ptr_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include <iomanip>
8 #include <libmesh/auto_ptr.h>
9 
10 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
11 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
12 // warnings. These can be ignored in GCC as long as we wrap the
13 // offending code in appropriate pragmas. We can't get away with a
14 // single ignore_warnings.h inclusion at the beginning of this file,
15 // since the libmesh headers pull in a restore_warnings.h at some
16 // point. We also don't bother restoring warnings at the end of this
17 // file since it's not a header.
18 #include <libmesh/ignore_warnings.h>
19 
20 using namespace libMesh;
21 
22 class UniquePtrTest : public CppUnit::TestCase
23 {
24 public:
25  CPPUNIT_TEST_SUITE( UniquePtrTest );
26 
27  // Test that we can call if (!foo) for UniquePtr
28  CPPUNIT_TEST( testComparison );
29 
30  // Test that the libmesh_make_unique macro works.
31  CPPUNIT_TEST( testMakeUnique );
32 
33  CPPUNIT_TEST_SUITE_END();
34 
35  // Note: this "extra" public declaration needs to be here, otherwise
36  // there is an unmatched private section coming from one of the
37  // macros above.
38 public:
39  void setUp ()
40  {}
41 
42  void tearDown ()
43  {}
44 
46  {
47  // Test that if(foo) and if (!foo) compile and do the right thing
48  // for UniquePtr. This tests that the safe_bool thing that our
49  // internal AutoPtr uses is actually working, in cases where users
50  // are configured to use that.
51  {
52  UniquePtr<int> foo(new int(42));
53 
54  bool test1_passed = false;
55  if (foo)
56  test1_passed = true;
57  CPPUNIT_ASSERT(test1_passed);
58 
59  bool test2_passed = true;
60  if (!foo)
61  test2_passed = false;
62  CPPUNIT_ASSERT(test2_passed);
63  }
64 
65  // Test the converse for when foo holds a NULL pointer.
66  {
67  UniquePtr<int> foo;
68 
69  bool test3_passed = true;
70  if (foo)
71  test3_passed = false;
72  CPPUNIT_ASSERT(test3_passed);
73 
74  bool test4_passed = false;
75  if (!foo)
76  test4_passed = true;
77  CPPUNIT_ASSERT(test4_passed);
78  }
79 
80  // Make sure that foo == bar is not allowed. This should not even
81  // compile -- I don't think that's possible to test with CPPUnit
82  // so it's commented out for now, but if you uncomment this test,
83  // you should get a compiler error. This is probably a candidate
84  // for a configure-time test instead.
85  // UniquePtr<int> bar(new int(21));
86  // if (foo == bar)
87  // std::cerr << "This should not compile." << std::endl;
88  }
89 
91  {
92  // If the compiler supports one of the ways of calling
93  // make_unique, test that our macro works for calling it works.
94 #if defined(LIBMESH_HAVE_CXX14_MAKE_UNIQUE) || defined(LIBMESH_HAVE_CXX11_MAKE_UNIQUE_WORKAROUND)
95  std::unique_ptr<int> foo = libmesh_make_unique<int>(42);
96 #endif
97  }
98 };
99 
void testMakeUnique()
void testComparison()
The libMesh namespace provides an interface to certain functionality in the library.
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
CPPUNIT_TEST_SUITE_REGISTRATION(UniquePtrTest)