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