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