Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | // // Tests for // friend bounded_ptr operator-(bounded_ptr p, std::ptrdiff_t n); // #include <libkern/c++/bounded_ptr.h> #include "test_utils.h" #include <array> #include <cstddef> #include <darwintest.h> #include <darwintest_utils.h> #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__) struct T { int i; }; template <typename T, typename QualT> static void tests() { std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}}; // Subtract positive offsets // T{0} T{1} T{2} T{3} T{4} <one-past-last> // ^ ^ // | | // begin end,ptr { test_bounded_ptr<QualT> const ptr(array.end(), array.begin(), array.end()); { test_bounded_ptr<QualT> res = ptr - static_cast<std::ptrdiff_t>(0); _assert(ptr == array.end()); } { test_bounded_ptr<QualT> res = ptr - 1; _assert(&*res == &array[4]); } { test_bounded_ptr<QualT> res = ptr - 2; _assert(&*res == &array[3]); } { test_bounded_ptr<QualT> res = ptr - 3; _assert(&*res == &array[2]); } { test_bounded_ptr<QualT> res = ptr - 4; _assert(&*res == &array[1]); } { test_bounded_ptr<QualT> res = ptr - 5; _assert(&*res == &array[0]); } } // Subtract negative offsets // T{0} T{1} T{2} T{3} T{4} <one-past-last> // ^ ^ // | | // begin,ptr end { test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end()); { test_bounded_ptr<QualT> res = ptr - static_cast<std::ptrdiff_t>(0); _assert(&*res == &array[0]); } { test_bounded_ptr<QualT> res = ptr - -1; _assert(&*res == &array[1]); } { test_bounded_ptr<QualT> res = ptr - -2; _assert(&*res == &array[2]); } { test_bounded_ptr<QualT> res = ptr - -3; _assert(&*res == &array[3]); } { test_bounded_ptr<QualT> res = ptr - -4; _assert(&*res == &array[4]); } { test_bounded_ptr<QualT> res = ptr - -5; _assert(res == array.end()); } } // Make sure the original pointer isn't modified { test_bounded_ptr<QualT> const ptr(array.begin() + 4, array.begin(), array.end()); (void)(ptr - 2); _assert(&*ptr == &array[4]); } } T_DECL(arith_subtract, "bounded_ptr.arith.subtract") { tests<T, T>(); tests<T, T const>(); tests<T, T volatile>(); tests<T, T const volatile>(); } |