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