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