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