From d0e3632b4357e45cd0bcf07a6722f1c32bd9f4ba Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 10 Mar 2024 22:24:11 -0400 Subject: [PATCH] Add/test pop_front(). --- include/bitcoin/system/data/collection.hpp | 7 ++++- .../bitcoin/system/impl/data/collection.ipp | 13 ++++++++ test/data/collection.cpp | 31 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/include/bitcoin/system/data/collection.hpp b/include/bitcoin/system/data/collection.hpp index 8bc5540fd3..5428b6cb4f 100644 --- a/include/bitcoin/system/data/collection.hpp +++ b/include/bitcoin/system/data/collection.hpp @@ -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::value_type inline pop(Collection& stack) NOEXCEPT; +/// Pop an element from the list (front) and return its value. +template +typename Collection::value_type +inline pop_front(Collection& stack) NOEXCEPT; + /// Determine if a collection contains only distinct members. template constexpr bool is_distinct(Collection&& list) NOEXCEPT; diff --git a/include/bitcoin/system/impl/data/collection.ipp b/include/bitcoin/system/impl/data/collection.ipp index 27fb313a43..de813d0758 100644 --- a/include/bitcoin/system/impl/data/collection.ipp +++ b/include/bitcoin/system/impl/data/collection.ipp @@ -159,6 +159,19 @@ inline pop(Collection& stack) NOEXCEPT return element; } +// Collection requires front and pop_front methods (list). +template +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 constexpr bool is_distinct(Collection&& list) NOEXCEPT diff --git a/test/data/collection.cpp b/test/data/collection.cpp index e397bc4e11..0233ccd679 100644 --- a/test/data/collection.cpp +++ b/test/data/collection.cpp @@ -18,6 +18,7 @@ */ #include "../test.hpp" #include +#include #include BOOST_AUTO_TEST_SUITE(collection_tests) @@ -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; + +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)