add more tests

This commit is contained in:
Ferdinand Bachmann
2020-04-18 13:31:37 +02:00
parent 9f289b9242
commit b3afcef1b6
5 changed files with 174 additions and 25 deletions

View File

@ -0,0 +1,51 @@
#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;
const ring_buffer<int, 4>& 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<int, 4> expected{3, 4, 5, 6};
std::array<int, 4> 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<int, 4> expected{4, 5, 6, 7};
std::array<int, 4> 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() {}

View File

@ -0,0 +1,35 @@
#include <vector>
#include <cassert>
#include <ring-buffer.h>
int main() {
ring_buffer<int, 4> buf;
const ring_buffer<int, 4>& 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<int> expected{3, 4, 5, 6};
std::vector<int> actual;
for (const int& i : const_buf) {
actual.push_back(i);
}
assert(actual == expected);
}
buf.push_back(7);
{
std::vector<int> expected{4, 5, 6, 7};
std::vector<int> actual;
for (const int& i : const_buf) {
actual.push_back(i);
}
assert(actual == expected);
}
}

View 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() {}

36
test/push-iterate.cpp Normal file
View File

@ -0,0 +1,36 @@
#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);
{
std::vector<int> expected{13, 14, 15, 16};
std::vector<int> actual;
for (int& i : buf) {
i += 10;
actual.push_back(i);
}
assert(actual == expected);
}
buf.push_back(7);
{
std::vector<int> expected{24, 25, 26, 17};
std::vector<int> actual;
for (int& i : buf) {
i += 10;
actual.push_back(i);
}
assert(actual == expected);
}
}

View File

@ -1,25 +0,0 @@
#include <iostream>
#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);
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";
}
}