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