fix ring-buffer for simple constexpr usage

This commit is contained in:
Ferdinand Bachmann
2020-04-18 13:46:17 +02:00
parent 43150587a8
commit 47f3737fb4
3 changed files with 14 additions and 6 deletions

View File

@ -2,6 +2,7 @@
#define _RING_BUFFER_H
#include <array>
#include <utility>
#include <ring-buffer-config.h>
#include <ring-buffer-iterator.h>
@ -20,6 +21,7 @@ private:
public:
basic_ring_buffer() = default;
~basic_ring_buffer() = default;
CONSTEXPR basic_ring_buffer(const Container& other) COND_NOEXCEPT(noexcept(Container(other))) : container(other) {}
basic_ring_buffer(const basic_ring_buffer& other) = default;
basic_ring_buffer(basic_ring_buffer&& other) = default;
basic_ring_buffer& operator=(const basic_ring_buffer& other) = default;

View File

@ -1,6 +1,7 @@
#include <array>
#include <ring-buffer.h>
#ifdef RING_BUFFER_CONSTEXPR
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++) {
@ -11,7 +12,8 @@ constexpr bool array_equal(const std::array<T, N>& a, const std::array<T, N>& b)
}
constexpr bool test() {
ring_buffer<int, 4> buf;
std::array<int, 4> init{0, 0, 0, 0};
ring_buffer<int, 4> buf(init);
const ring_buffer<int, 4>& const_buf = buf;
buf.push_back(1);
@ -23,7 +25,7 @@ constexpr bool test() {
{
std::array<int, 4> expected{3, 4, 5, 6};
std::array<int, 4> actual;
std::array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (const int& i : const_buf) {
actual[actual_index++] = i;
@ -35,7 +37,7 @@ constexpr bool test() {
{
std::array<int, 4> expected{4, 5, 6, 7};
std::array<int, 4> actual;
std::array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (const int& i : const_buf) {
actual[actual_index++] = i;
@ -47,5 +49,6 @@ constexpr bool test() {
}
static_assert(test());
#endif
int main() {}

View File

@ -1,6 +1,7 @@
#include <array>
#include <ring-buffer.h>
#ifdef RING_BUFFER_CONSTEXPR
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++) {
@ -11,7 +12,8 @@ constexpr bool array_equal(const std::array<T, N>& a, const std::array<T, N>& b)
}
constexpr bool test() {
ring_buffer<int, 4> buf;
std::array<int, 4> init{0, 0, 0, 0};
ring_buffer<int, 4> buf(init);
buf.push_back(1);
buf.push_back(2);
@ -22,7 +24,7 @@ constexpr bool test() {
{
std::array<int, 4> expected{13, 14, 15, 16};
std::array<int, 4> actual;
std::array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (int& i : buf) {
i += 10;
@ -35,7 +37,7 @@ constexpr bool test() {
{
std::array<int, 4> expected{24, 25, 26, 17};
std::array<int, 4> actual;
std::array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (int& i : buf) {
i += 10;
@ -48,5 +50,6 @@ constexpr bool test() {
}
static_assert(test());
#endif
int main() {}