1 | // Copyright 2011, 2020 Peter Dimov |
2 | // Distributed under the Boost Software License, Version 1.0. |
3 | // https://www.boost.org/LICENSE_1_0.txt |
4 | |
5 | #include <boost/weak_ptr.hpp> |
6 | #include <boost/shared_ptr.hpp> |
7 | #include <boost/core/lightweight_test.hpp> |
8 | #include <boost/config.hpp> |
9 | |
10 | #if defined(BOOST_NO_CXX11_HDR_UNORDERED_SET) |
11 | |
12 | int main() {} |
13 | |
14 | #else |
15 | |
16 | #include <unordered_set> |
17 | |
18 | int main() |
19 | { |
20 | std::unordered_set< boost::weak_ptr<void> > set; |
21 | |
22 | boost::shared_ptr<int> p1( (int*)0 ); |
23 | boost::shared_ptr<int> p2( p1 ); |
24 | boost::shared_ptr<void> p3( p1 ); |
25 | |
26 | set.insert( x: p1 ); |
27 | set.insert( x: p2 ); |
28 | set.insert( x: p3 ); |
29 | |
30 | BOOST_TEST_EQ( set.size(), 1 ); |
31 | |
32 | boost::weak_ptr<int> q1( p1 ); |
33 | boost::weak_ptr<int> q2( p2 ); |
34 | boost::weak_ptr<void> q3( p3 ); |
35 | boost::weak_ptr<int> q4( q2 ); |
36 | boost::weak_ptr<void> q5( q3 ); |
37 | |
38 | set.insert( x: q1 ); |
39 | set.insert( x: q2 ); |
40 | set.insert( x: q3 ); |
41 | set.insert( x: q4 ); |
42 | set.insert( x: q5 ); |
43 | |
44 | BOOST_TEST_EQ( set.size(), 1 ); |
45 | |
46 | boost::shared_ptr<int> p6( (int*)0 ); |
47 | |
48 | set.insert( x: p6 ); |
49 | |
50 | BOOST_TEST_EQ( set.size(), 2 ); |
51 | |
52 | boost::weak_ptr<int> q6( p6 ); |
53 | |
54 | set.insert( x: q6 ); |
55 | |
56 | BOOST_TEST_EQ( set.size(), 2 ); |
57 | |
58 | BOOST_TEST_EQ( set.count( q1 ), 1 ); |
59 | BOOST_TEST_EQ( set.count( q2 ), 1 ); |
60 | BOOST_TEST_EQ( set.count( q3 ), 1 ); |
61 | BOOST_TEST_EQ( set.count( q4 ), 1 ); |
62 | BOOST_TEST_EQ( set.count( q5 ), 1 ); |
63 | BOOST_TEST_EQ( set.count( q6 ), 1 ); |
64 | |
65 | boost::shared_ptr<int> p7( (int*)0 ); |
66 | boost::weak_ptr<int> q7( p7 ); |
67 | |
68 | BOOST_TEST_EQ( set.count( q7 ), 0 ); |
69 | |
70 | p1.reset(); |
71 | p2.reset(); |
72 | p3.reset(); |
73 | p6.reset(); |
74 | p7.reset(); |
75 | |
76 | BOOST_TEST_EQ( set.count( q1 ), 1 ); |
77 | BOOST_TEST_EQ( set.count( q2 ), 1 ); |
78 | BOOST_TEST_EQ( set.count( q3 ), 1 ); |
79 | BOOST_TEST_EQ( set.count( q4 ), 1 ); |
80 | BOOST_TEST_EQ( set.count( q5 ), 1 ); |
81 | BOOST_TEST_EQ( set.count( q6 ), 1 ); |
82 | BOOST_TEST_EQ( set.count( q7 ), 0 ); |
83 | |
84 | return boost::report_errors(); |
85 | } |
86 | |
87 | #endif // #if defined(BOOST_NO_CXX11_HDR_UNORDERED_SET) |
88 | |