From 6389462ce338ae327ea2bdd1e9d42fa42e37ee1c Mon Sep 17 00:00:00 2001 From: Ferdinand Bachmann Date: Mon, 12 May 2025 16:02:30 +0200 Subject: [PATCH] test/non-swappable-ring-buffer: add test to confirm non-swappable containers can be used --- test/non-swappable-ring-buffer.cpp | 77 ++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/non-swappable-ring-buffer.cpp diff --git a/test/non-swappable-ring-buffer.cpp b/test/non-swappable-ring-buffer.cpp new file mode 100644 index 0000000..82eb18a --- /dev/null +++ b/test/non-swappable-ring-buffer.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +struct non_swappable_array { + using value_type = int; + using size_type = size_t; + int data[4]; + + // non-copyable + non_swappable_array() = default; + non_swappable_array(const non_swappable_array&) = delete; + non_swappable_array& operator=(const non_swappable_array&) = delete; + + size_t size() const { + return 4; + } + + int * begin() { + return std::begin(data); + } + + int * end() { + return std::end(data); + } + + const int * cbegin() const { + return std::cbegin(data); + } + + const int * cend() const { + return std::cend(data); + } + + int& operator[](size_t i) { + return data[i]; + } + + const int& operator[](size_t i) const { + return data[i]; + } + + // note: no swap implementation +}; + +int main() { + basic_ring_buffer buf; + + buf.push_back(1); + buf.push_back(2); + buf.push_back(3); + buf.push_back(4); + buf.push_back(5); + buf.push_back(6); + + { + std::vector expected{13, 14, 15, 16}; + std::vector actual; + for (int& i : buf) { + i += 10; + actual.push_back(i); + } + assert(actual == expected); + } + + buf.push_back(7); + + { + std::vector expected{24, 25, 26, 17}; + std::vector actual; + for (int& i : buf) { + i += 10; + actual.push_back(i); + } + assert(actual == expected); + } +}