forked from Ferdi265/cxx-ring-buffer
add more tests
This commit is contained in:
52
test/push-iterate-constexpr.cpp
Normal file
52
test/push-iterate-constexpr.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include <array>
|
||||
#include <ring-buffer.h>
|
||||
|
||||
template <typename T, size_t N>
|
||||
constexpr bool array_equal(const std::array<T, N>& a, const std::array<T, N>& b) {
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
if (a[i] != b[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
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);
|
||||
|
||||
{
|
||||
std::array<int, 4> expected{13, 14, 15, 16};
|
||||
std::array<int, 4> 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<int, 4> expected{24, 25, 26, 17};
|
||||
std::array<int, 4> 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() {}
|
Reference in New Issue
Block a user