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