diff options
author | Neil MacIntosh <neilmac@microsoft.com> | 2017-07-13 23:53:56 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-13 23:53:56 +0300 |
commit | b2ee48433448556a7be63074f8aaf45ab47a95c1 (patch) | |
tree | df94d91f17334f50c33dfea4763f3b4b282f5f85 /tests/notnull_tests.cpp | |
parent | 1f87ef73f1477e8adafa8b10ccee042897612a20 (diff) |
Move from unittest-cpp to catch for unit testing. (#533)
Many thanks to @rianquinn. This should fix #495, #494 and #529.
Diffstat (limited to 'tests/notnull_tests.cpp')
-rw-r--r-- | tests/notnull_tests.cpp | 290 |
1 files changed, 140 insertions, 150 deletions
diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index caab476..62f1e57 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -14,7 +14,7 @@ // /////////////////////////////////////////////////////////////////////////////// -#include <UnitTest++/UnitTest++.h> +#include <catch/catch.hpp> #include <gsl/gsl> @@ -104,192 +104,182 @@ struct NonCopyableNonMovable NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete; }; +bool helper(not_null<int*> p) { return *p == 12; } -SUITE(NotNullTests) +TEST_CASE("TestNotNullConstructors") { - - bool helper(not_null<int*> p) { return *p == 12; } - - TEST(TestNotNullConstructors) - { #ifdef CONFIRM_COMPILATION_ERRORS - 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}); + 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}); #endif - int i = 12; - auto rp = RefCounted<int>(&i); - not_null<int*> p(rp); - CHECK(p.get() == &i); + int i = 12; + auto rp = RefCounted<int>(&i); + not_null<int*> p(rp); + CHECK(p.get() == &i); - not_null<std::shared_ptr<int>> x( - std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable - } + not_null<std::shared_ptr<int>> x( + std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable +} - TEST(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); +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); #ifdef CONFIRM_COMPILATION_ERRORS - q = u; // no viable conversion possible between MyBase* and Unrelated* - p = q; // not possible to implicitly convert MyBase* to MyDerived* + q = u; // no viable conversion possible between MyBase* and Unrelated* + p = q; // not possible to implicitly convert MyBase* to MyDerived* - not_null<Unrelated*> r = p; - not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p); + not_null<Unrelated*> r = p; + not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p); #endif - not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get()); - CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get())); - } + not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get()); + CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get())); +} - TEST(TestNotNullAssignment) - { - int i = 12; - not_null<int*> p = &i; - CHECK(helper(p)); +TEST_CASE("TestNotNullAssignment") +{ + int i = 12; + not_null<int*> p = &i; + CHECK(helper(p)); - int* q = nullptr; - CHECK_THROW(p = q, fail_fast); - } + int* q = nullptr; + CHECK_THROWS_AS(p = q, fail_fast); +} - TEST(TestNotNullRawPointerComparison) - { - int ints[2] = {42, 43}; - int* p1 = &ints[0]; - const int* p2 = &ints[1]; +TEST_CASE("TestNotNullRawPointerComparison") +{ + int ints[2] = {42, 43}; + int* p1 = &ints[0]; + const int* p2 = &ints[1]; - using NotNull1 = not_null<decltype(p1)>; - using NotNull2 = not_null<decltype(p2)>; + using NotNull1 = not_null<decltype(p1)>; + using NotNull2 = not_null<decltype(p2)>; - CHECK((NotNull1(p1) == NotNull1(p1)) == true); - CHECK((NotNull1(p1) == NotNull2(p2)) == false); + CHECK((NotNull1(p1) == NotNull1(p1)) == true); + CHECK((NotNull1(p1) == NotNull2(p2)) == false); - CHECK((NotNull1(p1) != NotNull1(p1)) == false); - CHECK((NotNull1(p1) != NotNull2(p2)) == true); + 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)); - 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)); - 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)); - CHECK((NotNull1(p1) >= NotNull1(p1)) == true); - CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2)); - CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1)); - } +} - TEST(TestNotNullSharedPtrComparison) +TEST_CASE("TestNotNullDereferenceOperator") +{ { - auto sp1 = std::make_shared<int>(42); - auto sp2 = std::make_shared<const int>(43); + auto sp1 = std::make_shared<NonCopyableNonMovable>(); using NotNullSp1 = not_null<decltype(sp1)>; - using NotNullSp2 = not_null<decltype(sp2)>; + CHECK(typeid(*sp1) == typeid(*NotNullSp1(sp1))); + CHECK(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1)); + } - CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true); - CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false); + { + int ints[1] = { 42 }; + CustomPtr<int> p1(&ints[0]); - CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false); - CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true); + using NotNull1 = not_null<decltype(p1)>; + CHECK(typeid(*NotNull1(p1)) == typeid(*p1)); + CHECK(*NotNull1(p1) == 42); + *NotNull1(p1) = 43; + CHECK(ints[0] == 43); + } - CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false); - CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2)); - CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1)); + { + int v = 42; + gsl::not_null<int*> p(&v); + CHECK(typeid(*p) == typeid(*(&v))); + *p = 43; + CHECK(v == 43); + } +} - CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false); - CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2)); - CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1)); +TEST_CASE("TestNotNullSharedPtrComparison") +{ + auto sp1 = std::make_shared<int>(42); + auto sp2 = std::make_shared<const int>(43); - CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true); - CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2)); - CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1)); + using NotNullSp1 = not_null<decltype(sp1)>; + using NotNullSp2 = not_null<decltype(sp2)>; - CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true); - CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2)); - CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1)); - } + CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true); + CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false); - TEST(TestNotNullCustomPtrComparison) - { - int ints[2] = {42, 43}; - CustomPtr<int> p1(&ints[0]); - CustomPtr<const int> p2(&ints[1]); + CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false); + CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true); - using NotNull1 = not_null<decltype(p1)>; - using NotNull2 = not_null<decltype(p2)>; + CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false); + CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2)); + CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1)); - CHECK((NotNull1(p1) == NotNull1(p1)) == "true"); - CHECK((NotNull1(p1) == NotNull2(p2)) == "false"); + CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false); + CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2)); + CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1)); - CHECK((NotNull1(p1) != NotNull1(p1)) == "false"); - CHECK((NotNull1(p1) != NotNull2(p2)) == "true"); + CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true); + CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2)); + CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1)); - CHECK((NotNull1(p1) < NotNull1(p1)) == "false"); - CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2)); - CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1)); + CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true); + CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2)); + CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1)); +} - CHECK((NotNull1(p1) > NotNull1(p1)) == "false"); - CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2)); - CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1)); +TEST_CASE("TestNotNullCustomPtrComparison") +{ + int ints[2] = {42, 43}; + CustomPtr<int> p1(&ints[0]); + CustomPtr<const int> p2(&ints[1]); - CHECK((NotNull1(p1) <= NotNull1(p1)) == "true"); - CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2)); - CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1)); + using NotNull1 = not_null<decltype(p1)>; + using NotNull2 = not_null<decltype(p2)>; - 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)) == "false"); - TEST(TestNotNullDereferenceOperator) - { - { - auto sp1 = std::make_shared<NonCopyableNonMovable>(); - - using NotNullSp1 = not_null<decltype(sp1)>; - CHECK(typeid(*sp1) == typeid(*NotNullSp1(sp1))); - CHECK(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1)); - } - - { - int ints[1] = { 42 }; - CustomPtr<int> p1(&ints[0]); - - using NotNull1 = not_null<decltype(p1)>; - CHECK(typeid(*NotNull1(p1)) == typeid(*p1)); - CHECK(*NotNull1(p1) == 42); - *NotNull1(p1) = 43; - CHECK(ints[0] == 43); - } - - { - int v = 42; - gsl::not_null<int*> p(&v); - CHECK(typeid(*p) == typeid(*(&v))); - *p = 43; - CHECK(v == 43); - } - } -} + 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)); -int main(int, const char* []) { return UnitTest::RunAllTests(); } + 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)); +} |