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