libMesh
Loading...
Searching...
No Matches
transparent_comparator.C
Go to the documentation of this file.
1// libMesh includes
2#include "libmesh/utility.h"
3
4// cppunit includes
5#include "libmesh_cppunit.h"
6
7// C++ includes
8#include <set>
9#include <memory>
10
11using namespace libMesh;
12
13class TransparentComparatorTest : public CppUnit::TestCase
14{
15public:
19
20 void testSet()
21 {
22 LOG_UNIT_TEST;
23
24 std::set<std::unique_ptr<int>, Utility::CompareUnderlying> s;
25 s.insert(std::make_unique<int>(1));
26 s.insert(std::make_unique<int>(2));
27 s.insert(std::make_unique<int>(3));
28 s.insert(std::make_unique<int>(3)); // Not a duplicate. This set does pointer comparisons, not value comparisons.
29
30 // Search for entry by pointer-to-int. This tests the desired
31 // behavior of the std::set comparison object.
32 int * first = s.begin()->get();
33 auto result = s.find(first);
34 CPPUNIT_ASSERT(result != s.end());
35 CPPUNIT_ASSERT(result->get() == first);
36
37 // Test set::count(). It should be using the same underlying
38 // machinery as std::set(find) but it's good to verify this.
39 CPPUNIT_ASSERT(s.count(first) == 1);
40
41 // Test that attempting to insert the same underlying pointer again fails. We simulate this by
42 // creating and moving from a standalone object.
43 auto four = std::make_unique<int>(4);
44
45 // The first insert() adds a non-nullptr underlying pointer to the
46 // set and leaves "four" with and underlying nullptr.
47 s.insert(std::move(four));
48 CPPUNIT_ASSERT(s.size() == 5);
49
50 // The second insert() adds a nullptr underlying pointer to the
51 // set and leaves "four" with and underlying nullptr.
52 s.insert(std::move(four));
53 CPPUNIT_ASSERT(s.size() == 6);
54
55 // The third insert() does not change the set because it tries to
56 // add a second nullptr underlying pointer to the set.
57 s.insert(std::move(four));
58 CPPUNIT_ASSERT(s.size() == 6);
59 }
60};
61
LIBMESH_CPPUNIT_TEST_SUITE(TransparentComparatorTest)
The libMesh namespace provides an interface to certain functionality in the library.
Struct which defines a custom comparison object that can be used with std::sets of std::unique_ptrs.
Definition utility.h:486
CPPUNIT_TEST_SUITE_REGISTRATION(TransparentComparatorTest)