started move from test_args to test_params

This commit is contained in:
Patrick 2024-07-25 10:45:18 +02:00
parent 3b11ead3f5
commit 221eac7cd4
4 changed files with 136 additions and 12 deletions

View File

@ -124,6 +124,16 @@ struct quick_test_def;
inline std::ostream& operator<<(std::ostream& os, ReturnCode rc);
template<std::size_t N>
struct tstring {
char value[N];
constexpr tstring(const char (&str)[N]) { std::copy_n(str, N, value); }
constexpr operator const char *() { return value; }
};
struct ret_val_s {
const char *test_name = "";
ReturnCode val = ReturnCode::FAILED;
@ -143,6 +153,26 @@ struct ret_val {
constexpr inline const ret_val_s& operator[](std::size_t i) const { return vals[i]; }
};
struct empty_param {};
struct test_params {
template<tstring var_name>
constexpr static auto value = empty_param{};
};
template<typename T, std::size_t Nr = 0>
struct test_context_params {
using type = T;
constexpr static std::size_t nr = Nr;
template<tstring var_name>
constexpr static auto value = empty_param{};
};
#define CONTEXT_PARAM(type, name, ...) template<> template<> constexpr type test_context_params<__VA_ARGS__>::value< #name >
#define CONTEXT_PARAM_ARR(type, name, ...) template<> template<> constexpr type test_context_params<__VA_ARGS__>::value< #name >[]
class test_definition {
public:
[[nodiscard]] virtual constexpr ret_val_s evaluate() const = 0;
@ -366,7 +396,7 @@ template <std::size_t _N, typename ...Ts, std::size_t N = _N - 1>
constexpr auto _repeat_for_types_n(auto f) {
std::array rets = {
[&]<typename T, std::size_t ...Ns>(std::index_sequence<Ns...>) constexpr {
return std::array { f.template operator()<T, Ns+1>()... };
return std::array { f.template operator()<T, Ns, test_context_params<T, Ns>>()... };
}.template operator()<Ts>(std::make_index_sequence<N>())...
};
// Clion does not accept this as a constant expression

View File

@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.26)
set(CMAKE_CXX_STANDARD 23)
add_executable(test_const_vector_constructor const_vector_constructor.test.cpp
test_args.h)
test_args.h
test_params.h)
target_link_libraries(test_const_vector_constructor const_container test_common)
add_test(NAME "const_vector constructor" COMMAND test_const_vector_constructor)

View File

@ -3,6 +3,7 @@
#include <const_vector.hpp>
#include "test.hpp"
#include "test_params.h"
#include "test_args.h"
#include <vector>
@ -1156,8 +1157,8 @@ constexpr test_suite tests = define_tests("Tests")
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::assign(InputIt first, InputIt last)", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N>() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t n, typename params>() constexpr {
constexpr std::size_t N = n+1;
cc::const_vector<T, test_defs::get<T>::template arr_len<N>()> v1;
cc::const_vector<T, test_defs::get<T>::template il_len<N>()> v2;
cc::const_vector<T, test_defs::get<T>::template il_len<N>()> v3;
@ -1187,23 +1188,29 @@ constexpr test_suite tests = define_tests("Tests")
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::assign(std::initializer_list<value_type> values)", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N>() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N, typename params>() constexpr {
cc::const_vector<T, test_defs::get<T>::template il_len<N>()> v1;
cc::const_vector<T, test_defs::get<T>::template il_len<N>() - 1> v2;
cc::const_vector<T, (params::template value<"ilist">).size()> v1;
cc::const_vector<T, (params::template value<"ilist">).size() - 1> v2;
v1.assign(test_defs::get<T>::template i_list<N>);
v1.assign(params::template value<"ilist">);
ASSERT_TYPE_NUM_VEC_ARR_EQ(v1, test_defs::get<T>::template i_list<N>, T, N);
ASSERT_TYPE_NUM(v1.size() == test_defs::get<T>::template il_len<N>(), T, N);
ASSERT_TYPE_NUM_VEC_ARR_EQ(v1, params::template value<"ilist">, T, N);
ASSERT_TYPE_NUM(v1.size() == params::template value<"ilist">.size(), T, N);
ASSERT_TYPE_NUM_THROWS(v2.assign(test_defs::get<T>::template i_list<N>), std::invalid_argument, T, N);
ASSERT_TYPE_NUM_THROWS(v2.assign(params::template value<"ilist">), std::invalid_argument, T, N);
return TEST_PASS();
}), 2, int, char, const char *, TestStruct);
}), 2, int, char, const char *, TestObj);
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::at(size_type pos)", []() constexpr {
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL)
/*("const_vector", []() constexpr {

View File

@ -0,0 +1,86 @@
#ifndef CONST_CONTAINER_TEST_ARGS_H
#define CONST_CONTAINER_TEST_ARGS_H
#include "test.hpp"
#define GEN_TEST_OBJ_FROM_ARR(nr, i) TestObj(test_context_params<int, nr>::value<"arr">[i], test_context_params<char, nr>::value<"arr">[i], test_context_params<const char *, nr>::value<"arr">[i])
struct TestObj {
int x = 0;
char c = 0;
const char * s = "This is a string";
constexpr bool operator==(const TestObj &other) const { return x == other.x && c == other.c && std::string_view(s) == other.s; }
};
ADD_TYPE_HINT(TestObj);
template<> template<> constexpr int test_context_params<int, 0>::value<"arr">[] = { 1, 2, 3, 4 };
template<> template<> constexpr int test_context_params<int, 1>::value<"arr">[] = { 5, 6, 7, 8, 9, 10, 11, 12 };
template<> template<> constexpr char test_context_params<char, 0>::value<"arr">[] = { 'a', 'B', 'c', 'D' };
template<> template<> constexpr char test_context_params<char, 1>::value<"arr">[] = { 'e', 'F', 'g', 'H', 'i', 'J', '\n', '\0' };
template<> template<> constexpr const char * test_context_params<const char *, 0>::value<"arr">[] = { "Lorem", "ipsum", "dolor", "sit" };
template<> template<> constexpr const char * test_context_params<const char *, 1>::value<"arr">[] = { "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "\0" };
template<> template<> constexpr TestObj test_context_params<TestObj, 0>::value<"arr">[] = {
GEN_TEST_OBJ_FROM_ARR(0, 0),
GEN_TEST_OBJ_FROM_ARR(0, 1),
GEN_TEST_OBJ_FROM_ARR(0, 2),
GEN_TEST_OBJ_FROM_ARR(0, 3) };
template<> template<> constexpr TestObj test_context_params<TestObj, 1>::value<"arr">[] = {
GEN_TEST_OBJ_FROM_ARR(1, 0),
GEN_TEST_OBJ_FROM_ARR(1, 1),
GEN_TEST_OBJ_FROM_ARR(1, 2),
GEN_TEST_OBJ_FROM_ARR(1, 3),
GEN_TEST_OBJ_FROM_ARR(1, 4),
GEN_TEST_OBJ_FROM_ARR(1, 5),
GEN_TEST_OBJ_FROM_ARR(1, 6),
GEN_TEST_OBJ_FROM_ARR(1, 7) };
template<> template<> constexpr std::initializer_list<int> test_context_params<int, 0>::value<"ilist"> = { 1, 2, 3, 4 };
template<> template<> constexpr std::initializer_list<int> test_context_params<int, 1>::value<"ilist"> = { 5, 6, 7, 8, 9, 10, 11, 12 };
template<> template<> constexpr std::initializer_list<char> test_context_params<char, 0>::value<"ilist"> = { 'a', 'B', 'c', 'D' };
template<> template<> constexpr std::initializer_list<char> test_context_params<char, 1>::value<"ilist"> = { 'e', 'F', 'g', 'H', 'i', 'J', '\n', '\0' };
template<> template<> constexpr std::initializer_list<const char *> test_context_params<const char *, 0>::value<"ilist"> = { "Lorem", "ipsum", "dolor", "sit" };
template<> template<> constexpr std::initializer_list<const char *> test_context_params<const char *, 1>::value<"ilist"> = { "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "\0" };
template<> template<> constexpr std::initializer_list<TestObj> test_context_params<TestObj, 0>::value<"ilist"> = {
GEN_TEST_OBJ_FROM_ARR(0, 0),
GEN_TEST_OBJ_FROM_ARR(0, 1),
GEN_TEST_OBJ_FROM_ARR(0, 2),
GEN_TEST_OBJ_FROM_ARR(0, 3) };
template<> template<> constexpr std::initializer_list<TestObj> test_context_params<TestObj, 1>::value<"ilist"> = {
GEN_TEST_OBJ_FROM_ARR(1, 0),
GEN_TEST_OBJ_FROM_ARR(1, 1),
GEN_TEST_OBJ_FROM_ARR(1, 2),
GEN_TEST_OBJ_FROM_ARR(1, 3),
GEN_TEST_OBJ_FROM_ARR(1, 4),
GEN_TEST_OBJ_FROM_ARR(1, 5),
GEN_TEST_OBJ_FROM_ARR(1, 6),
GEN_TEST_OBJ_FROM_ARR(1, 7) };
template<> template<> constexpr std::size_t test_context_params<int, 1>::value<"capacity"> = 10;
template<> template<> constexpr std::size_t test_context_params<char, 1>::value<"capacity"> = 10;
template<> template<> constexpr std::size_t test_context_params<const char *, 1>::value<"capacity"> = 10;
template<> template<> constexpr std::size_t test_context_params<TestObj, 1>::value<"capacity"> = 10;
template<> template<> constexpr std::size_t test_context_params<int, 2>::value<"capacity"> = 200;
template<> template<> constexpr std::size_t test_context_params<char, 2>::value<"capacity"> = 200;
template<> template<> constexpr std::size_t test_context_params<const char *, 2>::value<"capacity"> = 200;
template<> template<> constexpr std::size_t test_context_params<TestObj, 2>::value<"capacity"> = 200;
template<> template<> constexpr std::size_t test_context_params<int, 0>::value<"size"> = 25;
template<> template<> constexpr std::size_t test_context_params<char, 0>::value<"size"> = 25;
template<> template<> constexpr std::size_t test_context_params<const char *, 0>::value<"size"> = 25;
template<> template<> constexpr std::size_t test_context_params<TestObj, 0>::value<"size"> = 25;
template<> template<> constexpr std::size_t test_context_params<int, 1>::value<"size"> = 50;
template<> template<> constexpr std::size_t test_context_params<char, 1>::value<"size"> = 50;
template<> template<> constexpr std::size_t test_context_params<const char *, 1>::value<"size"> = 50;
template<> template<> constexpr std::size_t test_context_params<TestObj, 1>::value<"size"> = 50;
template<> template<> constexpr int test_context_params<int, 0>::value<"value"> = 5;
template<> template<> constexpr int test_context_params<int, 1>::value<"value"> = INT_MIN;
template<> template<> constexpr char test_context_params<char, 0>::value<"value"> = 'P';
template<> template<> constexpr char test_context_params<char, 1>::value<"value"> = CHAR_MAX;
template<> template<> constexpr const char * test_context_params<const char *, 0>::value<"value"> = "Test string 1";
template<> template<> constexpr const char * test_context_params<const char *, 1>::value<"value"> = "Test string 2";
template<> template<> constexpr TestObj test_context_params<TestObj, 0>::value<"value"> = TestObj(5, 'P', "Object String 1");
template<> template<> constexpr TestObj test_context_params<TestObj, 1>::value<"value"> = TestObj(INT_MAX, 'p', "2 Object String");
#endif //CONST_CONTAINER_TEST_ARGS_H