xref: /xnu-10002.81.5/tests/bounded_ptr_src/assign.nullptr.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions //
2*5e3eaea3SApple OSS Distributions // Tests for
3*5e3eaea3SApple OSS Distributions //  bounded_ptr& operator=(std::nullptr_t);
4*5e3eaea3SApple OSS Distributions //
5*5e3eaea3SApple OSS Distributions 
6*5e3eaea3SApple OSS Distributions #include <cstddef>
7*5e3eaea3SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
8*5e3eaea3SApple OSS Distributions #include <darwintest.h>
9*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
10*5e3eaea3SApple OSS Distributions #include "test_utils.h"
11*5e3eaea3SApple OSS Distributions 
12*5e3eaea3SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
13*5e3eaea3SApple OSS Distributions 
14*5e3eaea3SApple OSS Distributions struct T { };
15*5e3eaea3SApple OSS Distributions 
16*5e3eaea3SApple OSS Distributions template <typename T, typename TQual>
17*5e3eaea3SApple OSS Distributions static void
tests()18*5e3eaea3SApple OSS Distributions tests()
19*5e3eaea3SApple OSS Distributions {
20*5e3eaea3SApple OSS Distributions 	T obj{};
21*5e3eaea3SApple OSS Distributions 
22*5e3eaea3SApple OSS Distributions 	// Assign from nullptr
23*5e3eaea3SApple OSS Distributions 	{
24*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<TQual> p(&obj, &obj, &obj + 1);
25*5e3eaea3SApple OSS Distributions 		_assert(p != nullptr);
26*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<TQual>& ref = (p = nullptr);
27*5e3eaea3SApple OSS Distributions 		_assert(&ref == &p);
28*5e3eaea3SApple OSS Distributions 		_assert(p == nullptr);
29*5e3eaea3SApple OSS Distributions 	}
30*5e3eaea3SApple OSS Distributions 
31*5e3eaea3SApple OSS Distributions 	// Assign from NULL
32*5e3eaea3SApple OSS Distributions 	{
33*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<TQual> p(&obj, &obj, &obj + 1);
34*5e3eaea3SApple OSS Distributions 		_assert(p != nullptr);
35*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<TQual>& ref = (p = NULL);
36*5e3eaea3SApple OSS Distributions 		_assert(&ref == &p);
37*5e3eaea3SApple OSS Distributions 		_assert(p == nullptr);
38*5e3eaea3SApple OSS Distributions 	}
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions 	// Assign from 0
41*5e3eaea3SApple OSS Distributions 	{
42*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<TQual> p(&obj, &obj, &obj + 1);
43*5e3eaea3SApple OSS Distributions 		_assert(p != nullptr);
44*5e3eaea3SApple OSS Distributions 		test_bounded_ptr<TQual>& ref = (p = 0);
45*5e3eaea3SApple OSS Distributions 		_assert(&ref == &p);
46*5e3eaea3SApple OSS Distributions 		_assert(p == nullptr);
47*5e3eaea3SApple OSS Distributions 	}
48*5e3eaea3SApple OSS Distributions }
49*5e3eaea3SApple OSS Distributions 
50*5e3eaea3SApple OSS Distributions T_DECL(assign_nullptr, "bounded_ptr.assign.nullptr") {
51*5e3eaea3SApple OSS Distributions 	tests<T, T>();
52*5e3eaea3SApple OSS Distributions 	tests<T, T const>();
53*5e3eaea3SApple OSS Distributions 	tests<T, T volatile>();
54*5e3eaea3SApple OSS Distributions 	tests<T, T const volatile>();
55*5e3eaea3SApple OSS Distributions }
56