1*f6217f89SApple OSS Distributions //
2*f6217f89SApple OSS Distributions // Tests for
3*f6217f89SApple OSS Distributions // explicit bounded_array_ref(bounded_ptr<T, TrappingPolicy> data, size_t n);
4*f6217f89SApple OSS Distributions //
5*f6217f89SApple OSS Distributions
6*f6217f89SApple OSS Distributions #include <libkern/c++/bounded_array_ref.h>
7*f6217f89SApple OSS Distributions #include "test_policy.h"
8*f6217f89SApple OSS Distributions #include <cstddef>
9*f6217f89SApple OSS Distributions #include <darwintest.h>
10*f6217f89SApple OSS Distributions #include <darwintest_utils.h>
11*f6217f89SApple OSS Distributions
12*f6217f89SApple OSS Distributions struct T { int i; };
13*f6217f89SApple OSS Distributions inline bool
operator ==(T const & a,T const & b)14*f6217f89SApple OSS Distributions operator==(T const& a, T const& b)
15*f6217f89SApple OSS Distributions {
16*f6217f89SApple OSS Distributions return a.i == b.i;
17*f6217f89SApple OSS Distributions };
18*f6217f89SApple OSS Distributions
19*f6217f89SApple OSS Distributions template <typename T>
20*f6217f89SApple OSS Distributions static void
tests()21*f6217f89SApple OSS Distributions tests()
22*f6217f89SApple OSS Distributions {
23*f6217f89SApple OSS Distributions T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}};
24*f6217f89SApple OSS Distributions T* const begin = &array[0];
25*f6217f89SApple OSS Distributions T* const end = &array[5];
26*f6217f89SApple OSS Distributions
27*f6217f89SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
28*f6217f89SApple OSS Distributions // ^ ^
29*f6217f89SApple OSS Distributions // | |
30*f6217f89SApple OSS Distributions // begin,ptr end
31*f6217f89SApple OSS Distributions //
32*f6217f89SApple OSS Distributions // ^------------------- view -----------------------^
33*f6217f89SApple OSS Distributions {
34*f6217f89SApple OSS Distributions test_bounded_ptr<T> ptr(&array[0], begin, end);
35*f6217f89SApple OSS Distributions test_bounded_array_ref<T> view(ptr, 5);
36*f6217f89SApple OSS Distributions CHECK(view.data() == &array[0]);
37*f6217f89SApple OSS Distributions CHECK(view.size() == 5);
38*f6217f89SApple OSS Distributions CHECK(view[0] == T{0});
39*f6217f89SApple OSS Distributions CHECK(view[1] == T{1});
40*f6217f89SApple OSS Distributions CHECK(view[2] == T{2});
41*f6217f89SApple OSS Distributions CHECK(view[3] == T{3});
42*f6217f89SApple OSS Distributions CHECK(view[4] == T{4});
43*f6217f89SApple OSS Distributions }
44*f6217f89SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
45*f6217f89SApple OSS Distributions // ^ ^
46*f6217f89SApple OSS Distributions // | |
47*f6217f89SApple OSS Distributions // begin,ptr end
48*f6217f89SApple OSS Distributions //
49*f6217f89SApple OSS Distributions // ^----- view -----^
50*f6217f89SApple OSS Distributions {
51*f6217f89SApple OSS Distributions test_bounded_ptr<T> ptr(&array[0], begin, end);
52*f6217f89SApple OSS Distributions test_bounded_array_ref<T> view(ptr, 3);
53*f6217f89SApple OSS Distributions CHECK(view.data() == &array[0]);
54*f6217f89SApple OSS Distributions CHECK(view.size() == 3);
55*f6217f89SApple OSS Distributions CHECK(view[0] == T{0});
56*f6217f89SApple OSS Distributions CHECK(view[1] == T{1});
57*f6217f89SApple OSS Distributions CHECK(view[2] == T{2});
58*f6217f89SApple OSS Distributions }
59*f6217f89SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
60*f6217f89SApple OSS Distributions // ^ ^ ^
61*f6217f89SApple OSS Distributions // | | |
62*f6217f89SApple OSS Distributions // begin ptr end
63*f6217f89SApple OSS Distributions //
64*f6217f89SApple OSS Distributions // ^------- view --------^
65*f6217f89SApple OSS Distributions {
66*f6217f89SApple OSS Distributions test_bounded_ptr<T> ptr(&array[3], begin, end);
67*f6217f89SApple OSS Distributions test_bounded_array_ref<T> view(ptr, 2);
68*f6217f89SApple OSS Distributions CHECK(view.data() == &array[3]);
69*f6217f89SApple OSS Distributions CHECK(view.size() == 2);
70*f6217f89SApple OSS Distributions CHECK(view[0] == T{3});
71*f6217f89SApple OSS Distributions CHECK(view[1] == T{4});
72*f6217f89SApple OSS Distributions }
73*f6217f89SApple OSS Distributions // Check with a valid `bounded_ptr` and a size of 0.
74*f6217f89SApple OSS Distributions {
75*f6217f89SApple OSS Distributions test_bounded_ptr<T> ptr(&array[0], begin, end);
76*f6217f89SApple OSS Distributions test_bounded_array_ref<T> view(ptr, 0);
77*f6217f89SApple OSS Distributions CHECK(view.size() == 0);
78*f6217f89SApple OSS Distributions }
79*f6217f89SApple OSS Distributions // Check with a null `bounded_ptr` and a size of 0.
80*f6217f89SApple OSS Distributions {
81*f6217f89SApple OSS Distributions test_bounded_ptr<T> ptr = nullptr;
82*f6217f89SApple OSS Distributions test_bounded_array_ref<T> view(ptr, 0);
83*f6217f89SApple OSS Distributions CHECK(view.size() == 0);
84*f6217f89SApple OSS Distributions }
85*f6217f89SApple OSS Distributions // Check with a non-null but invalid `bounded_ptr` and a size of 0.
86*f6217f89SApple OSS Distributions {
87*f6217f89SApple OSS Distributions test_bounded_ptr<T> ptr(end, begin, end);
88*f6217f89SApple OSS Distributions test_bounded_array_ref<T> view(ptr, 0);
89*f6217f89SApple OSS Distributions CHECK(view.size() == 0);
90*f6217f89SApple OSS Distributions }
91*f6217f89SApple OSS Distributions // Make sure there's no ambiguity between constructors.
92*f6217f89SApple OSS Distributions {
93*f6217f89SApple OSS Distributions test_bounded_ptr<T> ptr(begin, begin, end);
94*f6217f89SApple OSS Distributions std::ptrdiff_t size = sizeof(array) / sizeof(*array);
95*f6217f89SApple OSS Distributions test_bounded_array_ref<T> view(ptr, size);
96*f6217f89SApple OSS Distributions CHECK(view.data() == &array[0]);
97*f6217f89SApple OSS Distributions CHECK(view.size() == 5);
98*f6217f89SApple OSS Distributions }
99*f6217f89SApple OSS Distributions }
100*f6217f89SApple OSS Distributions
101*f6217f89SApple OSS Distributions T_DECL(ctor_bounded_ptr, "bounded_array_ref.ctor.bounded_ptr", T_META_TAG_VM_PREFERRED) {
102*f6217f89SApple OSS Distributions tests<T>();
103*f6217f89SApple OSS Distributions tests<T const>();
104*f6217f89SApple OSS Distributions }
105