| 104 | } |
| 105 | |
| 106 | void memory_test::test_unique_ptr() |
| 107 | { |
| 108 | mock::clear(); |
| 109 | { |
| 110 | auto ptr = std::make_unique<mock>(); |
| 111 | test_eq("unique_ptr get() when non-null", (ptr.get() != nullptr), true); |
| 112 | test_eq("unique_ptr operator bool when non-null", (bool)ptr, true); |
| 113 | } |
| 114 | test_eq("unique_ptr make_unique", mock::constructor_calls, 1U); |
| 115 | test_eq("unique_ptr out of scope", mock::destructor_calls, 1U); |
| 116 | |
| 117 | mock::clear(); |
| 118 | { |
| 119 | auto ptr = std::make_unique<mock>(); |
| 120 | delete ptr.release(); |
| 121 | } |
| 122 | test_eq("unique_ptr release", mock::destructor_calls, 1U); |
| 123 | |
| 124 | mock::clear(); |
| 125 | { |
| 126 | auto ptr = std::make_unique<mock>(); |
| 127 | ptr.reset(new mock{}); |
| 128 | } |
| 129 | test_eq("unique_ptr reset", mock::destructor_calls, 2U); |
| 130 | |
| 131 | mock::clear(); |
| 132 | { |
| 133 | std::unique_ptr<mock> ptr1{}; |
| 134 | test_eq("unique_ptr get() when null", ptr1.get(), nullptr); |
| 135 | test_eq("unique_ptr operator bool when null", (bool)ptr1, false); |
| 136 | { |
| 137 | auto ptr2 = std::make_unique<mock>(); |
| 138 | ptr1 = std::move(ptr2); |
| 139 | } |
| 140 | test_eq("unique_ptr move pt1", mock::destructor_calls, 0U); |
| 141 | } |
| 142 | test_eq("unique_ptr move pt2", mock::destructor_calls, 1U); |
| 143 | |
| 144 | mock::clear(); |
| 145 | { |
| 146 | auto ptr = std::make_unique<mock[]>(10U); |
| 147 | test_eq("unique_ptr make_unique array version", mock::constructor_calls, 10U); |
| 148 | |
| 149 | new(&ptr[5]) mock{}; |
| 150 | test_eq("placement new into the array", mock::constructor_calls, 11U); |
| 151 | test_eq("original not destroyed during placement new", mock::destructor_calls, 0U); |
| 152 | } |
| 153 | test_eq("unique_ptr array out of scope", mock::destructor_calls, 10U); |
| 154 | } |
| 155 | |
| 156 | void memory_test::test_shared_ptr() |
| 157 | { |