// // Tests for // friend bounded_ptr operator+(bounded_ptr p, std::ptrdiff_t n); // friend bounded_ptr operator+(std::ptrdiff_t n, bounded_ptr p); // // The heavy lifting is done in operator+=, so we only check basic functioning. // #include #include #include #include #include "test_utils.h" #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__) struct T { int i; }; template static void tests() { std::array array = {T{0}, T{1}, T{2}, T{3}, T{4}}; // Add positive offsets // T{0} T{1} T{2} T{3} T{4} // ^ ^ // | | // begin, ptr end { test_bounded_ptr const ptr(array.begin(), array.begin(), array.end()); { test_bounded_ptr res = ptr + 0; _assert(&*res == &array[0]); } { test_bounded_ptr res = ptr + 1; _assert(&*res == &array[1]); } { test_bounded_ptr res = ptr + 2; _assert(&*res == &array[2]); } { test_bounded_ptr res = ptr + 3; _assert(&*res == &array[3]); } { test_bounded_ptr res = ptr + 4; _assert(&*res == &array[4]); } { test_bounded_ptr res = ptr + 5; _assert(res == array.end()); } } // Add negative offsets // T{0} T{1} T{2} T{3} T{4} // ^ ^ // | | // begin end,ptr { test_bounded_ptr const ptr(array.end(), array.begin(), array.end()); { test_bounded_ptr res = ptr + 0; _assert(res == array.end()); } { test_bounded_ptr res = ptr + -1; _assert(&*res == &array[4]); } { test_bounded_ptr res = ptr + -2; _assert(&*res == &array[3]); } { test_bounded_ptr res = ptr + -3; _assert(&*res == &array[2]); } { test_bounded_ptr res = ptr + -4; _assert(&*res == &array[1]); } { test_bounded_ptr res = ptr + -5; _assert(&*res == &array[0]); } } // Make sure the original pointer isn't modified { test_bounded_ptr const ptr(array.begin() + 1, array.begin(), array.end()); (void)(ptr + 3); _assert(&*ptr == &array[1]); } // Make sure the operator is commutative { { test_bounded_ptr const ptr(array.begin(), array.begin(), array.end()); test_bounded_ptr res = 0 + ptr; _assert(&*res == &array[0]); } { test_bounded_ptr const ptr(array.begin(), array.begin(), array.end()); test_bounded_ptr res = 3 + ptr; _assert(&*res == &array[3]); } { test_bounded_ptr const ptr(array.begin() + 3, array.begin(), array.end()); test_bounded_ptr res = -2 + ptr; _assert(&*res == &array[1]); } } } T_DECL(arith_add, "bounded_ptr.arith.add", T_META_TAG_VM_PREFERRED) { tests(); tests(); tests(); tests(); }