diff --git a/test/push-iterate-const-constexpr.cpp b/test/push-iterate-const-constexpr.cpp new file mode 100644 index 0000000..2deb805 --- /dev/null +++ b/test/push-iterate-const-constexpr.cpp @@ -0,0 +1,51 @@ +#include +#include + +template +constexpr bool array_equal(const std::array& a, const std::array& b) { + for (size_t i = 0; i < N; i++) { + if (a[i] != b[i]) return false; + } + + return true; +} + +constexpr bool test() { + ring_buffer buf; + const ring_buffer& const_buf = 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::array expected{3, 4, 5, 6}; + std::array actual; + int actual_index = 0; + for (const int& i : const_buf) { + actual[actual_index++] = i; + } + if (!array_equal(actual, expected)) return false; + } + + buf.push_back(7); + + { + std::array expected{4, 5, 6, 7}; + std::array actual; + int actual_index = 0; + for (const int& i : const_buf) { + actual[actual_index++] = i; + } + if (!array_equal(actual, expected)) return false; + } + + return true; +} + +static_assert(test()); + +int main() {} diff --git a/test/push-iterate-const.cpp b/test/push-iterate-const.cpp new file mode 100644 index 0000000..fd83c57 --- /dev/null +++ b/test/push-iterate-const.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +int main() { + ring_buffer buf; + const ring_buffer& const_buf = 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{3, 4, 5, 6}; + std::vector actual; + for (const int& i : const_buf) { + actual.push_back(i); + } + assert(actual == expected); + } + + buf.push_back(7); + + { + std::vector expected{4, 5, 6, 7}; + std::vector actual; + for (const int& i : const_buf) { + actual.push_back(i); + } + assert(actual == expected); + } +} diff --git a/test/push-iterate-constexpr.cpp b/test/push-iterate-constexpr.cpp new file mode 100644 index 0000000..16e1ccc --- /dev/null +++ b/test/push-iterate-constexpr.cpp @@ -0,0 +1,52 @@ +#include +#include + +template +constexpr bool array_equal(const std::array& a, const std::array& b) { + for (size_t i = 0; i < N; i++) { + if (a[i] != b[i]) return false; + } + + return true; +} + +constexpr bool test() { + 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::array expected{13, 14, 15, 16}; + std::array actual; + int actual_index = 0; + for (int& i : buf) { + i += 10; + actual[actual_index++] = i; + } + if (!array_equal(actual, expected)) return false; + } + + buf.push_back(7); + + { + std::array expected{24, 25, 26, 17}; + std::array actual; + int actual_index = 0; + for (int& i : buf) { + i += 10; + actual[actual_index++] = i; + } + if (!array_equal(actual, expected)) return false; + } + + return true; +} + +static_assert(test()); + +int main() {} diff --git a/test/push-iterate.cpp b/test/push-iterate.cpp new file mode 100644 index 0000000..71397d5 --- /dev/null +++ b/test/push-iterate.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +int main() { + 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); + } +} diff --git a/test/simple.cpp b/test/simple.cpp deleted file mode 100644 index 08f44ce..0000000 --- a/test/simple.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -int main() { - 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::cout << "before\n"; - for (int& i : buf) { - std::cout << i << "\n"; - } - - buf.push_back(7); - - std::cout << "after\n"; - for (int& i : buf) { - std::cout << i << "\n"; - } -}