test: add additional tests around swapping and iterators

This commit is contained in:
2025-05-12 15:30:46 +02:00
parent ab1a477757
commit 010362d1f6
3 changed files with 101 additions and 0 deletions

24
test/iterator-ops.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <vector>
#include <cassert>
#include <ring-buffer.h>
int main() {
ring_buffer<int, 4> 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);
{
auto it = buf.begin();
++it;
it++;
assert(*it == 5);
--it;
it--;
assert(*it == 3);
}
}

View File

@ -0,0 +1,33 @@
#include <vector>
#include <cassert>
#include <ring-buffer.h>
#include <utility>
int main() {
ring_buffer<int, 4> buf1;
buf1.push_back(1);
buf1.push_back(2);
buf1.push_back(3);
buf1.push_back(4);
buf1.push_back(5);
buf1.push_back(6);
ring_buffer<int, 4> buf2;
buf2.push_back(10);
buf2.push_back(20);
buf2.push_back(30);
buf2.push_back(40);
buf2.push_back(50);
buf2.push_back(60);
auto it1 = buf1.begin();
auto it2 = buf2.begin();
std::swap(it1, it2);
{
assert(*it1 == 30);
assert(*it2 == 3);
}
}

44
test/swap-ring-buffer.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <vector>
#include <cassert>
#include <ring-buffer.h>
#include <utility>
int main() {
ring_buffer<int, 4> buf1;
buf1.push_back(1);
buf1.push_back(2);
buf1.push_back(3);
buf1.push_back(4);
buf1.push_back(5);
buf1.push_back(6);
ring_buffer<int, 4> buf2;
buf2.push_back(10);
buf2.push_back(20);
buf2.push_back(30);
buf2.push_back(40);
buf2.push_back(50);
buf2.push_back(60);
std::swap(buf1, buf2);
{
std::vector<int> expected{30, 40, 50, 60};
std::vector<int> actual;
for (int& i : buf1) {
actual.push_back(i);
}
assert(actual == expected);
}
{
std::vector<int> expected{3, 4, 5, 6};
std::vector<int> actual;
for (int& i : buf2) {
actual.push_back(i);
}
assert(actual == expected);
}
}