1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  explicit bounded_array_ref(bounded_ptr<T, TrappingPolicy> data, size_t n);
4bb611c8fSApple OSS Distributions //
5bb611c8fSApple OSS Distributions 
6bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_array_ref.h>
7bb611c8fSApple OSS Distributions #include "test_policy.h"
8bb611c8fSApple OSS Distributions #include <cstddef>
9bb611c8fSApple OSS Distributions #include <darwintest.h>
10bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
11bb611c8fSApple OSS Distributions 
12bb611c8fSApple OSS Distributions struct T { int i; };
13bb611c8fSApple OSS Distributions inline bool
operator ==(T const & a,T const & b)14bb611c8fSApple OSS Distributions operator==(T const& a, T const& b)
15bb611c8fSApple OSS Distributions {
16bb611c8fSApple OSS Distributions 	return a.i == b.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 	T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}};
24bb611c8fSApple OSS Distributions 	T* const begin = &array[0];
25bb611c8fSApple OSS Distributions 	T* const end = &array[5];
26bb611c8fSApple OSS Distributions 
27bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
28bb611c8fSApple OSS Distributions 	//   ^                                                ^
29bb611c8fSApple OSS Distributions 	//   |                                                |
30bb611c8fSApple OSS Distributions 	// begin,ptr                                         end
31bb611c8fSApple OSS Distributions 	//
32bb611c8fSApple OSS Distributions 	//   ^------------------- view -----------------------^
33bb611c8fSApple OSS Distributions 	{
34bb611c8fSApple OSS Distributions 		test_bounded_ptr<T> ptr(&array[0], begin, end);
35bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 5);
36bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[0]);
37bb611c8fSApple OSS Distributions 		CHECK(view.size() == 5);
38bb611c8fSApple OSS Distributions 		CHECK(view[0] == T{0});
39bb611c8fSApple OSS Distributions 		CHECK(view[1] == T{1});
40bb611c8fSApple OSS Distributions 		CHECK(view[2] == T{2});
41bb611c8fSApple OSS Distributions 		CHECK(view[3] == T{3});
42bb611c8fSApple OSS Distributions 		CHECK(view[4] == T{4});
43bb611c8fSApple OSS Distributions 	}
44bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
45bb611c8fSApple OSS Distributions 	//   ^                                                ^
46bb611c8fSApple OSS Distributions 	//   |                                                |
47bb611c8fSApple OSS Distributions 	// begin,ptr                                         end
48bb611c8fSApple OSS Distributions 	//
49bb611c8fSApple OSS Distributions 	//   ^----- view -----^
50bb611c8fSApple OSS Distributions 	{
51bb611c8fSApple OSS Distributions 		test_bounded_ptr<T> ptr(&array[0], begin, end);
52bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 3);
53bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[0]);
54bb611c8fSApple OSS Distributions 		CHECK(view.size() == 3);
55bb611c8fSApple OSS Distributions 		CHECK(view[0] == T{0});
56bb611c8fSApple OSS Distributions 		CHECK(view[1] == T{1});
57bb611c8fSApple OSS Distributions 		CHECK(view[2] == T{2});
58bb611c8fSApple OSS Distributions 	}
59bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
60bb611c8fSApple OSS Distributions 	//   ^                          ^                     ^
61bb611c8fSApple OSS Distributions 	//   |                          |                     |
62bb611c8fSApple OSS Distributions 	// begin                       ptr                   end
63bb611c8fSApple OSS Distributions 	//
64bb611c8fSApple OSS Distributions 	//                              ^------- view --------^
65bb611c8fSApple OSS Distributions 	{
66bb611c8fSApple OSS Distributions 		test_bounded_ptr<T> ptr(&array[3], begin, end);
67bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 2);
68bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[3]);
69bb611c8fSApple OSS Distributions 		CHECK(view.size() == 2);
70bb611c8fSApple OSS Distributions 		CHECK(view[0] == T{3});
71bb611c8fSApple OSS Distributions 		CHECK(view[1] == T{4});
72bb611c8fSApple OSS Distributions 	}
73bb611c8fSApple OSS Distributions 	// Check with a valid `bounded_ptr` and a size of 0.
74bb611c8fSApple OSS Distributions 	{
75bb611c8fSApple OSS Distributions 		test_bounded_ptr<T> ptr(&array[0], begin, end);
76bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 0);
77bb611c8fSApple OSS Distributions 		CHECK(view.size() == 0);
78bb611c8fSApple OSS Distributions 	}
79bb611c8fSApple OSS Distributions 	// Check with a null `bounded_ptr` and a size of 0.
80bb611c8fSApple OSS Distributions 	{
81bb611c8fSApple OSS Distributions 		test_bounded_ptr<T> ptr = nullptr;
82bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 0);
83bb611c8fSApple OSS Distributions 		CHECK(view.size() == 0);
84bb611c8fSApple OSS Distributions 	}
85bb611c8fSApple OSS Distributions 	// Check with a non-null but invalid `bounded_ptr` and a size of 0.
86bb611c8fSApple OSS Distributions 	{
87bb611c8fSApple OSS Distributions 		test_bounded_ptr<T> ptr(end, begin, end);
88bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, 0);
89bb611c8fSApple OSS Distributions 		CHECK(view.size() == 0);
90bb611c8fSApple OSS Distributions 	}
91bb611c8fSApple OSS Distributions 	// Make sure there's no ambiguity between constructors.
92bb611c8fSApple OSS Distributions 	{
93bb611c8fSApple OSS Distributions 		test_bounded_ptr<T> ptr(begin, begin, end);
94bb611c8fSApple OSS Distributions 		std::ptrdiff_t size = sizeof(array) / sizeof(*array);
95bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(ptr, size);
96bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[0]);
97bb611c8fSApple OSS Distributions 		CHECK(view.size() == 5);
98bb611c8fSApple OSS Distributions 	}
99bb611c8fSApple OSS Distributions }
100bb611c8fSApple OSS Distributions 
101*8d741a5dSApple OSS Distributions T_DECL(ctor_bounded_ptr, "bounded_array_ref.ctor.bounded_ptr", T_META_TAG_VM_PREFERRED) {
102bb611c8fSApple OSS Distributions 	tests<T>();
103bb611c8fSApple OSS Distributions 	tests<T const>();
104bb611c8fSApple OSS Distributions }
105