xref: /xnu-10002.1.13/tests/bounded_array_ref_src/ctor.bounded_array.cpp (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions //
2*1031c584SApple OSS Distributions // Tests for
3*1031c584SApple OSS Distributions //  template <size_t N>
4*1031c584SApple OSS Distributions //  bounded_array_ref(bounded_array<T, N, TrappingPolicy>& data);
5*1031c584SApple OSS Distributions //
6*1031c584SApple OSS Distributions 
7*1031c584SApple OSS Distributions #include <libkern/c++/bounded_array_ref.h>
8*1031c584SApple OSS Distributions #include "test_policy.h"
9*1031c584SApple OSS Distributions #include <darwintest.h>
10*1031c584SApple OSS Distributions #include <darwintest_utils.h>
11*1031c584SApple OSS Distributions 
12*1031c584SApple OSS Distributions struct T { int i; };
13*1031c584SApple OSS Distributions inline bool
operator ==(T const & a,T const & b)14*1031c584SApple OSS Distributions operator==(T const& a, T const& b)
15*1031c584SApple OSS Distributions {
16*1031c584SApple OSS Distributions 	return a.i == b.i;
17*1031c584SApple OSS Distributions };
18*1031c584SApple OSS Distributions 
19*1031c584SApple OSS Distributions template <typename T>
20*1031c584SApple OSS Distributions static void
tests()21*1031c584SApple OSS Distributions tests()
22*1031c584SApple OSS Distributions {
23*1031c584SApple OSS Distributions 	{
24*1031c584SApple OSS Distributions 		test_bounded_array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
25*1031c584SApple OSS Distributions 		test_bounded_array_ref<T> view(array);
26*1031c584SApple OSS Distributions 		CHECK(view.data() == array.data());
27*1031c584SApple OSS Distributions 		CHECK(view.size() == 5);
28*1031c584SApple OSS Distributions 		CHECK(view[0] == T{0});
29*1031c584SApple OSS Distributions 		CHECK(view[1] == T{1});
30*1031c584SApple OSS Distributions 		CHECK(view[2] == T{2});
31*1031c584SApple OSS Distributions 		CHECK(view[3] == T{3});
32*1031c584SApple OSS Distributions 		CHECK(view[4] == T{4});
33*1031c584SApple OSS Distributions 	}
34*1031c584SApple OSS Distributions 
35*1031c584SApple OSS Distributions 	{
36*1031c584SApple OSS Distributions 		test_bounded_array<T, 1> array = {T{11}};
37*1031c584SApple OSS Distributions 		test_bounded_array_ref<T> view(array);
38*1031c584SApple OSS Distributions 		CHECK(view.data() == array.data());
39*1031c584SApple OSS Distributions 		CHECK(view.size() == 1);
40*1031c584SApple OSS Distributions 		CHECK(view[0] == T{11});
41*1031c584SApple OSS Distributions 	}
42*1031c584SApple OSS Distributions 
43*1031c584SApple OSS Distributions 	{
44*1031c584SApple OSS Distributions 		test_bounded_array<T, 0> array = {};
45*1031c584SApple OSS Distributions 		test_bounded_array_ref<T> view(array);
46*1031c584SApple OSS Distributions 		CHECK(view.data() == array.data());
47*1031c584SApple OSS Distributions 		CHECK(view.size() == 0);
48*1031c584SApple OSS Distributions 	}
49*1031c584SApple OSS Distributions 
50*1031c584SApple OSS Distributions 	// Also test implicit construction
51*1031c584SApple OSS Distributions 	{
52*1031c584SApple OSS Distributions 		test_bounded_array<T, 1> array = {T{11}};
53*1031c584SApple OSS Distributions 		test_bounded_array_ref<T> view = array;
54*1031c584SApple OSS Distributions 		CHECK(view.data() == array.data());
55*1031c584SApple OSS Distributions 		CHECK(view.size() == 1);
56*1031c584SApple OSS Distributions 	}
57*1031c584SApple OSS Distributions 	{
58*1031c584SApple OSS Distributions 		test_bounded_array<T, 1> array = {T{11}};
59*1031c584SApple OSS Distributions 		auto check = [&array](test_bounded_array_ref<T> view) {
60*1031c584SApple OSS Distributions 			    CHECK(view.data() == array.data());
61*1031c584SApple OSS Distributions 			    CHECK(view.size() == 1);
62*1031c584SApple OSS Distributions 		    };
63*1031c584SApple OSS Distributions 		check(array);
64*1031c584SApple OSS Distributions 	}
65*1031c584SApple OSS Distributions }
66*1031c584SApple OSS Distributions 
67*1031c584SApple OSS Distributions T_DECL(ctor_bounded_array, "bounded_array_ref.ctor.bounded_array") {
68*1031c584SApple OSS Distributions 	tests<T>();
69*1031c584SApple OSS Distributions }
70