From c935543e954222c9111a62d6898eca2fda3b21db Mon Sep 17 00:00:00 2001 From: Radu Nichita <45298861+RaduNichita@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:51:49 +0100 Subject: [PATCH] tests: Add unit tests for repeated_chars_iterator (#29) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --------- Co-authored-by: Darius Neațu --- .../iterator_interface.test.cpp | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/tests/beman/iterator_interface/iterator_interface.test.cpp b/tests/beman/iterator_interface/iterator_interface.test.cpp index 6cea90a..4856420 100644 --- a/tests/beman/iterator_interface/iterator_interface.test.cpp +++ b/tests/beman/iterator_interface/iterator_interface.test.cpp @@ -17,6 +17,14 @@ namespace {} // namespace TEST(IteratorTest, TestGTest) { ASSERT_EQ(1, 1); } +#define CONSTEXPR_EXPECT_EQ(val1, val2) \ + if (::std::is_constant_evaluated()) { \ + if (!((val1) == (val2))) { \ + ::std::abort(); \ + } \ + } else \ + EXPECT_EQ(val1, val2) + struct repeated_chars_iterator : ext_iterator_interface_compat { constexpr repeated_chars_iterator() : first_(nullptr), size_(0), n_(0) {} @@ -37,11 +45,57 @@ struct repeated_chars_iterator }; TEST(IteratorTest, TestRepeatedChars) { + auto lambda = [&] { + repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position. + repeated_chars_iterator last("foo", 3, 7); // Same as above, but now the iterator's position is 7. + std::string result; + std::copy(first, last, std::back_inserter(result)); + CONSTEXPR_EXPECT_EQ(result, "foofoof"); + }; + + static_assert((lambda(), true)); + lambda(); +} + +TEST(IteratorTest, TestDistance) { + auto lambda = [&] { + repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position. + repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position. + std::string result; + std::copy(first, last, std::back_inserter(result)); + CONSTEXPR_EXPECT_EQ(std::distance(first, last), 3); + }; + + static_assert((lambda(), true)); + lambda(); +} + +TEST(IteratorTest, TestNext) { + auto lambda = [&] { + repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position. + repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position. + CONSTEXPR_EXPECT_EQ(std::next(first, 3), last); + }; + + static_assert((lambda(), true)); + lambda(); +} + +TEST(IteratorTest, TestConcepts) { + const auto test = [](auto&& it) { + // The iterator type of it. + using iterator = typename std::remove_reference_t; + + // Check std::contiguous_iterator concept. + // Note: Check each sub-concept to get the less verbose error message first! + static_assert(std::input_iterator); + static_assert(std::forward_iterator); + static_assert(std::bidirectional_iterator); + static_assert(std::random_access_iterator); + }; + repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position. - repeated_chars_iterator last("foo", 3, 7); // Same as above, but now the iterator's position is 7. - std::string result; - std::copy(first, last, std::back_inserter(result)); - ASSERT_EQ(result, "foofoof"); + test(first); } template