Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/test pop_front(). #1416

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion include/bitcoin/system/data/collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ constexpr insert_sorted(Collection& list,
const typename Collection::value_type& element,
Predicate predicate) NOEXCEPT;

/// Pop an element from the stack and return its value.
/// Pop an element from the stack (back) and return its value.
template <typename Collection>
typename Collection::value_type
inline pop(Collection& stack) NOEXCEPT;

/// Pop an element from the list (front) and return its value.
template <typename Collection>
typename Collection::value_type
inline pop_front(Collection& stack) NOEXCEPT;

/// Determine if a collection contains only distinct members.
template <typename Collection>
constexpr bool is_distinct(Collection&& list) NOEXCEPT;
Expand Down
13 changes: 13 additions & 0 deletions include/bitcoin/system/impl/data/collection.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ inline pop(Collection& stack) NOEXCEPT
return element;
}

// Collection requires front and pop_front methods (list).
template <typename Collection>
typename Collection::value_type
inline pop_front(Collection& stack) NOEXCEPT
{
if (std::empty(stack))
return {};

typename Collection::value_type element{ std::move(stack.front()) };
stack.pop_front();
return element;
}

// C++17: Parallel policy for std::sort, std::unique.
template <typename Collection>
constexpr bool is_distinct(Collection&& list) NOEXCEPT
Expand Down
31 changes: 31 additions & 0 deletions test/data/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include "../test.hpp"
#include <array>
#include <list>
#include <vector>

BOOST_AUTO_TEST_SUITE(collection_tests)
Expand Down Expand Up @@ -189,6 +190,36 @@ BOOST_AUTO_TEST_CASE(collection__pop__multiple__popped_expected)
BOOST_REQUIRE_EQUAL(stack, expected_stack);
}

// pop_front
using data_queue = std::list<uint8_t>;

BOOST_AUTO_TEST_CASE(collection__pop_front__empty__empty_default)
{
data_queue queue{};
const auto value = pop_front(queue);
BOOST_REQUIRE(queue.empty());
BOOST_REQUIRE_EQUAL(value, 0u);
}

BOOST_AUTO_TEST_CASE(collection__pop_front__single__empty_expected)
{
const uint8_t expected = 42u;
data_queue queue{ expected };
const auto value = pop_front(queue);
BOOST_REQUIRE(queue.empty());
BOOST_REQUIRE_EQUAL(value, expected);
}

BOOST_AUTO_TEST_CASE(collection__pop_front__multiple__popped_expected)
{
const uint8_t expected_value = 42u;
data_queue queue{ expected_value, 0, 1, 2, 3 };
const data_queue expected_queue{ 0, 1, 2, 3 };
const auto value = pop_front(queue);
BOOST_REQUIRE_EQUAL(value, expected_value);
BOOST_REQUIRE(queue == expected_queue);
}

// is_distinct

BOOST_AUTO_TEST_CASE(collection__is_distinct__empty__true)
Expand Down