From 284301b3cb8f0dbbd6e34863b839e21ef4bea103 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 8 Jun 2023 10:06:50 +0200 Subject: tests: port constructor tests --- source/tests/CMakeLists.txt | 2 +- source/tests/src/constructors.cpp | 103 ++++++++++++++++++++++++ source/tests/src/new_type_constructor_suite.cpp | 91 --------------------- 3 files changed, 104 insertions(+), 92 deletions(-) create mode 100644 source/tests/src/constructors.cpp delete mode 100644 source/tests/src/new_type_constructor_suite.cpp diff --git a/source/tests/CMakeLists.txt b/source/tests/CMakeLists.txt index 3c206db..7d08e12 100644 --- a/source/tests/CMakeLists.txt +++ b/source/tests/CMakeLists.txt @@ -8,13 +8,13 @@ include("Catch") add_executable("${PROJECT_NAME}_tests" "src/arithmetic.cpp" + "src/constructors.cpp" "src/conversion.cpp" "src/derivation_clause.cpp" "src/equality_comparison.cpp" "src/hash.cpp" "src/io_operators.cpp" # "src/iterable_suite.cpp" - # "src/new_type_constructor_suite.cpp" # "src/relational_operators_suite.cpp" ) diff --git a/source/tests/src/constructors.cpp b/source/tests/src/constructors.cpp new file mode 100644 index 0000000..75e1839 --- /dev/null +++ b/source/tests/src/constructors.cpp @@ -0,0 +1,103 @@ +#include "newtype/derivable.hpp" +#include "newtype/newtype.hpp" + +#include +#include + +#include + +using fundamental_types = std::tuple; + +TEMPLATE_LIST_TEST_CASE("Scenario: Construction from Fundamental Types", "[construction]", fundamental_types) +{ + GIVEN("A new_type over a fundamental type") + { + using type_alias = nt::new_type; + + THEN("objects of it can be constructed from the fundamental type") + { + STATIC_REQUIRE(std::is_constructible_v); + } + } +} + +SCENARIO("Default Construction", "[construction]") +{ + struct not_default_constructible + { + not_default_constructible() = delete; + }; + + GIVEN("A new_type over a default-constructible type") + { + using type_alias = nt::new_type; + static_assert(std::is_default_constructible_v); + + THEN("it is default-constructible") + { + STATIC_REQUIRE(std::is_default_constructible_v); + } + } + + GIVEN("A new_type over a type that is not default-constructible") + { + using type_alias = nt::new_type; + static_assert(!std::is_default_constructible_v); + + THEN("it is not default-constructible") + { + STATIC_REQUIRE_FALSE(std::is_default_constructible_v); + } + } +} + +SCENARIO("Copy Construction", "[construction]") +{ + struct not_copy_constructible + { + not_copy_constructible() = default; + not_copy_constructible(not_copy_constructible const &) = delete; + not_copy_constructible(not_copy_constructible &&) = default; + auto operator=(not_copy_constructible const &) -> not_copy_constructible & = default; + auto operator=(not_copy_constructible &&) -> not_copy_constructible & = default; + }; + + GIVEN("A new_type over a copy-constructible type") + { + using type_alias = nt::new_type; + static_assert(std::is_copy_constructible_v); + + THEN("it is copy-constructible") + { + STATIC_REQUIRE(std::is_copy_constructible_v); + } + } + + GIVEN("A new_type over a type that is not copy-constructible") + { + using type_alias = nt::new_type; + static_assert(!std::is_copy_constructible_v); + + THEN("it is not copy-constructible") + { + STATIC_REQUIRE_FALSE(std::is_copy_constructible_v); + } + } +} diff --git a/source/tests/src/new_type_constructor_suite.cpp b/source/tests/src/new_type_constructor_suite.cpp deleted file mode 100644 index fc3e9f6..0000000 --- a/source/tests/src/new_type_constructor_suite.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "new_type_constructor_suite.hpp" - -#include "kawaii.hpp" -#include "newtype/derivable.hpp" -#include "newtype/newtype.hpp" - -#include - -#include - -inline namespace constructor_tests -{ - - struct not_default_constructible - { - not_default_constructible() = delete; - }; - - auto a_new__type_based_on_a_type_that_is_default_constructible_is_default_constructible_too() -> void - { - using nt_float = nt::new_type; - - ASSERT(std::is_default_constructible_v); - } - - auto a_new__type_based_on_a_type_that_is_not_default_constructible_is_not_default_constructible_too() -> void - { - using nt_not_default_constructible = nt::new_type; - - ASSERT(!std::is_default_constructible_v); - } - - template - auto a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type() -> void - { - using nt_old = nt::new_type; - - ASSERT((std::is_constructible_v)); - } - - auto a_new__type_instance_can_be_copy_constructed_if_the_base_type_can_be_copy_constructed() -> void - { - using type_alias = nt::new_type; - ASSERT((std::is_copy_constructible_v)); - } - - auto a_new__type_instance_can_not_be_copy_constructed_if_the_base_type_can_not_be_copy_constructed() -> void - { - struct not_copy_constructible - { - not_copy_constructible() = default; - not_copy_constructible(not_copy_constructible const &) = delete; - not_copy_constructible(not_copy_constructible &&) = default; - auto operator=(not_copy_constructible const &) -> not_copy_constructible & = default; - auto operator=(not_copy_constructible &&) -> not_copy_constructible & = default; - }; - - using type_alias = nt::new_type; - ASSERT(!(std::is_copy_constructible_v)); - } - -} // namespace constructor_tests - -auto new_type_constructor_suite() -> std::pair -{ - return {{ - KAWAII(a_new__type_based_on_a_type_that_is_default_constructible_is_default_constructible_too), - KAWAII(a_new__type_based_on_a_type_that_is_not_default_constructible_is_not_default_constructible_too), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type), - KAWAII(a_new__type_instance_can_be_copy_constructed_if_the_base_type_can_be_copy_constructed), - KAWAII(a_new__type_instance_can_not_be_copy_constructed_if_the_base_type_can_not_be_copy_constructed), - }, - "new_type Constructor Tests"}; -} \ No newline at end of file -- cgit v1.2.3