Skip to content

Commit c935543

Browse files
tests: Add unit tests for repeated_chars_iterator (#29)
--------- Co-authored-by: Darius Neațu <[email protected]>
1 parent 94adc3e commit c935543

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

tests/beman/iterator_interface/iterator_interface.test.cpp

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ namespace {} // namespace
1717

1818
TEST(IteratorTest, TestGTest) { ASSERT_EQ(1, 1); }
1919

20+
#define CONSTEXPR_EXPECT_EQ(val1, val2) \
21+
if (::std::is_constant_evaluated()) { \
22+
if (!((val1) == (val2))) { \
23+
::std::abort(); \
24+
} \
25+
} else \
26+
EXPECT_EQ(val1, val2)
27+
2028
struct repeated_chars_iterator
2129
: ext_iterator_interface_compat<repeated_chars_iterator, std::random_access_iterator_tag, char, char> {
2230
constexpr repeated_chars_iterator() : first_(nullptr), size_(0), n_(0) {}
@@ -37,11 +45,57 @@ struct repeated_chars_iterator
3745
};
3846

3947
TEST(IteratorTest, TestRepeatedChars) {
48+
auto lambda = [&] {
49+
repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position.
50+
repeated_chars_iterator last("foo", 3, 7); // Same as above, but now the iterator's position is 7.
51+
std::string result;
52+
std::copy(first, last, std::back_inserter(result));
53+
CONSTEXPR_EXPECT_EQ(result, "foofoof");
54+
};
55+
56+
static_assert((lambda(), true));
57+
lambda();
58+
}
59+
60+
TEST(IteratorTest, TestDistance) {
61+
auto lambda = [&] {
62+
repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position.
63+
repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position.
64+
std::string result;
65+
std::copy(first, last, std::back_inserter(result));
66+
CONSTEXPR_EXPECT_EQ(std::distance(first, last), 3);
67+
};
68+
69+
static_assert((lambda(), true));
70+
lambda();
71+
}
72+
73+
TEST(IteratorTest, TestNext) {
74+
auto lambda = [&] {
75+
repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position.
76+
repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position.
77+
CONSTEXPR_EXPECT_EQ(std::next(first, 3), last);
78+
};
79+
80+
static_assert((lambda(), true));
81+
lambda();
82+
}
83+
84+
TEST(IteratorTest, TestConcepts) {
85+
const auto test = [](auto&& it) {
86+
// The iterator type of it.
87+
using iterator = typename std::remove_reference_t<decltype(it)>;
88+
89+
// Check std::contiguous_iterator concept.
90+
// Note: Check each sub-concept to get the less verbose error message first!
91+
static_assert(std::input_iterator<iterator>);
92+
static_assert(std::forward_iterator<iterator>);
93+
static_assert(std::bidirectional_iterator<iterator>);
94+
static_assert(std::random_access_iterator<iterator>);
95+
};
96+
4097
repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position.
41-
repeated_chars_iterator last("foo", 3, 7); // Same as above, but now the iterator's position is 7.
42-
std::string result;
43-
std::copy(first, last, std::back_inserter(result));
44-
ASSERT_EQ(result, "foofoof");
98+
test(first);
4599
}
46100

47101
template <typename Pred>

0 commit comments

Comments
 (0)