From b01d495beb586d8d18ef592d80f593ea1bbe10e2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 28 Feb 2020 06:46:26 +0100 Subject: new_type: implement rbegin and crbegin --- test/src/iterable_suite.cpp | 136 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/src/iterable_suite.cpp b/test/src/iterable_suite.cpp index 3bdb16b..778dc35 100644 --- a/test/src/iterable_suite.cpp +++ b/test/src/iterable_suite.cpp @@ -18,10 +18,15 @@ namespace { using iterator = void *; using const_iterator = void const *; + using reverse_iterator = void *; + using const_reverse_iterator = void const *; auto begin() -> iterator; auto begin() const -> const_iterator; auto cbegin() const -> const_iterator; + auto rbegin() -> reverse_iterator; + auto rbegin() const -> const_reverse_iterator; + auto crbegin() const -> const_reverse_iterator; }; } // namespace @@ -135,6 +140,117 @@ inline namespace cbegin_tests } // namespace cbegin_tests +inline namespace rbegin_tests +{ + + auto a_new__type_not_deriving_iterable_has_no_rbegin() -> void + { + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_rbegin_v)); + } + + auto a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_rbegin() -> void + { + static_assert(!nt::impl::has_rbegin_v); + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_rbegin_v)); + } + + auto a_new__type_based_on_an_iterable_type_with_member_rbegin_deriving_iterable_has_member_rbegin() -> void + { + static_assert(nt::impl::has_member_rbegin_v); + using type_alias = nt::new_type; + ASSERT(nt::impl::has_member_rbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_with_constant_member_rbegin_deriving_iterable_has_constant_member_rbegin() -> void + { + static_assert(nt::impl::has_member_rbegin_v); + using type_alias = nt::new_type; + ASSERT(nt::impl::has_member_rbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_without_free_rbegin_deriving_iterable_has_no_free_rbegin() -> void + { + static_assert(!nt::impl::has_free_rbegin_v); + using type_alias = nt::new_type; + ASSERT(!nt::impl::has_free_rbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_without_constant_free_rbegin_deriving_iterable_has_no_constant_free_rbegin() -> void + { + static_assert(!nt::impl::has_free_rbegin_v); + using type_alias = nt::new_type; + ASSERT(!nt::impl::has_free_rbegin_v); + } + + auto accessing_the_first_element_of_a_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type() + -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto weak = std::array{42, 21, 10}; + auto strong = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(*weak.rbegin(), *strong.rbegin()); + } + + auto an_iterator_obtained_via_member_rbegin_compares_equal_to_an_iterator_obtained_via_free_rbegin() -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto instance = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(rbegin(instance), instance.rbegin()); + } + +} // namespace rbegin_tests + +inline namespace crbegin_tests +{ + + auto a_new__type_not_deriving_iterable_has_no_crbegin() -> void + { + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_crbegin_v)); + } + + auto a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_crbegin() -> void + { + static_assert(!nt::impl::has_crbegin_v); + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_crbegin_v)); + } + + auto a_new__type_based_on_an_iterable_type_with_member_crbegin_deriving_iterable_has_member_crbegin() -> void + { + static_assert(nt::impl::has_member_crbegin_v); + using type_alias = nt::new_type; + ASSERT(nt::impl::has_member_crbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_without_free_crbegin_deriving_iterable_has_no_free_crbegin() -> void + { + static_assert(!nt::impl::has_free_crbegin_v); + using type_alias = nt::new_type; + ASSERT(!nt::impl::has_free_crbegin_v); + } + + auto + accessing_the_first_element_of_a_constant_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type() + -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto weak = std::array{42, 21, 10}; + auto strong = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(*weak.crbegin(), *strong.crbegin()); + } + + auto an_iterator_obtained_via_member_crbegin_compares_equal_to_an_iterator_obtained_via_free_crbegin() -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto instance = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(crbegin(instance), instance.crbegin()); + } + +} // namespace crbegin_tests + auto iterable_suite() -> std::pair { return { @@ -150,13 +266,31 @@ auto iterable_suite() -> std::pair KAWAII(accessing_the_first_element_of_an_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), KAWAII(an_iterator_obtained_via_member_begin_compares_equal_to_an_iterator_obtained_via_free_begin), - /// 'begin' Tests + /// 'cbegin' Tests KAWAII(a_new__type_not_deriving_iterable_has_no_cbegin), KAWAII(a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_cbegin), KAWAII(a_new__type_based_on_an_iterable_type_with_member_cbegin_deriving_iterable_has_member_cbegin), KAWAII(a_new__type_based_on_an_iterable_type_without_free_cbegin_deriving_iterable_has_no_free_cbegin), KAWAII(accessing_the_first_element_of_a_constant_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), KAWAII(an_iterator_obtained_via_member_cbegin_compares_equal_to_an_iterator_obtained_via_free_cbegin), + + /// 'rbegin' Tests + KAWAII(a_new__type_not_deriving_iterable_has_no_rbegin), + KAWAII(a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_with_member_rbegin_deriving_iterable_has_member_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_with_constant_member_rbegin_deriving_iterable_has_constant_member_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_without_free_rbegin_deriving_iterable_has_no_free_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_without_constant_free_rbegin_deriving_iterable_has_no_constant_free_rbegin), + KAWAII(accessing_the_first_element_of_a_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), + KAWAII(an_iterator_obtained_via_member_rbegin_compares_equal_to_an_iterator_obtained_via_free_rbegin), + + /// 'cbegin' Tests + KAWAII(a_new__type_not_deriving_iterable_has_no_crbegin), + KAWAII(a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_crbegin), + KAWAII(a_new__type_based_on_an_iterable_type_with_member_crbegin_deriving_iterable_has_member_crbegin), + KAWAII(a_new__type_based_on_an_iterable_type_without_free_crbegin_deriving_iterable_has_no_free_crbegin), + KAWAII(accessing_the_first_element_of_a_constant_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), + KAWAII(an_iterator_obtained_via_member_crbegin_compares_equal_to_an_iterator_obtained_via_free_crbegin), // clang-format on }, "Iterable Tests"}; -- cgit v1.2.3