Skip to content

Commit d630b4d

Browse files
committed
tests: Add unit tests for repeated_chars_iterator
1 parent fed466b commit d630b4d

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

tests/beman/iterator_interface/iterator_interface.test.cpp

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ namespace {} // namespace
1616

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

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

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

46100
template <typename Pred>
@@ -83,6 +137,7 @@ struct filtered_int_iterator
83137
};
84138

85139
TEST(IteratorTest, TestFilteredIter) {
140+
86141
int a[] = {1, 2, 3, 4};
87142
filtered_int_iterator f{std::begin(a), std::end(a), [](int i) { return (i % 2) == 0; }};
88143
ASSERT_EQ(*f, 2);

0 commit comments

Comments
 (0)