Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (c) 2015 Microsoft Corporation. All rights reserved. |
| 4 | // |
| 5 | // This code is licensed under the MIT License (MIT). |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | // THE SOFTWARE. |
| 14 | // |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 17 | #include <UnitTest++/UnitTest++.h> |
| 18 | |
Galik | 222c2d8 | 2016-08-10 17:24:00 +0100 | [diff] [blame] | 19 | #include <gsl/gsl> |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 20 | |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <string> |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 23 | #include <vector> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 24 | |
Neil MacIntosh | ef626fd | 2015-09-29 16:41:37 -0700 | [diff] [blame] | 25 | using namespace gsl; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 26 | |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 27 | struct MyBase |
| 28 | { |
| 29 | }; |
| 30 | struct MyDerived : public MyBase |
| 31 | { |
| 32 | }; |
| 33 | struct Unrelated |
| 34 | { |
| 35 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 36 | |
| 37 | // stand-in for a user-defined ref-counted class |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 38 | template <typename T> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 39 | struct RefCounted |
| 40 | { |
| 41 | RefCounted(T* p) : p_(p) {} |
| 42 | operator T*() { return p_; } |
| 43 | T* p_; |
| 44 | }; |
| 45 | |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 46 | // user defined smart pointer with comparison operators returning non bool value |
| 47 | template <typename T> |
| 48 | struct CustomPtr |
| 49 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 50 | CustomPtr(T* p) : p_(p) {} |
| 51 | operator T*() { return p_; } |
| 52 | bool operator!=(std::nullptr_t) const { return p_ != nullptr; } |
| 53 | T* p_ = nullptr; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | template <typename T, typename U> |
| 57 | std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs) |
| 58 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 59 | return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true" |
| 60 | : "false"; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | template <typename T, typename U> |
| 64 | std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs) |
| 65 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 66 | return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true" |
| 67 | : "false"; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | template <typename T, typename U> |
| 71 | std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs) |
| 72 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 73 | return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true" |
| 74 | : "false"; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | template <typename T, typename U> |
| 78 | std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs) |
| 79 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 80 | return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true" |
| 81 | : "false"; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | template <typename T, typename U> |
| 85 | std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs) |
| 86 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 87 | return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true" |
| 88 | : "false"; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | template <typename T, typename U> |
| 92 | std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs) |
| 93 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 94 | return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true" |
| 95 | : "false"; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 96 | } |
| 97 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 98 | SUITE(NotNullTests) |
| 99 | { |
| 100 | |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 101 | bool helper(not_null<int*> p) { return *p == 12; } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 102 | |
| 103 | TEST(TestNotNullConstructors) |
| 104 | { |
| 105 | #ifdef CONFIRM_COMPILATION_ERRORS |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 106 | not_null<int*> p = nullptr; // yay...does not compile! |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 107 | not_null<std::vector<char>*> p = 0; // yay...does not compile! |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 108 | not_null<int*> p; // yay...does not compile! |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 109 | std::unique_ptr<int> up = std::make_unique<int>(120); |
| 110 | not_null<int*> p = up; |
Kern Handa | 2b6d904 | 2015-09-25 09:41:40 -0700 | [diff] [blame] | 111 | |
| 112 | // Forbid non-nullptr assignable types |
| 113 | not_null<std::vector<int>> f(std::vector<int>{1}); |
| 114 | not_null<int> z(10); |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 115 | not_null<std::vector<int>> y({1, 2}); |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 116 | #endif |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 117 | int i = 12; |
| 118 | auto rp = RefCounted<int>(&i); |
| 119 | not_null<int*> p(rp); |
| 120 | CHECK(p.get() == &i); |
Kern Handa | 2b6d904 | 2015-09-25 09:41:40 -0700 | [diff] [blame] | 121 | |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 122 | not_null<std::shared_ptr<int>> x( |
| 123 | std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | TEST(TestNotNullCasting) |
| 127 | { |
Kern Handa | 783eaab | 2015-09-28 07:35:18 +0000 | [diff] [blame] | 128 | MyBase base; |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 129 | MyDerived derived; |
| 130 | Unrelated unrelated; |
| 131 | not_null<Unrelated*> u = &unrelated; |
| 132 | (void) u; |
| 133 | not_null<MyDerived*> p = &derived; |
Kern Handa | 783eaab | 2015-09-28 07:35:18 +0000 | [diff] [blame] | 134 | not_null<MyBase*> q = &base; |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 135 | q = p; // allowed with heterogeneous copy ctor |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 136 | CHECK(q == p); |
| 137 | |
| 138 | #ifdef CONFIRM_COMPILATION_ERRORS |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 139 | q = u; // no viable conversion possible between MyBase* and Unrelated* |
| 140 | p = q; // not possible to implicitly convert MyBase* to MyDerived* |
Kern Handa | 783eaab | 2015-09-28 07:35:18 +0000 | [diff] [blame] | 141 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 142 | not_null<Unrelated*> r = p; |
| 143 | not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p); |
| 144 | #endif |
| 145 | not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get()); |
Rian Quinn | d641796 | 2016-11-03 19:38:32 -0600 | [diff] [blame] | 146 | CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get())); |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | TEST(TestNotNullAssignment) |
| 150 | { |
| 151 | int i = 12; |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 152 | not_null<int*> p = &i; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 153 | CHECK(helper(p)); |
| 154 | |
| 155 | int* q = nullptr; |
| 156 | CHECK_THROW(p = q, fail_fast); |
| 157 | } |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 158 | |
| 159 | TEST(TestNotNullRawPointerComparison) |
| 160 | { |
| 161 | int ints[2] = {42, 43}; |
| 162 | int* p1 = &ints[0]; |
| 163 | const int* p2 = &ints[1]; |
| 164 | |
| 165 | using NotNull1 = not_null<decltype(p1)>; |
| 166 | using NotNull2 = not_null<decltype(p2)>; |
| 167 | |
| 168 | CHECK((NotNull1(p1) == NotNull1(p1)) == true); |
| 169 | CHECK((NotNull1(p1) == NotNull2(p2)) == false); |
| 170 | |
| 171 | CHECK((NotNull1(p1) != NotNull1(p1)) == false); |
| 172 | CHECK((NotNull1(p1) != NotNull2(p2)) == true); |
| 173 | |
| 174 | CHECK((NotNull1(p1) < NotNull1(p1)) == false); |
| 175 | CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2)); |
| 176 | CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1)); |
| 177 | |
| 178 | CHECK((NotNull1(p1) > NotNull1(p1)) == false); |
| 179 | CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2)); |
| 180 | CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1)); |
| 181 | |
| 182 | CHECK((NotNull1(p1) <= NotNull1(p1)) == true); |
| 183 | CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2)); |
| 184 | CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1)); |
| 185 | |
| 186 | CHECK((NotNull1(p1) >= NotNull1(p1)) == true); |
| 187 | CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2)); |
| 188 | CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1)); |
| 189 | } |
| 190 | |
| 191 | TEST(TestNotNullSharedPtrComparison) |
| 192 | { |
| 193 | auto sp1 = std::make_shared<int>(42); |
| 194 | auto sp2 = std::make_shared<const int>(43); |
| 195 | |
| 196 | using NotNullSp1 = not_null<decltype(sp1)>; |
| 197 | using NotNullSp2 = not_null<decltype(sp2)>; |
| 198 | |
| 199 | CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true); |
| 200 | CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false); |
| 201 | |
| 202 | CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false); |
| 203 | CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true); |
| 204 | |
| 205 | CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false); |
| 206 | CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2)); |
| 207 | CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1)); |
| 208 | |
| 209 | CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false); |
| 210 | CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2)); |
| 211 | CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1)); |
| 212 | |
| 213 | CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true); |
| 214 | CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2)); |
| 215 | CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1)); |
| 216 | |
| 217 | CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true); |
| 218 | CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2)); |
| 219 | CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1)); |
| 220 | } |
| 221 | |
| 222 | TEST(TestNotNullCustomPtrComparison) |
| 223 | { |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 224 | int ints[2] = {42, 43}; |
Alexey Malov | 534bb4c | 2017-04-13 03:34:39 +0300 | [diff] [blame] | 225 | CustomPtr<int> p1(&ints[0]); |
| 226 | CustomPtr<const int> p2(&ints[1]); |
| 227 | |
| 228 | using NotNull1 = not_null<decltype(p1)>; |
| 229 | using NotNull2 = not_null<decltype(p2)>; |
| 230 | |
| 231 | CHECK((NotNull1(p1) == NotNull1(p1)) == "true"); |
| 232 | CHECK((NotNull1(p1) == NotNull2(p2)) == "false"); |
| 233 | |
| 234 | CHECK((NotNull1(p1) != NotNull1(p1)) == "false"); |
| 235 | CHECK((NotNull1(p1) != NotNull2(p2)) == "true"); |
| 236 | |
| 237 | CHECK((NotNull1(p1) < NotNull1(p1)) == "false"); |
| 238 | CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2)); |
| 239 | CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1)); |
| 240 | |
| 241 | CHECK((NotNull1(p1) > NotNull1(p1)) == "false"); |
| 242 | CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2)); |
| 243 | CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1)); |
| 244 | |
| 245 | CHECK((NotNull1(p1) <= NotNull1(p1)) == "true"); |
| 246 | CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2)); |
| 247 | CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1)); |
| 248 | |
| 249 | CHECK((NotNull1(p1) >= NotNull1(p1)) == "true"); |
| 250 | CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2)); |
| 251 | CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1)); |
| 252 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Tiago | ebe7ebf | 2017-04-20 07:51:37 -0700 | [diff] [blame^] | 255 | int main(int, const char* []) { return UnitTest::RunAllTests(); } |