2015-08-20 21:09:14 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-04-20 10:51:37 -04:00
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
#include <catch/catch.hpp>
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2016-08-10 12:24:00 -04:00
|
|
|
#include <gsl/gsl>
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2017-04-12 20:34:39 -04:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2017-04-20 10:51:37 -04:00
|
|
|
#include <vector>
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2015-09-29 19:41:37 -04:00
|
|
|
using namespace gsl;
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
struct MyBase
|
|
|
|
{
|
|
|
|
};
|
|
|
|
struct MyDerived : public MyBase
|
|
|
|
{
|
|
|
|
};
|
|
|
|
struct Unrelated
|
|
|
|
{
|
|
|
|
};
|
2015-08-20 21:09:14 -04:00
|
|
|
|
|
|
|
// stand-in for a user-defined ref-counted class
|
2017-04-20 10:51:37 -04:00
|
|
|
template <typename T>
|
2015-08-20 21:09:14 -04:00
|
|
|
struct RefCounted
|
|
|
|
{
|
|
|
|
RefCounted(T* p) : p_(p) {}
|
|
|
|
operator T*() { return p_; }
|
|
|
|
T* p_;
|
|
|
|
};
|
|
|
|
|
2017-04-12 20:34:39 -04:00
|
|
|
// user defined smart pointer with comparison operators returning non bool value
|
|
|
|
template <typename T>
|
|
|
|
struct CustomPtr
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
CustomPtr(T* p) : p_(p) {}
|
|
|
|
operator T*() { return p_; }
|
|
|
|
bool operator!=(std::nullptr_t) const { return p_ != nullptr; }
|
|
|
|
T* p_ = nullptr;
|
2017-04-12 20:34:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-31 14:18:55 -04:00
|
|
|
struct NonCopyableNonMovable
|
|
|
|
{
|
|
|
|
NonCopyableNonMovable() = default;
|
|
|
|
NonCopyableNonMovable(const NonCopyableNonMovable&) = delete;
|
|
|
|
NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete;
|
|
|
|
NonCopyableNonMovable(NonCopyableNonMovable&&) = delete;
|
|
|
|
NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete;
|
|
|
|
};
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
bool helper(not_null<int*> p) { return *p == 12; }
|
2017-05-31 14:18:55 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("TestNotNullConstructors")
|
2015-08-20 21:09:14 -04:00
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
not_null<int*> p = nullptr; // yay...does not compile!
|
|
|
|
not_null<std::vector<char>*> p = 0; // yay...does not compile!
|
|
|
|
not_null<int*> p; // yay...does not compile!
|
|
|
|
std::unique_ptr<int> up = std::make_unique<int>(120);
|
|
|
|
not_null<int*> p = up;
|
|
|
|
|
|
|
|
// Forbid non-nullptr assignable types
|
|
|
|
not_null<std::vector<int>> f(std::vector<int>{1});
|
|
|
|
not_null<int> z(10);
|
|
|
|
not_null<std::vector<int>> y({1, 2});
|
2015-08-20 21:09:14 -04:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
int i = 12;
|
|
|
|
auto rp = RefCounted<int>(&i);
|
|
|
|
not_null<int*> p(rp);
|
|
|
|
CHECK(p.get() == &i);
|
2015-09-25 12:41:40 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
not_null<std::shared_ptr<int>> x(
|
|
|
|
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
|
|
|
}
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("TestNotNullCasting")
|
|
|
|
{
|
|
|
|
MyBase base;
|
|
|
|
MyDerived derived;
|
|
|
|
Unrelated unrelated;
|
|
|
|
not_null<Unrelated*> u = &unrelated;
|
|
|
|
(void) u;
|
|
|
|
not_null<MyDerived*> p = &derived;
|
|
|
|
not_null<MyBase*> q = &base;
|
|
|
|
q = p; // allowed with heterogeneous copy ctor
|
|
|
|
CHECK(q == p);
|
2015-08-20 21:09:14 -04:00
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
q = u; // no viable conversion possible between MyBase* and Unrelated*
|
|
|
|
p = q; // not possible to implicitly convert MyBase* to MyDerived*
|
2015-09-28 03:35:18 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
not_null<Unrelated*> r = p;
|
|
|
|
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
|
2015-08-20 21:09:14 -04:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get());
|
|
|
|
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
|
|
|
|
}
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("TestNotNullAssignment")
|
|
|
|
{
|
|
|
|
int i = 12;
|
|
|
|
not_null<int*> p = &i;
|
|
|
|
CHECK(helper(p));
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
int* q = nullptr;
|
|
|
|
CHECK_THROWS_AS(p = q, fail_fast);
|
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("TestNotNullRawPointerComparison")
|
|
|
|
{
|
|
|
|
int ints[2] = {42, 43};
|
|
|
|
int* p1 = &ints[0];
|
|
|
|
const int* p2 = &ints[1];
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNull1 = not_null<decltype(p1)>;
|
|
|
|
using NotNull2 = not_null<decltype(p2)>;
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) == NotNull1(p1)) == true);
|
|
|
|
CHECK((NotNull1(p1) == NotNull2(p2)) == false);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) != NotNull1(p1)) == false);
|
|
|
|
CHECK((NotNull1(p1) != NotNull2(p2)) == true);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) < NotNull1(p1)) == false);
|
|
|
|
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
|
|
|
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) > NotNull1(p1)) == false);
|
|
|
|
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
|
|
|
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) <= NotNull1(p1)) == true);
|
|
|
|
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
|
|
|
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("TestNotNullDereferenceOperator")
|
|
|
|
{
|
2017-04-12 20:34:39 -04:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
auto sp1 = std::make_shared<NonCopyableNonMovable>();
|
2017-04-12 20:34:39 -04:00
|
|
|
|
|
|
|
using NotNullSp1 = not_null<decltype(sp1)>;
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
|
|
|
|
CHECK(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
|
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int ints[1] = { 42 };
|
|
|
|
CustomPtr<int> p1(&ints[0]);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNull1 = not_null<decltype(p1)>;
|
|
|
|
CHECK(typeid(*NotNull1(p1)) == typeid(*p1));
|
|
|
|
CHECK(*NotNull1(p1) == 42);
|
|
|
|
*NotNull1(p1) = 43;
|
|
|
|
CHECK(ints[0] == 43);
|
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int v = 42;
|
|
|
|
gsl::not_null<int*> p(&v);
|
|
|
|
CHECK(typeid(*p) == typeid(*(&v)));
|
|
|
|
*p = 43;
|
|
|
|
CHECK(v == 43);
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("TestNotNullSharedPtrComparison")
|
|
|
|
{
|
|
|
|
auto sp1 = std::make_shared<int>(42);
|
|
|
|
auto sp2 = std::make_shared<const int>(43);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNullSp1 = not_null<decltype(sp1)>;
|
|
|
|
using NotNullSp2 = not_null<decltype(sp2)>;
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
|
|
|
|
CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
|
|
|
|
CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
|
|
|
|
CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
|
|
|
|
CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
|
|
|
|
CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
|
|
|
|
CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
|
|
|
|
CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
|
|
|
|
CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
|
|
|
|
CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
|
|
|
|
CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
|
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("TestNotNullCustomPtrComparison")
|
|
|
|
{
|
|
|
|
int ints[2] = {42, 43};
|
|
|
|
CustomPtr<int> p1(&ints[0]);
|
|
|
|
CustomPtr<const int> p2(&ints[1]);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNull1 = not_null<decltype(p1)>;
|
|
|
|
using NotNull2 = not_null<decltype(p2)>;
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) == NotNull1(p1)) == "true");
|
|
|
|
CHECK((NotNull1(p1) == NotNull2(p2)) == "false");
|
2017-05-30 13:06:21 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) != NotNull1(p1)) == "false");
|
|
|
|
CHECK((NotNull1(p1) != NotNull2(p2)) == "true");
|
|
|
|
|
|
|
|
CHECK((NotNull1(p1) < NotNull1(p1)) == "false");
|
|
|
|
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
|
|
|
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
|
|
|
|
|
|
|
CHECK((NotNull1(p1) > NotNull1(p1)) == "false");
|
|
|
|
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
|
|
|
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((NotNull1(p1) <= NotNull1(p1)) == "true");
|
|
|
|
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
|
|
|
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
|
|
|
|
|
|
|
CHECK((NotNull1(p1) >= NotNull1(p1)) == "true");
|
|
|
|
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
|
|
|
|
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
|
|
|
|
}
|