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