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