Skip to content

Commit

Permalink
add string_collection iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Aposhian committed Dec 8, 2023
1 parent 4691652 commit 967ce5d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/mqtt/string_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ class string_collection
*
*/
char* const* c_arr() const { return (char* const *) cArr_.data(); }

using const_iterator = collection_type::const_iterator;

const_iterator begin() const { return coll_.begin(); }

const_iterator end() const { return coll_.end(); }
};

/////////////////////////////////////////////////////////////////////////////
Expand Down
79 changes: 79 additions & 0 deletions test/unit/test_string_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,85 @@ TEST_CASE("string_collection clear", "[collections]")
REQUIRE(size_t(0) == sc.size());
}


// ----------------------------------------------------------------------
// Test the const_iterator class
// ----------------------------------------------------------------------

TEST_CASE("string_collection for each", "[collections]")
{
string_collection sc(VEC);

size_t i = 0u;
for (const auto & string : sc) {
REQUIRE(sc[i] == sc[i]);
++i;
}
REQUIRE(i == sc.size());
}

TEST_CASE("string_collection iterator for with advance", "[collections]")
{
string_collection sc(VEC);

size_t i = 0u;
for (auto it = sc.begin(); it != sc.end(); std::advance(it, 1)) {
REQUIRE(*it == sc[i]);
++i;
}
REQUIRE(i == sc.size());
}

TEST_CASE("string_collection iterator for with pre-increment", "[collections]")
{
string_collection sc(VEC);

size_t i = 0u;
for (auto it = sc.begin(); it != sc.end(); ++it) {
REQUIRE(*it == sc[i]);
++i;
}
REQUIRE(i == sc.size());
}

TEST_CASE("string_collection iterator for with post increment", "[collections]")
{
string_collection sc(VEC);

size_t i = 0u;
for (auto it = sc.begin(); it != sc.end(); it++) {
REQUIRE(*it == sc[i]);
++i;
}
REQUIRE(i == sc.size());
}

TEST_CASE("string_collection begin", "[collections]")
{
string_collection sc(VEC);

REQUIRE(*(sc.begin()) == sc[0u]);
}

TEST_CASE("string_collection std copy", "[collections]")
{
string_collection sc(VEC);

std::vector<std::string> output;
std::copy(
sc.begin(),
sc.end(),
std::back_inserter(output)
);

REQUIRE(sc.size() == output.size());

for (size_t i = 0u; i < sc.size(); ++i) {
REQUIRE(sc[i] == output[i]);
}
}


/////////////////////////////////////////////////////////////////////////////
// name_value_collection
/////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 967ce5d

Please sign in to comment.