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