notnull_tests.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <gtest/gtest.h>
  17. #include <gsl/pointers> // for not_null, operator<, operator<=, operator>
  18. #include <algorithm> // for addressof
  19. #include <cstdint> // for uint16_t
  20. #include <memory> // for shared_ptr, make_shared, operator<, opera...
  21. #include <sstream> // for operator<<, ostringstream, basic_ostream:...
  22. #include <string> // for basic_string, operator==, string, operator<<
  23. #include <typeinfo> // for type_info
  24. #include <variant> // for variant, monostate, get
  25. #include "deathTestCommon.h"
  26. using namespace gsl;
  27. struct MyBase
  28. {
  29. };
  30. struct MyDerived : public MyBase
  31. {
  32. };
  33. struct Unrelated
  34. {
  35. };
  36. // stand-in for a user-defined ref-counted class
  37. template <typename T>
  38. struct RefCounted
  39. {
  40. RefCounted(T* p) : p_(p) {}
  41. operator T*() { return p_; }
  42. T* p_;
  43. };
  44. // user defined smart pointer with comparison operators returning non bool value
  45. template <typename T>
  46. struct CustomPtr
  47. {
  48. CustomPtr(T* p) : p_(p) {}
  49. operator T*() const { return p_; }
  50. bool operator!=(std::nullptr_t) const { return p_ != nullptr; }
  51. T* p_ = nullptr;
  52. };
  53. template <typename T, typename U>
  54. std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
  55. {
  56. // clang-format off
  57. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  58. // clang-format on
  59. return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
  60. : "false";
  61. }
  62. template <typename T, typename U>
  63. std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
  64. {
  65. // clang-format off
  66. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  67. // clang-format on
  68. return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
  69. : "false";
  70. }
  71. template <typename T, typename U>
  72. std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
  73. {
  74. // clang-format off
  75. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  76. // clang-format on
  77. return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
  78. : "false";
  79. }
  80. template <typename T, typename U>
  81. std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
  82. {
  83. // clang-format off
  84. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  85. // clang-format on
  86. return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
  87. : "false";
  88. }
  89. template <typename T, typename U>
  90. std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
  91. {
  92. // clang-format off
  93. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  94. // clang-format on
  95. return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
  96. : "false";
  97. }
  98. template <typename T, typename U>
  99. std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
  100. {
  101. // clang-format off
  102. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  103. // clang-format on
  104. return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
  105. : "false";
  106. }
  107. struct NonCopyableNonMovable
  108. {
  109. NonCopyableNonMovable() = default;
  110. NonCopyableNonMovable(const NonCopyableNonMovable&) = delete;
  111. NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete;
  112. NonCopyableNonMovable(NonCopyableNonMovable&&) = delete;
  113. NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete;
  114. };
  115. namespace
  116. {
  117. // clang-format off
  118. GSL_SUPPRESS(f .4) // NO-FORMAT: attribute
  119. // clang-format on
  120. bool helper(not_null<int*> p) { return *p == 12; }
  121. // clang-format off
  122. GSL_SUPPRESS(f .4) // NO-FORMAT: attribute
  123. // clang-format on
  124. bool helper_const(not_null<const int*> p) { return *p == 12; }
  125. int* return_pointer() { return nullptr; }
  126. } // namespace
  127. TEST(notnull_tests, TestNotNullConstructors)
  128. {
  129. {
  130. #ifdef CONFIRM_COMPILATION_ERRORS
  131. not_null<int*> p = nullptr; // yay...does not compile!
  132. not_null<std::vector<char>*> p1 = 0; // yay...does not compile!
  133. not_null<int*> p2; // yay...does not compile!
  134. std::unique_ptr<int> up = std::make_unique<int>(120);
  135. not_null<int*> p3 = up;
  136. // Forbid non-nullptr assignable types
  137. not_null<std::vector<int>> f(std::vector<int>{1});
  138. not_null<int> z(10);
  139. not_null<std::vector<int>> y({1, 2});
  140. #endif
  141. }
  142. const auto terminateHandler = std::set_terminate([] {
  143. std::cerr << "Expected Death. TestNotNullConstructors";
  144. std::abort();
  145. });
  146. const auto expected = GetExpectedDeathString(terminateHandler);
  147. {
  148. // from shared pointer
  149. int i = 12;
  150. auto rp = RefCounted<int>(&i);
  151. not_null<int*> p(rp);
  152. EXPECT_TRUE(p.get() == &i);
  153. not_null<std::shared_ptr<int>> x(
  154. std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
  155. int* pi = nullptr;
  156. EXPECT_DEATH((not_null<decltype(pi)>(pi)), expected);
  157. }
  158. {
  159. // from pointer to local
  160. int t = 42;
  161. not_null<int*> x = &t;
  162. helper(&t);
  163. helper_const(&t);
  164. EXPECT_TRUE(*x == 42);
  165. }
  166. {
  167. // from raw pointer
  168. // from not_null pointer
  169. int t = 42;
  170. int* p = &t;
  171. not_null<int*> x = p;
  172. helper(p);
  173. helper_const(p);
  174. helper(x);
  175. helper_const(x);
  176. EXPECT_TRUE(*x == 42);
  177. }
  178. {
  179. // from raw const pointer
  180. // from not_null const pointer
  181. int t = 42;
  182. const int* cp = &t;
  183. not_null<const int*> x = cp;
  184. helper_const(cp);
  185. helper_const(x);
  186. EXPECT_TRUE(*x == 42);
  187. }
  188. {
  189. // from not_null const pointer, using auto
  190. int t = 42;
  191. const int* cp = &t;
  192. auto x = not_null<const int*>{cp};
  193. EXPECT_TRUE(*x == 42);
  194. }
  195. {
  196. // from returned pointer
  197. EXPECT_DEATH(helper(return_pointer()), expected);
  198. EXPECT_DEATH(helper_const(return_pointer()), expected);
  199. }
  200. }
  201. template <typename T>
  202. void ostream_helper(T v)
  203. {
  204. not_null<T*> p(&v);
  205. {
  206. std::ostringstream os;
  207. std::ostringstream ref;
  208. os << static_cast<void*>(p);
  209. ref << static_cast<void*>(&v);
  210. EXPECT_TRUE(os.str() == ref.str());
  211. }
  212. {
  213. std::ostringstream os;
  214. std::ostringstream ref;
  215. os << *p;
  216. ref << v;
  217. EXPECT_TRUE(os.str() == ref.str());
  218. }
  219. }
  220. TEST(notnull_tests, TestNotNullostream)
  221. {
  222. ostream_helper<int>(17);
  223. ostream_helper<float>(21.5f);
  224. ostream_helper<double>(3.4566e-7);
  225. ostream_helper<char>('c');
  226. ostream_helper<uint16_t>(0x0123u);
  227. ostream_helper<const char*>("cstring");
  228. ostream_helper<std::string>("string");
  229. }
  230. TEST(notnull_tests, TestNotNullCasting)
  231. {
  232. MyBase base;
  233. MyDerived derived;
  234. Unrelated unrelated;
  235. not_null<Unrelated*> u{&unrelated};
  236. (void) u;
  237. not_null<MyDerived*> p{&derived};
  238. not_null<MyBase*> q(&base);
  239. q = p; // allowed with heterogeneous copy ctor
  240. EXPECT_TRUE(q == p);
  241. #ifdef CONFIRM_COMPILATION_ERRORS
  242. q = u; // no viable conversion possible between MyBase* and Unrelated*
  243. p = q; // not possible to implicitly convert MyBase* to MyDerived*
  244. not_null<Unrelated*> r = p;
  245. not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
  246. #endif
  247. not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
  248. EXPECT_TRUE(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
  249. }
  250. TEST(notnull_tests, TestNotNullAssignment)
  251. {
  252. const auto terminateHandler = std::set_terminate([] {
  253. std::cerr << "Expected Death. TestNotNullAssignmentd";
  254. std::abort();
  255. });
  256. const auto expected = GetExpectedDeathString(terminateHandler);
  257. int i = 12;
  258. not_null<int*> p(&i);
  259. EXPECT_TRUE(helper(p));
  260. int* q = nullptr;
  261. EXPECT_DEATH(p = not_null<int*>(q), expected);
  262. }
  263. TEST(notnull_tests, TestNotNullRawPointerComparison)
  264. {
  265. int ints[2] = {42, 43};
  266. int* p1 = &ints[0];
  267. const int* p2 = &ints[1];
  268. using NotNull1 = not_null<decltype(p1)>;
  269. using NotNull2 = not_null<decltype(p2)>;
  270. EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == true);
  271. EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == false);
  272. EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == false);
  273. EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == true);
  274. EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == false);
  275. EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
  276. EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
  277. EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == false);
  278. EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
  279. EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
  280. EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == true);
  281. EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
  282. EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
  283. }
  284. TEST(notnull_tests, TestNotNullDereferenceOperator)
  285. {
  286. {
  287. auto sp1 = std::make_shared<NonCopyableNonMovable>();
  288. using NotNullSp1 = not_null<decltype(sp1)>;
  289. EXPECT_TRUE(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
  290. EXPECT_TRUE(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
  291. }
  292. {
  293. int ints[1] = {42};
  294. CustomPtr<int> p1(&ints[0]);
  295. using NotNull1 = not_null<decltype(p1)>;
  296. EXPECT_TRUE(typeid(*NotNull1(p1)) == typeid(*p1));
  297. EXPECT_TRUE(*NotNull1(p1) == 42);
  298. *NotNull1(p1) = 43;
  299. EXPECT_TRUE(ints[0] == 43);
  300. }
  301. {
  302. int v = 42;
  303. gsl::not_null<int*> p(&v);
  304. EXPECT_TRUE(typeid(*p) == typeid(*(&v)));
  305. *p = 43;
  306. EXPECT_TRUE(v == 43);
  307. }
  308. }
  309. TEST(notnull_tests, TestNotNullSharedPtrComparison)
  310. {
  311. auto sp1 = std::make_shared<int>(42);
  312. auto sp2 = std::make_shared<const int>(43);
  313. using NotNullSp1 = not_null<decltype(sp1)>;
  314. using NotNullSp2 = not_null<decltype(sp2)>;
  315. EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
  316. EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
  317. EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
  318. EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
  319. EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
  320. EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
  321. EXPECT_TRUE((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
  322. EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
  323. EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
  324. EXPECT_TRUE((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
  325. EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
  326. EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
  327. EXPECT_TRUE((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
  328. EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
  329. EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
  330. EXPECT_TRUE((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
  331. }
  332. TEST(notnull_tests, TestNotNullCustomPtrComparison)
  333. {
  334. int ints[2] = {42, 43};
  335. CustomPtr<int> p1(&ints[0]);
  336. CustomPtr<const int> p2(&ints[1]);
  337. using NotNull1 = not_null<decltype(p1)>;
  338. using NotNull2 = not_null<decltype(p2)>;
  339. EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == "true");
  340. EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == "false");
  341. EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == "false");
  342. EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == "true");
  343. EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == "false");
  344. EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
  345. EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
  346. EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == "false");
  347. EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
  348. EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
  349. EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == "true");
  350. EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
  351. EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
  352. EXPECT_TRUE((NotNull1(p1) >= NotNull1(p1)) == "true");
  353. EXPECT_TRUE((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
  354. EXPECT_TRUE((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
  355. }
  356. #if defined(__cplusplus) && (__cplusplus >= 201703L)
  357. TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
  358. {
  359. {
  360. int i = 42;
  361. not_null x{&i};
  362. helper(not_null{&i});
  363. helper_const(not_null{&i});
  364. EXPECT_TRUE(*x == 42);
  365. }
  366. {
  367. const int i = 42;
  368. not_null x{&i};
  369. #ifdef CONFIRM_COMPILATION_ERRORS
  370. helper(not_null{&i});
  371. #endif
  372. helper_const(not_null{&i});
  373. EXPECT_TRUE(*x == 42);
  374. }
  375. {
  376. int i = 42;
  377. int* p = &i;
  378. not_null x{p};
  379. helper(not_null{p});
  380. helper_const(not_null{p});
  381. EXPECT_TRUE(*x == 42);
  382. }
  383. {
  384. const int i = 42;
  385. const int* p = &i;
  386. not_null x{p};
  387. #ifdef CONFIRM_COMPILATION_ERRORS
  388. helper(not_null{p});
  389. #endif
  390. helper_const(not_null{p});
  391. EXPECT_TRUE(*x == 42);
  392. }
  393. const auto terminateHandler = std::set_terminate([] {
  394. std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction";
  395. std::abort();
  396. });
  397. const auto expected = GetExpectedDeathString(terminateHandler);
  398. {
  399. auto workaround_macro = []() {
  400. int* p1 = nullptr;
  401. const not_null x{p1};
  402. };
  403. EXPECT_DEATH(workaround_macro(), expected);
  404. }
  405. {
  406. auto workaround_macro = []() {
  407. const int* p1 = nullptr;
  408. const not_null x{p1};
  409. };
  410. EXPECT_DEATH(workaround_macro(), expected);
  411. }
  412. {
  413. int* p = nullptr;
  414. EXPECT_DEATH(helper(not_null{p}), expected);
  415. EXPECT_DEATH(helper_const(not_null{p}), expected);
  416. }
  417. #ifdef CONFIRM_COMPILATION_ERRORS
  418. {
  419. not_null x{nullptr};
  420. helper(not_null{nullptr});
  421. helper_const(not_null{nullptr});
  422. }
  423. #endif
  424. }
  425. TEST(notnull_tests, TestVariantEmplace)
  426. {
  427. int i = 0;
  428. std::variant<std::monostate, not_null<int*>> v;
  429. v.emplace<not_null<int*>>(&i);
  430. EXPECT_FALSE(v.valueless_by_exception());
  431. EXPECT_TRUE(v.index() == 1);
  432. EXPECT_TRUE(std::get<not_null<int*>>(v) == &i);
  433. }
  434. #endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
  435. TEST(notnull_tests, TestMakeNotNull)
  436. {
  437. {
  438. int i = 42;
  439. const auto x = make_not_null(&i);
  440. helper(make_not_null(&i));
  441. helper_const(make_not_null(&i));
  442. EXPECT_TRUE(*x == 42);
  443. }
  444. {
  445. const int i = 42;
  446. const auto x = make_not_null(&i);
  447. #ifdef CONFIRM_COMPILATION_ERRORS
  448. helper(make_not_null(&i));
  449. #endif
  450. helper_const(make_not_null(&i));
  451. EXPECT_TRUE(*x == 42);
  452. }
  453. {
  454. int i = 42;
  455. int* p = &i;
  456. const auto x = make_not_null(p);
  457. helper(make_not_null(p));
  458. helper_const(make_not_null(p));
  459. EXPECT_TRUE(*x == 42);
  460. }
  461. {
  462. const int i = 42;
  463. const int* p = &i;
  464. const auto x = make_not_null(p);
  465. #ifdef CONFIRM_COMPILATION_ERRORS
  466. helper(make_not_null(p));
  467. #endif
  468. helper_const(make_not_null(p));
  469. EXPECT_TRUE(*x == 42);
  470. }
  471. const auto terminateHandler = std::set_terminate([] {
  472. std::cerr << "Expected Death. TestMakeNotNull";
  473. std::abort();
  474. });
  475. const auto expected = GetExpectedDeathString(terminateHandler);
  476. {
  477. const auto workaround_macro = []() {
  478. int* p1 = nullptr;
  479. const auto x = make_not_null(p1);
  480. EXPECT_TRUE(*x == 42);
  481. };
  482. EXPECT_DEATH(workaround_macro(), expected);
  483. }
  484. {
  485. const auto workaround_macro = []() {
  486. const int* p1 = nullptr;
  487. const auto x = make_not_null(p1);
  488. EXPECT_TRUE(*x == 42);
  489. };
  490. EXPECT_DEATH(workaround_macro(), expected);
  491. }
  492. {
  493. int* p = nullptr;
  494. EXPECT_DEATH(helper(make_not_null(p)), expected);
  495. EXPECT_DEATH(helper_const(make_not_null(p)), expected);
  496. }
  497. #ifdef CONFIRM_COMPILATION_ERRORS
  498. {
  499. EXPECT_DEATH(make_not_null(nullptr), expected);
  500. EXPECT_DEATH(helper(make_not_null(nullptr)), expected);
  501. EXPECT_DEATH(helper_const(make_not_null(nullptr)), expected);
  502. }
  503. #endif
  504. }
  505. TEST(notnull_tests, TestStdHash)
  506. {
  507. {
  508. int x = 42;
  509. int y = 99;
  510. not_null<int*> nn{&x};
  511. const not_null<int*> cnn{&x};
  512. std::hash<not_null<int*>> hash_nn;
  513. std::hash<int*> hash_intptr;
  514. EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x));
  515. EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y));
  516. EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
  517. }
  518. {
  519. const int x = 42;
  520. const int y = 99;
  521. not_null<const int*> nn{&x};
  522. const not_null<const int*> cnn{&x};
  523. std::hash<not_null<const int*>> hash_nn;
  524. std::hash<const int*> hash_intptr;
  525. EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x));
  526. EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y));
  527. EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
  528. }
  529. }