Skip to content

Commit

Permalink
Merge pull request libbitcoin#1416 from evoskuil/master
Browse files Browse the repository at this point in the history
Add/test pop_front().
  • Loading branch information
evoskuil authored Mar 11, 2024
2 parents 6437bcb + d0e3632 commit e1e8652
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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

0 comments on commit e1e8652

Please sign in to comment.