add constexpr-array-shim to test constexpr on C++14

This commit is contained in:
Ferdinand Bachmann
2020-04-18 14:15:25 +02:00
parent 97c4bed1ec
commit fba15b1a72
4 changed files with 79 additions and 18 deletions

View File

@ -7,6 +7,7 @@ foreach(test-source ${test-sources})
list(APPEND test-targets ${test-target})
add_executable(${test-target} ${test-source})
target_include_directories(${test-target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${test-target} PRIVATE ring-buffer)
endforeach()
@ -27,7 +28,7 @@ foreach(test-directory ${test-directories})
endif()
add_executable(${test-target} ${test-directory-sources})
if(IS_DIRECTORY ${test-directory}/include)
target_include_directories(${test-target} PRIVATE ${test-directory}/include)
target_include_directories(${test-target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${test-directory}/include)
endif()
target_link_libraries(${test-target} PRIVATE ring-buffer)
endforeach()

View File

@ -0,0 +1,56 @@
#ifndef _CONSTEXPR_ARRAY_SHIM_H
#define _CONSTEXPR_ARRAY_SHIM_H
#include <array>
#if defined( __cpp_lib_array_constexpr) && __cpp_lib_array_constexpr >= 201606L
template <typename T, size_t N>
using constexpr_array = array;
#else
template <typename T, size_t N>
struct constexpr_array {
using size_type = size_t;
using value_type = T;
T data[N];
constexpr size_t size() const {
return N;
}
constexpr T* begin() {
return data;
}
constexpr T* end() {
return &data[N];
}
constexpr const T* begin() const {
return data;
}
constexpr const T* end() const {
return &data[N];
}
constexpr const T* cbegin() const {
return data;
}
constexpr const T* cend() const {
return &data[N];
}
constexpr T& operator[](size_t index) {
return data[index];
}
constexpr const T& operator[](size_t index) const {
return data[index];
}
};
#endif
#endif

View File

@ -2,8 +2,10 @@
#include <ring-buffer.h>
#ifdef RING_BUFFER_CONSTEXPR
#include "constexpr-array-shim.h"
template <typename T, size_t N>
constexpr bool array_equal(const std::array<T, N>& a, const std::array<T, N>& b) {
constexpr bool array_equal(const constexpr_array<T, N>& a, const constexpr_array<T, N>& b) {
for (size_t i = 0; i < N; i++) {
if (a[i] != b[i]) return false;
}
@ -12,9 +14,9 @@ constexpr bool array_equal(const std::array<T, N>& a, const std::array<T, N>& b)
}
constexpr bool test() {
std::array<int, 4> init{0, 0, 0, 0};
ring_buffer<int, 4> buf(init);
const ring_buffer<int, 4>& const_buf = buf;
constexpr_array<int, 4> init{0, 0, 0, 0};
basic_ring_buffer<constexpr_array<int, 4>> buf(init);
const basic_ring_buffer<constexpr_array<int, 4>>& const_buf = buf;
buf.push_back(1);
buf.push_back(2);
@ -24,8 +26,8 @@ constexpr bool test() {
buf.push_back(6);
{
std::array<int, 4> expected{3, 4, 5, 6};
std::array<int, 4> actual{0, 0, 0, 0};
constexpr_array<int, 4> expected{3, 4, 5, 6};
constexpr_array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (const int& i : const_buf) {
actual[actual_index++] = i;
@ -36,8 +38,8 @@ constexpr bool test() {
buf.push_back(7);
{
std::array<int, 4> expected{4, 5, 6, 7};
std::array<int, 4> actual{0, 0, 0, 0};
constexpr_array<int, 4> expected{4, 5, 6, 7};
constexpr_array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (const int& i : const_buf) {
actual[actual_index++] = i;
@ -48,7 +50,7 @@ constexpr bool test() {
return true;
}
static_assert(test());
static_assert(test(), "");
#endif
int main() {}

View File

@ -2,8 +2,10 @@
#include <ring-buffer.h>
#ifdef RING_BUFFER_CONSTEXPR
#include "constexpr-array-shim.h"
template <typename T, size_t N>
constexpr bool array_equal(const std::array<T, N>& a, const std::array<T, N>& b) {
constexpr bool array_equal(const constexpr_array<T, N>& a, const constexpr_array<T, N>& b) {
for (size_t i = 0; i < N; i++) {
if (a[i] != b[i]) return false;
}
@ -12,8 +14,8 @@ constexpr bool array_equal(const std::array<T, N>& a, const std::array<T, N>& b)
}
constexpr bool test() {
std::array<int, 4> init{0, 0, 0, 0};
ring_buffer<int, 4> buf(init);
constexpr_array<int, 4> init{0, 0, 0, 0};
basic_ring_buffer<constexpr_array<int, 4>> buf(init);
buf.push_back(1);
buf.push_back(2);
@ -23,8 +25,8 @@ constexpr bool test() {
buf.push_back(6);
{
std::array<int, 4> expected{13, 14, 15, 16};
std::array<int, 4> actual{0, 0, 0, 0};
constexpr_array<int, 4> expected{13, 14, 15, 16};
constexpr_array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (int& i : buf) {
i += 10;
@ -36,8 +38,8 @@ constexpr bool test() {
buf.push_back(7);
{
std::array<int, 4> expected{24, 25, 26, 17};
std::array<int, 4> actual{0, 0, 0, 0};
constexpr_array<int, 4> expected{24, 25, 26, 17};
constexpr_array<int, 4> actual{0, 0, 0, 0};
int actual_index = 0;
for (int& i : buf) {
i += 10;
@ -49,7 +51,7 @@ constexpr bool test() {
return true;
}
static_assert(test());
static_assert(test(), "");
#endif
int main() {}