Skip to content

Commit

Permalink
added tests for non copyable not_null
Browse files Browse the repository at this point in the history
also, declared not_null move assignment as defaulted
  • Loading branch information
ericLemanissier committed May 30, 2018
1 parent 2b2b333 commit 39d4fa5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/gsl/pointers
Expand Up @@ -84,6 +84,7 @@ public:

not_null(not_null&& other) = default;
not_null(const not_null& other) = default;
not_null& operator=(not_null&& other) = default;
not_null& operator=(const not_null& other) = default;

constexpr T const & get() const
Expand Down
36 changes: 35 additions & 1 deletion tests/notnull_tests.cpp
Expand Up @@ -349,4 +349,38 @@ TEST_CASE("TestNotNullUniquePtrComparison") {
// values are the same
CHECK((NotNull1(std::make_unique<UniquePointerTestStruct>())->i == NotNull1(std::make_unique<UniquePointerTestStruct>())->i));
}
}
}
static_assert(!std::is_assignable<not_null<std::unique_ptr<int>>, std::unique_ptr<int>>::value, "not_null<std::unique_ptr> must be non copy constructible");
static_assert(!std::is_copy_constructible<not_null<std::unique_ptr<int>>>::value, "not_null<std::unique_ptr> must be non copy constructible");
static_assert(!std::is_copy_assignable<not_null<std::unique_ptr<int>>>::value, "not_null<std::unique_ptr> must be non copy assignable");
static_assert(std::is_nothrow_move_constructible<not_null<std::unique_ptr<int>>>::value, "not_null<std::unique_ptr> must be no-throw move constructible");
static_assert(std::is_nothrow_move_assignable<not_null<std::unique_ptr<int>>>::value, "not_null<std::unique_ptr> must be no-throw move assignable");


TEST_CASE("TestNotNullUniquePtrMove") {

{
using NotNull1 = not_null<std::unique_ptr<int>>;

// values are the same
NotNull1 src(std::make_unique<int>(42));
NotNull1 dst(std::move(src));
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
CHECK_THROWS_AS(!src.get(), fail_fast);
#endif
CHECK(dst.get());
CHECK(*dst.get() == 42);
src = std::move(dst);
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
CHECK_THROWS_AS(!dst.get(), fail_fast);
#endif
CHECK(src.get());
CHECK(*src.get() == 42);
}
{
using NotNull1 = not_null<std::unique_ptr<UniquePointerTestStruct>>;

// values are the same
CHECK((NotNull1(std::make_unique<UniquePointerTestStruct>())->i == NotNull1(std::make_unique<UniquePointerTestStruct>())->i));
}
}

0 comments on commit 39d4fa5

Please sign in to comment.