Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or https://opensource.org/license/mit/.
4 : :
5 : : #include <sync.h>
6 : : #include <test/util/common.h>
7 : : #include <util/not_null.h>
8 : :
9 : : #include <boost/test/unit_test.hpp>
10 : :
11 : : #include <memory>
12 : : #include <set>
13 : : #include <type_traits>
14 : : #include <unordered_set>
15 : :
16 : : BOOST_AUTO_TEST_SUITE(util_not_null_tests)
17 : :
18 : : static_assert(std::is_same_v<util::NotNullUniquePtr<int>, util::NotNull<std::unique_ptr<int>>>);
19 : : static_assert(std::is_same_v<util::NotNullSharedPtr<int>, util::NotNull<std::shared_ptr<int>>>);
20 : :
21 : : template <class P>
22 : : concept NotNullConstructible = requires { typename std::bool_constant<(util::NotNull{P{}}, true)>; };
23 : :
24 [ + - + - : 6 : BOOST_AUTO_TEST_CASE(check_ctor)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
25 : : {
26 : 0 : struct SomePtr {
27 : : constexpr bool operator==(std::nullptr_t) const { return false; }
28 : : };
29 : 0 : struct NullPtr {
30 : : constexpr bool operator==(std::nullptr_t) const { return true; }
31 : : };
32 : :
33 : 0 : static_assert(NotNullConstructible<SomePtr>);
34 : 0 : static_assert(!NotNullConstructible<NullPtr>);
35 : 0 : static_assert(!NotNullConstructible<std::nullptr_t>);
36 : 0 : static_assert(!NotNullConstructible<int*>);
37 : 0 : }
38 : :
39 : : template <class T>
40 : : concept NotNullType = requires { typename util::NotNull<T>; };
41 : :
42 [ + - + - : 6 : BOOST_AUTO_TEST_CASE(check_ctor_ref)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
43 : : {
44 : 0 : static_assert(NotNullType<std::shared_ptr<int>>);
45 : :
46 : 0 : static_assert(!NotNullType<std::shared_ptr<int>&>);
47 : 0 : static_assert(!NotNullType<int*&>);
48 : 0 : }
49 : :
50 [ + - + - : 6 : BOOST_AUTO_TEST_CASE(check_ctor_conv)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
51 : : {
52 : 0 : using Ptr = std::shared_ptr<int>;
53 : 0 : using NNPtr = util::NotNullSharedPtr<int>;
54 : :
55 : : // For context: std::shared_ptr's raw pointer constructor is explicit
56 : 0 : static_assert(std::constructible_from<Ptr, int*>);
57 : 0 : static_assert(!std::convertible_to<int*, Ptr>);
58 : :
59 : : // util::NotNull's constructor is explicit
60 : 0 : static_assert(std::constructible_from<NNPtr, Ptr>);
61 : 0 : static_assert(!std::convertible_to<Ptr, NNPtr>);
62 : :
63 : : // For context: std::reference_wrapper's constructor is implicit
64 : 0 : using Ref = std::reference_wrapper<int>;
65 : 0 : static_assert(std::constructible_from<Ref, int&>);
66 : 0 : static_assert(std::convertible_to<int&, Ref>);
67 : :
68 : 0 : using UniqPtr = std::unique_ptr<int>;
69 : 0 : using NNUniqPtr = util::NotNullUniquePtr<int>;
70 : :
71 : : // NNPtr can be copied, but NNUniqPtr can not:
72 : 0 : static_assert(std::is_nothrow_constructible_v<NNPtr, NNPtr&>);
73 : 0 : static_assert(std::is_nothrow_constructible_v<NNPtr, const NNPtr&>);
74 : 0 : static_assert(std::is_nothrow_constructible_v<NNUniqPtr, NNUniqPtr&&>);
75 : 0 : static_assert(!std::is_constructible_v<NNUniqPtr, NNUniqPtr&>);
76 : 0 : static_assert(!std::is_constructible_v<NNUniqPtr, const NNUniqPtr&>);
77 : 0 : static_assert(!std::is_constructible_v<NNPtr, const NNUniqPtr&>);
78 : :
79 : : // NNPtr from NNUniqPtr may throw:
80 : 0 : static_assert(!std::is_nothrow_constructible_v<NNPtr, NNUniqPtr&&>);
81 : 0 : static_assert(std::is_constructible_v<NNPtr, NNUniqPtr&&>);
82 : : // NNUniqPtr -> NNPtr is implicit, just like the underlying conversion:
83 : 0 : static_assert(std::convertible_to<NNUniqPtr&&, NNPtr>);
84 : 0 : static_assert(std::convertible_to<UniqPtr&&, Ptr>);
85 : :
86 : : // Extraction is implicit:
87 : 0 : static_assert(std::convertible_to<NNUniqPtr&&, UniqPtr>);
88 : 0 : static_assert(std::convertible_to<const NNPtr&, Ptr>);
89 : 0 : static_assert(std::convertible_to<NNPtr&&, Ptr>);
90 : 0 : }
91 : :
92 : : template <class Target, class Source>
93 : : concept NotNullCopyConvertible = requires {
94 : : typename std::bool_constant<([] {
95 : : util::NotNull<Source> copy_source{Source{}};
96 : : [[maybe_unused]] util::NotNull<Target> copy_target{copy_source};
97 : : return true;
98 : : }())>;
99 : : };
100 : :
101 : : template <class Target, class Source>
102 : : concept NotNullMoveConvertible = requires { typename std::bool_constant<(util::NotNull<Target>{util::NotNull<Source>{Source{}}}, true)>; };
103 : :
104 [ + - + - : 6 : BOOST_AUTO_TEST_CASE(check_cross_type_conversion)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
105 : : {
106 : 0 : struct SomePtr;
107 : 0 : struct NullPtr {
108 : : constexpr NullPtr(const SomePtr&) {}
109 : : constexpr bool operator==(std::nullptr_t) const { return true; }
110 : : };
111 : 0 : struct SomePtr {
112 : : constexpr explicit operator bool() const { return true; }
113 : : constexpr bool operator==(std::nullptr_t) const { return false; }
114 : : };
115 : :
116 : : // Assume checks failed conversions during constant evaluation.
117 : 0 : static_assert(!NotNullCopyConvertible<NullPtr, SomePtr>);
118 : 0 : static_assert(!NotNullMoveConvertible<NullPtr, SomePtr>);
119 : 0 : }
120 : :
121 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_null_smart_pointer)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
122 : : {
123 : : // This conversion can throw, allowing the test hook to observe Assert.
124 : 1 : static_assert(!std::is_nothrow_constructible_v<util::NotNullSharedPtr<int>, std::unique_ptr<int>>);
125 [ + - ]: 1 : test_only_CheckFailuresAreExceptionsNotAborts mock_checks{};
126 [ + - - + : 3 : BOOST_CHECK_EXCEPTION(util::NotNullSharedPtr<int>{std::unique_ptr<int>{nullptr}}, NonFatalCheckError,
- - - - -
+ + - + -
+ - + - ]
127 : : HasReason{"Internal bug detected: `m_ptr != nullptr` check failed"});
128 : 1 : }
129 : :
130 : : template <class P>
131 : : concept NullptrComparable = requires(P val) { val == nullptr; };
132 : : template <class P>
133 : : concept HasOperatorBool = requires(P val) { bool{val}; };
134 : :
135 [ + - + - : 6 : BOOST_AUTO_TEST_CASE(check_nullptr_compare)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
136 : : {
137 : : // The deleted ctor for nullptr also disallows nullptr compare:
138 : 0 : static_assert(NullptrComparable<std::shared_ptr<int>>);
139 : 0 : static_assert(!NullptrComparable<util::NotNullSharedPtr<int>>);
140 : :
141 : 0 : static_assert(HasOperatorBool<std::shared_ptr<int>>);
142 : 0 : static_assert(!HasOperatorBool<util::NotNullSharedPtr<int>>);
143 : 0 : }
144 : :
145 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_nocopy_ptr)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
146 : : {
147 : 1 : using NoCopyPtr = std::unique_ptr<int>;
148 : 1 : {
149 : 1 : NoCopyPtr no_copy_ptr{std::make_unique<int>()};
150 : 1 : util::NotNull no_copy{std::move(no_copy_ptr)};
151 : 1 : NoCopyPtr extract{std::move(no_copy)};
152 : 1 : (void)extract;
153 : 1 : }
154 : 1 : {
155 : 1 : util::NotNull no_copy{NoCopyPtr{std::make_unique<int>()}};
156 : 1 : NoCopyPtr extract{std::move(no_copy).get()};
157 : 1 : (void)extract;
158 : 1 : }
159 : 1 : }
160 : :
161 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_derived)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
162 : : {
163 : 2 : struct MyBase {
164 : 2 : virtual ~MyBase() = default;
165 : : };
166 : 2 : struct MyDerived : public MyBase {
167 : : };
168 : :
169 [ - + ]: 1 : util::NotNull p{std::make_shared<MyDerived>()};
170 [ + - - + ]: 1 : util::NotNull q{std::make_shared<MyBase>()};
171 : : // Conversion is implicit:
172 : 1 : static_assert(std::convertible_to<const decltype(p)&, decltype(q)>);
173 : 1 : static_assert(std::is_assignable_v<decltype(q)&, const decltype(p)&>);
174 [ - + ]: 1 : q = p;
175 : 1 : const bool same_ptr{q == p};
176 [ + - + - : 2 : BOOST_CHECK(same_ptr);
+ - ]
177 : :
178 [ + - ]: 1 : util::NotNull nn_derived{std::make_unique<MyDerived>()};
179 [ + - ]: 1 : util::NotNull nn_base{std::make_unique<MyBase>()};
180 : 1 : nn_base = std::move(nn_derived);
181 : 1 : std::unique_ptr<MyBase> base_inner{std::move(nn_base)};
182 [ + - + - ]: 3 : }
183 : :
184 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_move)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
185 : : {
186 : 1 : auto a{std::make_unique<int>()};
187 : 1 : util::NotNull box{std::move(a)};
188 [ + - ]: 1 : auto new_box{std::move(box)};
189 : :
190 [ + - ]: 1 : auto b{std::make_shared<int>()};
191 : 1 : util::NotNull arc{b};
192 [ + - ]: 1 : auto new_arc{std::move(arc)};
193 [ - + + - ]: 2 : }
194 : :
195 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_swap)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
196 : : {
197 : 1 : util::NotNull box_a{std::make_unique<int>(1)};
198 [ + - ]: 1 : util::NotNull box_b{std::make_unique<int>(2)};
199 : 1 : static_assert(std::is_nothrow_swappable_v<decltype(box_a)>);
200 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(*box_a - *box_b, -1);
201 : 1 : swap(box_a, box_b);
202 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(*box_a - *box_b, 1);
203 : 1 : }
204 : :
205 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_deref)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
206 : : {
207 : 1 : util::NotNull p{std::make_unique<int>(2)};
208 [ + - ]: 1 : *p = 3;
209 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(*p, 3);
210 [ + - ]: 1 : *p.get() = 4;
211 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(*p, 4);
212 : 1 : }
213 : :
214 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_compare_set)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
215 : : {
216 : 1 : auto a{std::make_shared<int>(1)};
217 [ + - ]: 1 : auto b{std::make_shared<int>(2)};
218 [ + - ]: 1 : std::set<util::NotNullSharedPtr<int>> uniq{};
219 [ + - ]: 1 : uniq.emplace(a);
220 [ + - ]: 1 : uniq.emplace(a);
221 [ + - ]: 1 : uniq.emplace(b);
222 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(uniq.size(), 2);
223 [ + - + - ]: 3 : }
224 : :
225 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_hash_set)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
226 : : {
227 : 1 : auto a{std::make_shared<int>(1)};
228 [ + - ]: 1 : auto b{std::make_shared<int>(2)};
229 [ + - ]: 1 : std::unordered_set<util::NotNullSharedPtr<int>> uniq{};
230 [ + - ]: 1 : uniq.emplace(a);
231 [ + - ]: 1 : uniq.emplace(a);
232 [ + - ]: 1 : uniq.emplace(b);
233 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(uniq.size(), 2);
234 [ + - + - ]: 3 : }
235 : :
236 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(check_tsa)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
237 : : {
238 : 2 : struct Thing {
239 : : Mutex mutex{};
240 : : int a GUARDED_BY(mutex){2};
241 : : };
242 [ - + ]: 1 : const util::NotNull p{std::make_shared<Thing>()};
243 : : // Clang thread safety annotation (TSA) may not be able to derive that
244 : : // operator-> and operator* refer to the same object. So use a named
245 : : // reference before taking the lock.
246 [ + - ]: 1 : Thing& ref{*p};
247 [ + - ]: 1 : LOCK(ref.mutex);
248 [ + - + - : 1 : BOOST_CHECK_EQUAL(ref.a, 2);
+ - ]
249 [ + - ]: 2 : }
250 : :
251 : : BOOST_AUTO_TEST_SUITE_END()
|