1 // 2 // Tests for 3 // bounded_ptr& operator++(); 4 // bounded_ptr operator++(int); 5 // bounded_ptr& operator--(); 6 // bounded_ptr operator--(int); 7 // 8 9 #include <libkern/c++/bounded_ptr.h> 10 #include <array> 11 #include <darwintest.h> 12 #include <darwintest_utils.h> 13 #include "test_utils.h" 14 15 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__) 16 17 struct T { 18 int i; 19 }; 20 21 template <typename T, typename QualT> 22 static void 23 tests() 24 { 25 std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}}; 26 27 { 28 // Test pre-increment and pre-decrement 29 test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end()); 30 _assert(&*ptr == &array[0]); 31 32 { 33 auto& ref = ++ptr; 34 _assert(&ref == &ptr); 35 _assert(&*ptr == &array[1]); 36 } 37 38 { 39 auto& ref = ++ptr; 40 _assert(&ref == &ptr); 41 _assert(&*ptr == &array[2]); 42 } 43 { 44 auto& ref = ++ptr; 45 _assert(&ref == &ptr); 46 _assert(&*ptr == &array[3]); 47 } 48 { 49 auto& ref = ++ptr; 50 _assert(&ref == &ptr); 51 _assert(&*ptr == &array[4]); 52 } 53 { 54 auto& ref = ++ptr; 55 _assert(&ref == &ptr); 56 // ptr is now one-past-last 57 } 58 { 59 auto& ref = --ptr; 60 _assert(&ref == &ptr); 61 _assert(&*ptr == &array[4]); 62 } 63 { 64 auto& ref = --ptr; 65 _assert(&ref == &ptr); 66 _assert(&*ptr == &array[3]); 67 } 68 { 69 auto& ref = --ptr; 70 _assert(&ref == &ptr); 71 _assert(&*ptr == &array[2]); 72 } 73 { 74 auto& ref = --ptr; 75 _assert(&ref == &ptr); 76 _assert(&*ptr == &array[1]); 77 } 78 { 79 auto& ref = --ptr; 80 _assert(&ref == &ptr); 81 _assert(&*ptr == &array[0]); 82 } 83 } 84 { 85 // Test post-increment and post-decrement 86 test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end()); 87 _assert(&*ptr == &array[0]); 88 89 { 90 auto prev = ptr++; 91 _assert(&*prev == &array[0]); 92 _assert(&*ptr == &array[1]); 93 } 94 { 95 auto prev = ptr++; 96 _assert(&*prev == &array[1]); 97 _assert(&*ptr == &array[2]); 98 } 99 { 100 auto prev = ptr++; 101 _assert(&*prev == &array[2]); 102 _assert(&*ptr == &array[3]); 103 } 104 { 105 auto prev = ptr++; 106 _assert(&*prev == &array[3]); 107 _assert(&*ptr == &array[4]); 108 } 109 { 110 auto prev = ptr++; 111 _assert(&*prev == &array[4]); 112 _assert(ptr == array.end()); 113 } 114 { 115 auto prev = ptr--; 116 _assert(prev == array.end()); 117 _assert(&*ptr == &array[4]); 118 } 119 { 120 auto prev = ptr--; 121 _assert(&*prev == &array[4]); 122 _assert(&*ptr == &array[3]); 123 } 124 { 125 auto prev = ptr--; 126 _assert(&*prev == &array[3]); 127 _assert(&*ptr == &array[2]); 128 } 129 { 130 auto prev = ptr--; 131 _assert(&*prev == &array[2]); 132 _assert(&*ptr == &array[1]); 133 } 134 { 135 auto prev = ptr--; 136 _assert(&*prev == &array[1]); 137 _assert(&*ptr == &array[0]); 138 } 139 } 140 } 141 142 T_DECL(arith_inc_dec, "bounded_ptr.arith.inc_dec") { 143 tests<T, T>(); 144 tests<T, T const>(); 145 tests<T, T volatile>(); 146 tests<T, T const volatile>(); 147 } 148