1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  explicit bounded_array_ref(T* first, T* last);
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 <darwintest.h>
9bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
10bb611c8fSApple OSS Distributions 
11bb611c8fSApple OSS Distributions struct T { int i; };
12bb611c8fSApple OSS Distributions inline bool
operator ==(T const & a,T const & b)13bb611c8fSApple OSS Distributions operator==(T const& a, T const& b)
14bb611c8fSApple OSS Distributions {
15bb611c8fSApple OSS Distributions 	return a.i == b.i;
16bb611c8fSApple OSS Distributions };
17bb611c8fSApple OSS Distributions 
18bb611c8fSApple OSS Distributions template <typename T>
19bb611c8fSApple OSS Distributions static void
tests()20bb611c8fSApple OSS Distributions tests()
21bb611c8fSApple OSS Distributions {
22bb611c8fSApple OSS Distributions 	T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}};
23bb611c8fSApple OSS Distributions 
24bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
25bb611c8fSApple OSS Distributions 	//   ^                                                ^
26bb611c8fSApple OSS Distributions 	//   |                                                |
27bb611c8fSApple OSS Distributions 	// first                                             last
28bb611c8fSApple OSS Distributions 	{
29bb611c8fSApple OSS Distributions 		T* first = &array[0];
30bb611c8fSApple OSS Distributions 		T* last = &array[5];
31bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
32bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[0]);
33bb611c8fSApple OSS Distributions 		CHECK(view.size() == 5);
34bb611c8fSApple OSS Distributions 		CHECK(view[0] == T{0});
35bb611c8fSApple OSS Distributions 		CHECK(view[1] == T{1});
36bb611c8fSApple OSS Distributions 		CHECK(view[2] == T{2});
37bb611c8fSApple OSS Distributions 		CHECK(view[3] == T{3});
38bb611c8fSApple OSS Distributions 		CHECK(view[4] == T{4});
39bb611c8fSApple OSS Distributions 	}
40bb611c8fSApple OSS Distributions 
41bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
42bb611c8fSApple OSS Distributions 	//   ^        ^
43bb611c8fSApple OSS Distributions 	//   |        |
44bb611c8fSApple OSS Distributions 	// first     last
45bb611c8fSApple OSS Distributions 	{
46bb611c8fSApple OSS Distributions 		T* first = &array[0];
47bb611c8fSApple OSS Distributions 		T* last = &array[1];
48bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
49bb611c8fSApple OSS Distributions 		CHECK(view.data() == &array[0]);
50bb611c8fSApple OSS Distributions 		CHECK(view.size() == 1);
51bb611c8fSApple OSS Distributions 		CHECK(view[0] == T{0});
52bb611c8fSApple OSS Distributions 	}
53bb611c8fSApple OSS Distributions 
54bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
55bb611c8fSApple OSS Distributions 	//   ^
56bb611c8fSApple OSS Distributions 	//   |
57bb611c8fSApple OSS Distributions 	// first,last
58bb611c8fSApple OSS Distributions 	{
59bb611c8fSApple OSS Distributions 		T* first = &array[0];
60bb611c8fSApple OSS Distributions 		T* last = &array[0];
61bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
62bb611c8fSApple OSS Distributions 		CHECK(view.size() == 0);
63bb611c8fSApple OSS Distributions 	}
64bb611c8fSApple OSS Distributions 
65bb611c8fSApple OSS Distributions 	// T{0}     T{1}     T{2}     T{3}     T{4}     <one-past-last>
66bb611c8fSApple OSS Distributions 	//                                                    ^
67bb611c8fSApple OSS Distributions 	//                                                    |
68bb611c8fSApple OSS Distributions 	//                                               first,last
69bb611c8fSApple OSS Distributions 	{
70bb611c8fSApple OSS Distributions 		T* first = &array[5];
71bb611c8fSApple OSS Distributions 		T* last = &array[5];
72bb611c8fSApple OSS Distributions 		test_bounded_array_ref<T> view(first, last);
73bb611c8fSApple OSS Distributions 		CHECK(view.size() == 0);
74bb611c8fSApple OSS Distributions 	}
75bb611c8fSApple OSS Distributions }
76bb611c8fSApple OSS Distributions 
77*8d741a5dSApple OSS Distributions T_DECL(ctor_begin_end, "bounded_array_ref.ctor.begin_end", T_META_TAG_VM_PREFERRED) {
78bb611c8fSApple OSS Distributions 	tests<T>();
79bb611c8fSApple OSS Distributions 	tests<T const>();
80bb611c8fSApple OSS Distributions }
81