xref: /xnu-12377.81.4/tests/bounded_array_src/operator.subscript.cpp (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions //
2*043036a2SApple OSS Distributions // Tests for
3*043036a2SApple OSS Distributions //  T& operator[](ptrdiff_t n);
4*043036a2SApple OSS Distributions //  T const& operator[](ptrdiff_t n) const;
5*043036a2SApple OSS Distributions //
6*043036a2SApple OSS Distributions 
7*043036a2SApple OSS Distributions #include <libkern/c++/bounded_array.h>
8*043036a2SApple OSS Distributions #include "test_policy.h"
9*043036a2SApple OSS Distributions #include <darwintest.h>
10*043036a2SApple OSS Distributions #include <type_traits>
11*043036a2SApple OSS Distributions 
12*043036a2SApple OSS Distributions struct T { int i; };
13*043036a2SApple OSS Distributions inline bool
operator ==(T const & a,T const & b)14*043036a2SApple OSS Distributions operator==(T const& a, T const& b)
15*043036a2SApple OSS Distributions {
16*043036a2SApple OSS Distributions 	return a.i == b.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 	{
24*043036a2SApple OSS Distributions 		test_bounded_array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
25*043036a2SApple OSS Distributions 		T& a0 = array[0];
26*043036a2SApple OSS Distributions 		CHECK(&a0 == array.data());
27*043036a2SApple OSS Distributions 		CHECK(a0 == T{0});
28*043036a2SApple OSS Distributions 		T& a1 = array[1];
29*043036a2SApple OSS Distributions 		CHECK(a1 == T{1});
30*043036a2SApple OSS Distributions 		T& a2 = array[2];
31*043036a2SApple OSS Distributions 		CHECK(a2 == T{2});
32*043036a2SApple OSS Distributions 		T& a3 = array[3];
33*043036a2SApple OSS Distributions 		CHECK(a3 == T{3});
34*043036a2SApple OSS Distributions 		T& a4 = array[4];
35*043036a2SApple OSS Distributions 		CHECK(a4 == T{4});
36*043036a2SApple OSS Distributions 	}
37*043036a2SApple OSS Distributions 
38*043036a2SApple OSS Distributions 	{
39*043036a2SApple OSS Distributions 		test_bounded_array<T, 5> const array = {T{0}, T{1}, T{2}, T{3}, T{4}};
40*043036a2SApple OSS Distributions 		T const& a0 = array[0];
41*043036a2SApple OSS Distributions 		CHECK(&a0 == array.data());
42*043036a2SApple OSS Distributions 		CHECK(a0 == T{0});
43*043036a2SApple OSS Distributions 		T const& a1 = array[1];
44*043036a2SApple OSS Distributions 		CHECK(a1 == T{1});
45*043036a2SApple OSS Distributions 		T const& a2 = array[2];
46*043036a2SApple OSS Distributions 		CHECK(a2 == T{2});
47*043036a2SApple OSS Distributions 		T const& a3 = array[3];
48*043036a2SApple OSS Distributions 		CHECK(a3 == T{3});
49*043036a2SApple OSS Distributions 		T const& a4 = array[4];
50*043036a2SApple OSS Distributions 		CHECK(a4 == T{4});
51*043036a2SApple OSS Distributions 	}
52*043036a2SApple OSS Distributions }
53*043036a2SApple OSS Distributions 
54*043036a2SApple OSS Distributions T_DECL(operator_subscript, "bounded_array.operator.subscript", T_META_TAG_VM_PREFERRED) {
55*043036a2SApple OSS Distributions 	tests<T>();
56*043036a2SApple OSS Distributions }
57