From 33c2badc1ad818706977fca1ab84e13430feeac1 Mon Sep 17 00:00:00 2001 From: Adam Emerson Date: Wed, 9 Aug 2023 21:57:41 -0400 Subject: [PATCH] test/neorados: common_tests gets functions for bufferlists Mostly for getting things into and out of them conveniently. Signed-off-by: Adam Emerson --- src/test/neorados/common_tests.h | 81 ++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/test/neorados/common_tests.h b/src/test/neorados/common_tests.h index 1a33a8a57502e..7f429f27ebb02 100644 --- a/src/test/neorados/common_tests.h +++ b/src/test/neorados/common_tests.h @@ -14,13 +14,16 @@ #pragma once +#include #include #include #include #include #include +#include #include #include +#include #include #include @@ -364,3 +367,81 @@ public: /// \param test_name Name of the test #define CORO_TEST(test_suite_name, test_name) \ CORO_TEST_F(test_suite_name, test_name, CoroTest) + +/// \brief Generate buffer::list filled with repeating byte +/// +/// \param c Byte with which to fill +/// \param s Number of bites +/// +/// \return A buffer::list filled with `s` copies of `c` +inline auto filled_buffer_list(char c, std::size_t s) { + ceph::buffer::ptr bp{buffer::create(s)}; + std::memset(bp.c_str(), c, bp.length()); + ceph::buffer::list bl; + bl.push_back(std::move(bp)); + return bl; +}; + +/// \brief Create buffer::list with specified bytes +/// +/// \param cs Bytes the buffer::list should contain +/// +/// \return A buffer::list containing the bytes in `cs` +inline auto to_buffer_list(std::initializer_list cs) { + ceph::buffer::ptr bp{buffer::create(cs.size())}; + auto ci = cs.begin(); + for (auto i = 0; i < std::ssize(cs); ++i, ++ci) { + bp[i] = *ci; + } + ceph::buffer::list bl; + bl.push_back(std::move(bp)); + return bl; +}; + +/// \brief Create buffer::list with the content of a string_view +/// +/// \param s View with data to copy +/// +/// \return A buffer::list containing a copy of `s`. +inline auto to_buffer_list(std::string_view s) { + ceph::buffer::list bl; + bl.append(s); + return bl; +}; + +/// \brief Create buffer::list with the content of a span +/// +/// \param s Span with data to copy +/// +/// \return A buffer::list containing a copy of `s`. +inline auto to_buffer_list(std::span s) { + ceph::buffer::list bl; + bl.append(s.data(), s.size()); + return bl; +}; + +/// \brief Create buffer::list containing integer +/// +/// \param n Integer with which to fill the list +/// +/// \return A buffer::list containing the encoded `n` +inline auto to_buffer_list(std::integral auto n) { + ceph::buffer::list bl; + encode(n, bl); + return bl; +}; + +/// \brief Return value contained by buffer::list +/// +/// \param bl List with encoded value +/// +/// \return The value encoded in `bl`. +template +inline auto from_buffer_list(const ceph::buffer::list& bl) +{ + using ceph::decode; + T t; + auto bi = bl.begin(); + decode(t, bi); + return t; +} -- 2.39.5