diff options
| -rw-r--r-- | source/tests/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | source/tests/src/constructors.cpp | 103 | ||||
| -rw-r--r-- | source/tests/src/new_type_constructor_suite.cpp | 91 |
3 files changed, 104 insertions, 92 deletions
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 <catch2/catch_template_test_macros.hpp> +#include <catch2/catch_test_macros.hpp> + +#include <type_traits> + +using fundamental_types = std::tuple<bool, + char, + unsigned char, + signed char, + wchar_t, + char16_t, + char32_t, + short, + unsigned short, + int, + unsigned int, + long, + unsigned long, + long long, + unsigned long long, + float, + double, + long double>; + +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<TestType, struct tag>; + + THEN("objects of it can be constructed from the fundamental type") + { + STATIC_REQUIRE(std::is_constructible_v<type_alias, TestType>); + } + } +} + +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<int, struct tag>; + static_assert(std::is_default_constructible_v<type_alias::base_type>); + + THEN("it is default-constructible") + { + STATIC_REQUIRE(std::is_default_constructible_v<type_alias>); + } + } + + GIVEN("A new_type over a type that is not default-constructible") + { + using type_alias = nt::new_type<not_default_constructible, struct tag>; + static_assert(!std::is_default_constructible_v<type_alias::base_type>); + + THEN("it is not default-constructible") + { + STATIC_REQUIRE_FALSE(std::is_default_constructible_v<type_alias>); + } + } +} + +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<int, struct tag>; + static_assert(std::is_copy_constructible_v<type_alias::base_type>); + + THEN("it is copy-constructible") + { + STATIC_REQUIRE(std::is_copy_constructible_v<type_alias>); + } + } + + GIVEN("A new_type over a type that is not copy-constructible") + { + using type_alias = nt::new_type<not_copy_constructible, struct tag>; + static_assert(!std::is_copy_constructible_v<type_alias::base_type>); + + THEN("it is not copy-constructible") + { + STATIC_REQUIRE_FALSE(std::is_copy_constructible_v<type_alias>); + } + } +} 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 <cute/cute.h> - -#include <type_traits> - -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<float, struct nt_float_tag>; - - ASSERT(std::is_default_constructible_v<nt_float>); - } - - 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<not_default_constructible, struct nt_not_default_constructible_tag>; - - ASSERT(!std::is_default_constructible_v<nt_not_default_constructible>); - } - - template<typename OldType> - 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<OldType, struct nt_old_tag>; - - ASSERT((std::is_constructible_v<nt_old, OldType>)); - } - - auto a_new__type_instance_can_be_copy_constructed_if_the_base_type_can_be_copy_constructed() -> void - { - using type_alias = nt::new_type<int, struct tag_type>; - ASSERT((std::is_copy_constructible_v<type_alias>)); - } - - 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<not_copy_constructible, struct tag_type>; - ASSERT(!(std::is_copy_constructible_v<type_alias>)); - } - -} // namespace constructor_tests - -auto new_type_constructor_suite() -> std::pair<cute::suite, std::string> -{ - 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<bool>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<char>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<unsigned char>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<signed char>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<wchar_t>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<char16_t>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<char32_t>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<short>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<unsigned short>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<int>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<unsigned int>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<long>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<unsigned long>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<long long>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<unsigned long long>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<float>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<double>), - KAWAII(a_new__type_based_on_a_fundamental_type_can_be_constructed_with_a_value_of_fundamental_type<long double>), - 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 |
