1*27b03b36SApple OSS Distributions //
2*27b03b36SApple OSS Distributions // Tests for
3*27b03b36SApple OSS Distributions // explicit bounded_ptr(T* pointer, T const* begin, T const* end);
4*27b03b36SApple OSS Distributions //
5*27b03b36SApple OSS Distributions
6*27b03b36SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
7*27b03b36SApple OSS Distributions #include <array>
8*27b03b36SApple OSS Distributions #include <darwintest.h>
9*27b03b36SApple OSS Distributions #include <darwintest_utils.h>
10*27b03b36SApple OSS Distributions #include "test_utils.h"
11*27b03b36SApple OSS Distributions
12*27b03b36SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
13*27b03b36SApple OSS Distributions
14*27b03b36SApple OSS Distributions struct T {
15*27b03b36SApple OSS Distributions int i;
16*27b03b36SApple OSS Distributions friend constexpr bool
operator ==(T const volatile & a,T const & b)17*27b03b36SApple OSS Distributions operator==(T const volatile& a, T const& b)
18*27b03b36SApple OSS Distributions {
19*27b03b36SApple OSS Distributions return a.i == b.i;
20*27b03b36SApple OSS Distributions }
21*27b03b36SApple OSS Distributions };
22*27b03b36SApple OSS Distributions
23*27b03b36SApple OSS Distributions template <typename T, typename QualT>
24*27b03b36SApple OSS Distributions static void
tests()25*27b03b36SApple OSS Distributions tests()
26*27b03b36SApple OSS Distributions {
27*27b03b36SApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
28*27b03b36SApple OSS Distributions {
29*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.begin() + 0, array.begin(), array.end());
30*27b03b36SApple OSS Distributions _assert(*p == T{0});
31*27b03b36SApple OSS Distributions }
32*27b03b36SApple OSS Distributions {
33*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.begin() + 1, array.begin(), array.end());
34*27b03b36SApple OSS Distributions _assert(*p == T{1});
35*27b03b36SApple OSS Distributions }
36*27b03b36SApple OSS Distributions {
37*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.begin() + 2, array.begin(), array.end());
38*27b03b36SApple OSS Distributions _assert(*p == T{2});
39*27b03b36SApple OSS Distributions }
40*27b03b36SApple OSS Distributions {
41*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.begin() + 3, array.begin(), array.end());
42*27b03b36SApple OSS Distributions _assert(*p == T{3});
43*27b03b36SApple OSS Distributions }
44*27b03b36SApple OSS Distributions {
45*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.begin() + 4, array.begin(), array.end());
46*27b03b36SApple OSS Distributions _assert(*p == T{4});
47*27b03b36SApple OSS Distributions }
48*27b03b36SApple OSS Distributions
49*27b03b36SApple OSS Distributions // It must be valid to construct out-of-bounds pointers, but we obviously
50*27b03b36SApple OSS Distributions // can't dereference them.
51*27b03b36SApple OSS Distributions {
52*27b03b36SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
53*27b03b36SApple OSS Distributions // ^ ^ ^
54*27b03b36SApple OSS Distributions // | | |
55*27b03b36SApple OSS Distributions // ptr begin end
56*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.begin() + 1, array.begin() + 3, array.end());
57*27b03b36SApple OSS Distributions _assert(p.unsafe_discard_bounds() == array.begin() + 1);
58*27b03b36SApple OSS Distributions }
59*27b03b36SApple OSS Distributions {
60*27b03b36SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
61*27b03b36SApple OSS Distributions // ^ ^ ^
62*27b03b36SApple OSS Distributions // | | |
63*27b03b36SApple OSS Distributions // begin end ptr
64*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.begin() + 4, array.begin(), array.begin() + 3);
65*27b03b36SApple OSS Distributions _assert(p.unsafe_discard_bounds() == array.begin() + 4);
66*27b03b36SApple OSS Distributions }
67*27b03b36SApple OSS Distributions {
68*27b03b36SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
69*27b03b36SApple OSS Distributions // ^ ^
70*27b03b36SApple OSS Distributions // | |
71*27b03b36SApple OSS Distributions // begin end,ptr
72*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(array.end(), array.begin(), array.end());
73*27b03b36SApple OSS Distributions _assert(p.unsafe_discard_bounds() == array.end());
74*27b03b36SApple OSS Distributions }
75*27b03b36SApple OSS Distributions
76*27b03b36SApple OSS Distributions // Test creating a bounded_ptr from a null pointer.
77*27b03b36SApple OSS Distributions {
78*27b03b36SApple OSS Distributions test_bounded_ptr<QualT> p(nullptr, nullptr, nullptr);
79*27b03b36SApple OSS Distributions _assert(p.unsafe_discard_bounds() == nullptr);
80*27b03b36SApple OSS Distributions }
81*27b03b36SApple OSS Distributions }
82*27b03b36SApple OSS Distributions
83*27b03b36SApple OSS Distributions struct Base { };
84*27b03b36SApple OSS Distributions struct Derived : Base { };
85*27b03b36SApple OSS Distributions
86*27b03b36SApple OSS Distributions T_DECL(ctor_begin_end, "bounded_ptr.ctor.begin_end") {
87*27b03b36SApple OSS Distributions tests<T, T>();
88*27b03b36SApple OSS Distributions tests<T, T const>();
89*27b03b36SApple OSS Distributions tests<T, T volatile>();
90*27b03b36SApple OSS Distributions tests<T, T const volatile>();
91*27b03b36SApple OSS Distributions
92*27b03b36SApple OSS Distributions // Make sure we can construct a `bounded_ptr<Base>` from `Derived*` pointers
93*27b03b36SApple OSS Distributions {
94*27b03b36SApple OSS Distributions std::array<Derived, 5> array = {};
95*27b03b36SApple OSS Distributions test_bounded_ptr<Base> p(static_cast<Derived*>(array.begin()),
96*27b03b36SApple OSS Distributions static_cast<Derived*>(array.begin()),
97*27b03b36SApple OSS Distributions static_cast<Derived*>(array.end()));
98*27b03b36SApple OSS Distributions }
99*27b03b36SApple OSS Distributions }
100