xref: /xnu-12377.81.4/tests/safe_allocation_src/begin_end.cpp (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions //
2*043036a2SApple OSS Distributions // Tests for
3*043036a2SApple OSS Distributions //  iterator begin();
4*043036a2SApple OSS Distributions //  const_iterator begin() const;
5*043036a2SApple OSS Distributions //
6*043036a2SApple OSS Distributions //  iterator end();
7*043036a2SApple OSS Distributions //  const_iterator end() const;
8*043036a2SApple OSS Distributions //
9*043036a2SApple OSS Distributions 
10*043036a2SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
11*043036a2SApple OSS Distributions #include <darwintest.h>
12*043036a2SApple OSS Distributions #include "test_utils.h"
13*043036a2SApple OSS Distributions #include <type_traits>
14*043036a2SApple OSS Distributions 
15*043036a2SApple OSS Distributions struct T {
16*043036a2SApple OSS Distributions 	int i;
17*043036a2SApple OSS Distributions };
18*043036a2SApple OSS Distributions 
19*043036a2SApple OSS Distributions template <typename T>
20*043036a2SApple OSS Distributions static void
tests()21*043036a2SApple OSS Distributions tests()
22*043036a2SApple OSS Distributions {
23*043036a2SApple OSS Distributions 	using A = test_safe_allocation<T>;
24*043036a2SApple OSS Distributions 
25*043036a2SApple OSS Distributions 	// Check begin()/end() for a non-null allocation
26*043036a2SApple OSS Distributions 	{
27*043036a2SApple OSS Distributions 		A array(10, libkern::allocate_memory);
28*043036a2SApple OSS Distributions 		T* data = array.data();
29*043036a2SApple OSS Distributions 		test_bounded_ptr<T> begin = array.begin();
30*043036a2SApple OSS Distributions 		test_bounded_ptr<T> end = array.end();
31*043036a2SApple OSS Distributions 		CHECK(begin.discard_bounds() == data);
32*043036a2SApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == data + 10);
33*043036a2SApple OSS Distributions 	}
34*043036a2SApple OSS Distributions 	{
35*043036a2SApple OSS Distributions 		A const array(10, libkern::allocate_memory);
36*043036a2SApple OSS Distributions 		T const* data = array.data();
37*043036a2SApple OSS Distributions 		test_bounded_ptr<T const> begin = array.begin();
38*043036a2SApple OSS Distributions 		test_bounded_ptr<T const> end = array.end();
39*043036a2SApple OSS Distributions 		CHECK(begin.discard_bounds() == data);
40*043036a2SApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == data + 10);
41*043036a2SApple OSS Distributions 	}
42*043036a2SApple OSS Distributions 
43*043036a2SApple OSS Distributions 	// Check begin()/end() for a null allocation
44*043036a2SApple OSS Distributions 	{
45*043036a2SApple OSS Distributions 		A array = nullptr;
46*043036a2SApple OSS Distributions 		test_bounded_ptr<T> begin = array.begin();
47*043036a2SApple OSS Distributions 		test_bounded_ptr<T> end = array.end();
48*043036a2SApple OSS Distributions 		CHECK(begin.unsafe_discard_bounds() == nullptr);
49*043036a2SApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == nullptr);
50*043036a2SApple OSS Distributions 		CHECK(begin == end);
51*043036a2SApple OSS Distributions 	}
52*043036a2SApple OSS Distributions 	{
53*043036a2SApple OSS Distributions 		A const array = nullptr;
54*043036a2SApple OSS Distributions 		test_bounded_ptr<T const> begin = array.begin();
55*043036a2SApple OSS Distributions 		test_bounded_ptr<T const> end = array.end();
56*043036a2SApple OSS Distributions 		CHECK(begin.unsafe_discard_bounds() == nullptr);
57*043036a2SApple OSS Distributions 		CHECK(end.unsafe_discard_bounds() == nullptr);
58*043036a2SApple OSS Distributions 		CHECK(begin == end);
59*043036a2SApple OSS Distributions 	}
60*043036a2SApple OSS Distributions 
61*043036a2SApple OSS Distributions 	// Check associated types
62*043036a2SApple OSS Distributions 	{
63*043036a2SApple OSS Distributions 		static_assert(std::is_same_v<typename A::iterator, test_bounded_ptr<T> >);
64*043036a2SApple OSS Distributions 		static_assert(std::is_same_v<typename A::const_iterator, test_bounded_ptr<T const> >);
65*043036a2SApple OSS Distributions 	}
66*043036a2SApple OSS Distributions }
67*043036a2SApple OSS Distributions 
68*043036a2SApple OSS Distributions T_DECL(begin_end, "safe_allocation.begin_end", T_META_TAG_VM_PREFERRED) {
69*043036a2SApple OSS Distributions 	tests<T>();
70*043036a2SApple OSS Distributions 	tests<T const>();
71*043036a2SApple OSS Distributions }
72