1*bb611c8fSApple OSS Distributions // 2*bb611c8fSApple OSS Distributions // Tests for 3*bb611c8fSApple OSS Distributions // template <typename T, typename P1, typename U, typename P2> 4*bb611c8fSApple OSS Distributions // bool operator==(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b); 5*bb611c8fSApple OSS Distributions // 6*bb611c8fSApple OSS Distributions // template <typename T, typename P1, typename U, typename P2> 7*bb611c8fSApple OSS Distributions // bool operator!=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b); 8*bb611c8fSApple OSS Distributions // 9*bb611c8fSApple OSS Distributions 10*bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h> 11*bb611c8fSApple OSS Distributions #include <array> 12*bb611c8fSApple OSS Distributions #include <darwintest.h> 13*bb611c8fSApple OSS Distributions #include <darwintest_utils.h> 14*bb611c8fSApple OSS Distributions #include "test_utils.h" 15*bb611c8fSApple OSS Distributions 16*bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__) 17*bb611c8fSApple OSS Distributions 18*bb611c8fSApple OSS Distributions struct dummy_policy1 { 19*bb611c8fSApple OSS Distributions static constexpr void 20*bb611c8fSApple OSS Distributions trap(char const*) 21*bb611c8fSApple OSS Distributions { 22*bb611c8fSApple OSS Distributions } 23*bb611c8fSApple OSS Distributions }; 24*bb611c8fSApple OSS Distributions struct dummy_policy2 { 25*bb611c8fSApple OSS Distributions static constexpr void 26*bb611c8fSApple OSS Distributions trap(char const*) 27*bb611c8fSApple OSS Distributions { 28*bb611c8fSApple OSS Distributions } 29*bb611c8fSApple OSS Distributions }; 30*bb611c8fSApple OSS Distributions 31*bb611c8fSApple OSS Distributions template <typename T, typename U> 32*bb611c8fSApple OSS Distributions static void 33*bb611c8fSApple OSS Distributions check_eq(T t, U u) 34*bb611c8fSApple OSS Distributions { 35*bb611c8fSApple OSS Distributions _assert(t == u); 36*bb611c8fSApple OSS Distributions _assert(u == t); 37*bb611c8fSApple OSS Distributions _assert(!(t != u)); 38*bb611c8fSApple OSS Distributions _assert(!(u != t)); 39*bb611c8fSApple OSS Distributions } 40*bb611c8fSApple OSS Distributions 41*bb611c8fSApple OSS Distributions template <typename T, typename U> 42*bb611c8fSApple OSS Distributions static void 43*bb611c8fSApple OSS Distributions check_ne(T t, U u) 44*bb611c8fSApple OSS Distributions { 45*bb611c8fSApple OSS Distributions _assert(!(t == u)); 46*bb611c8fSApple OSS Distributions _assert(!(u == t)); 47*bb611c8fSApple OSS Distributions _assert(t != u); 48*bb611c8fSApple OSS Distributions _assert(u != t); 49*bb611c8fSApple OSS Distributions } 50*bb611c8fSApple OSS Distributions 51*bb611c8fSApple OSS Distributions template <typename T, typename TQual> 52*bb611c8fSApple OSS Distributions static void 53*bb611c8fSApple OSS Distributions tests() 54*bb611c8fSApple OSS Distributions { 55*bb611c8fSApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}}; 56*bb611c8fSApple OSS Distributions 57*bb611c8fSApple OSS Distributions // Pointers with the same bounds 58*bb611c8fSApple OSS Distributions { 59*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end()); 60*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin(), array.begin(), array.end()); 61*bb611c8fSApple OSS Distributions check_eq(a, b); 62*bb611c8fSApple OSS Distributions } 63*bb611c8fSApple OSS Distributions { 64*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end()); 65*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin() + 1, array.begin(), array.end()); 66*bb611c8fSApple OSS Distributions check_ne(a, b); 67*bb611c8fSApple OSS Distributions } 68*bb611c8fSApple OSS Distributions { 69*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end()); 70*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin() + 2, array.begin(), array.end()); 71*bb611c8fSApple OSS Distributions check_ne(a, b); 72*bb611c8fSApple OSS Distributions } 73*bb611c8fSApple OSS Distributions { 74*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end()); 75*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.end(), array.begin(), array.end()); 76*bb611c8fSApple OSS Distributions check_ne(a, b); 77*bb611c8fSApple OSS Distributions } 78*bb611c8fSApple OSS Distributions { 79*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.end(), array.begin(), array.end()); 80*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.end(), array.begin(), array.end()); 81*bb611c8fSApple OSS Distributions check_eq(a, b); 82*bb611c8fSApple OSS Distributions } 83*bb611c8fSApple OSS Distributions 84*bb611c8fSApple OSS Distributions // Compare null pointers 85*bb611c8fSApple OSS Distributions { 86*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a; 87*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin(), array.begin(), array.end()); 88*bb611c8fSApple OSS Distributions check_ne(a, b); 89*bb611c8fSApple OSS Distributions } 90*bb611c8fSApple OSS Distributions { 91*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a; 92*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b; 93*bb611c8fSApple OSS Distributions check_eq(a, b); 94*bb611c8fSApple OSS Distributions } 95*bb611c8fSApple OSS Distributions 96*bb611c8fSApple OSS Distributions // Pointers with different bounds 97*bb611c8fSApple OSS Distributions { 98*bb611c8fSApple OSS Distributions // Overlapping bounds, equal 99*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin() + 2, array.end()); 100*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin(), array.begin(), array.end()); 101*bb611c8fSApple OSS Distributions check_eq(a, b); 102*bb611c8fSApple OSS Distributions } 103*bb611c8fSApple OSS Distributions { 104*bb611c8fSApple OSS Distributions // Overlapping bounds, not equal 105*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin() + 2, array.end()); 106*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin() + 2, array.begin(), array.end()); 107*bb611c8fSApple OSS Distributions check_ne(a, b); 108*bb611c8fSApple OSS Distributions } 109*bb611c8fSApple OSS Distributions { 110*bb611c8fSApple OSS Distributions // Non-overlapping bounds, equal 111*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.begin() + 1); 112*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin(), array.begin() + 2, array.end()); 113*bb611c8fSApple OSS Distributions check_eq(a, b); 114*bb611c8fSApple OSS Distributions } 115*bb611c8fSApple OSS Distributions { 116*bb611c8fSApple OSS Distributions // Non-overlapping bounds, not equal 117*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.begin() + 1); 118*bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const b(array.begin() + 3, array.begin() + 2, array.end()); 119*bb611c8fSApple OSS Distributions check_ne(a, b); 120*bb611c8fSApple OSS Distributions } 121*bb611c8fSApple OSS Distributions 122*bb611c8fSApple OSS Distributions // Test with different policies 123*bb611c8fSApple OSS Distributions { 124*bb611c8fSApple OSS Distributions libkern::bounded_ptr<TQual, dummy_policy1> const a(array.begin(), array.begin(), array.end()); 125*bb611c8fSApple OSS Distributions libkern::bounded_ptr<TQual, dummy_policy2> const b(array.begin(), array.begin(), array.end()); 126*bb611c8fSApple OSS Distributions check_eq(a, b); 127*bb611c8fSApple OSS Distributions } 128*bb611c8fSApple OSS Distributions } 129*bb611c8fSApple OSS Distributions 130*bb611c8fSApple OSS Distributions struct Base { int i; }; 131*bb611c8fSApple OSS Distributions struct Derived : Base { }; 132*bb611c8fSApple OSS Distributions 133*bb611c8fSApple OSS Distributions template <typename Related> 134*bb611c8fSApple OSS Distributions static void 135*bb611c8fSApple OSS Distributions tests_convert() 136*bb611c8fSApple OSS Distributions { 137*bb611c8fSApple OSS Distributions std::array<Derived, 5> array = {Derived{0}, Derived{1}, Derived{2}, Derived{3}, Derived{4}}; 138*bb611c8fSApple OSS Distributions test_bounded_ptr<Derived> const a(array.begin(), array.begin(), array.end() - 1); 139*bb611c8fSApple OSS Distributions test_bounded_ptr<Related> const b(array.begin(), array.begin(), array.end() - 1); 140*bb611c8fSApple OSS Distributions check_eq(a, b); 141*bb611c8fSApple OSS Distributions } 142*bb611c8fSApple OSS Distributions 143*bb611c8fSApple OSS Distributions T_DECL(compare_equal, "bounded_ptr.compare.equal") { 144*bb611c8fSApple OSS Distributions tests<Derived, Derived>(); 145*bb611c8fSApple OSS Distributions tests<Derived, Derived const>(); 146*bb611c8fSApple OSS Distributions tests<Derived, Derived volatile>(); 147*bb611c8fSApple OSS Distributions tests<Derived, Derived const volatile>(); 148*bb611c8fSApple OSS Distributions tests_convert<Base>(); 149*bb611c8fSApple OSS Distributions tests_convert<Base const>(); 150*bb611c8fSApple OSS Distributions tests_convert<Base volatile>(); 151*bb611c8fSApple OSS Distributions tests_convert<Base const volatile>(); 152*bb611c8fSApple OSS Distributions } 153