refactored testing for const_list

This commit is contained in:
cyborg1811m 2024-03-25 16:39:19 +01:00
parent 7fa0ae1257
commit ea7f2776a4
7 changed files with 294 additions and 4 deletions

View File

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.26)
project(const_container) project(const_container)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CONST_CONTAINER_TEST ON)
add_library(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include/const_vector.hpp" add_library(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include/const_vector.hpp"
"${CMAKE_CURRENT_LIST_DIR}/include/helper.h" "${CMAKE_CURRENT_LIST_DIR}/include/helper.h"
@ -10,7 +11,10 @@ add_library(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include/const_v
"${CMAKE_CURRENT_LIST_DIR}/include/CompileOptional.h") "${CMAKE_CURRENT_LIST_DIR}/include/CompileOptional.h")
target_include_directories(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include") target_include_directories(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include")
add_executable(const_container_test test/test_main.cpp) if (DEFINED CONST_CONTAINER_TEST AND CONST_CONTAINER_TEST)
target_link_libraries(const_container_test PRIVATE const_container)
add_executable(const_list_test test/test_const_list.cpp) include(CTest)
target_link_libraries(const_list_test PRIVATE const_container) enable_testing()
add_subdirectory(test)
endif()

7
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.26)
add_library(test_common INTERFACE)
target_include_directories(test_common INTERFACE common_helper)
target_link_libraries(test_common INTERFACE const_container)
add_subdirectory(const_vector)

View File

@ -0,0 +1,41 @@
#ifndef CONST_CONTAINER_TEST_RETURN_VAL_H_
#define CONST_CONTAINER_TEST_RETURN_VAL_H_
#include <concepts>
#include <const_list.h>
#define TEST_FAIL(name, msg) ret_val_s { name, ReturnCode::FAILED, msg }
#define TEST_PASS(name, msg) ret_val_s { name, ReturnCode::PASSED, msg }
enum ReturnCode { FAILED = -1, PASSED = 0 };
/*struct test_ret_val {
struct ret_val_s : public cc::const_list_node<ret_val_s> {
ReturnCode val = FAILED;
const char *msg = "";
};
cc::const_list<ret_val_s> list;
constexpr test_ret_val() = default;
constexpr test_ret_val(const test_ret_val& other) = delete;
constexpr test_ret_val(test_ret_val&& other) = default;
constexpr test_ret_val& operator=(const test_ret_val& other) = delete;
constexpr test_ret_val& operator=(test_ret_val&& other) = default;
constexpr operator bool() const { return std::all_of(list.cbegin(), list.cend(), [](auto e){ return !static_cast<bool>(e.val); }); }
};*/
struct ret_val_s {
const char *test_name = "";
ReturnCode val = FAILED;
const char *msg = "";
};
template<std::size_t Nr>
using ret_val = std::array<ret_val_s, Nr>;
#endif //CONST_CONTAINER_TEST_RETURN_VAL_H_

View File

@ -0,0 +1,54 @@
//
// Created by Patrick Maschek on 14/03/2024.
//
#ifndef CONST_CONTAINER_TEST_UTIL_H_
#define CONST_CONTAINER_TEST_UTIL_H_
#include <concepts>
#include <iostream>
#include <string_view>
#include "test_ret_val.h"
#define TEST(ret_val, ret_code, msg_success, msg_fail)
struct TestStruct {
int x = 0;
char c = 0;
const char * s = "This is a string";
constexpr bool operator==(const TestStruct &other) const { return x == other.x && c == other.c && std::string_view(s) == other.s; }
};
template<std::invocable F>
consteval auto consteval_caller(F &func) {
return func();
}
template<std::invocable ...TestFuncs>
constexpr auto run_tests(TestFuncs... tests) {
constexpr std::size_t num_tests = sizeof...(tests);
std::array test_arr = { tests... };
ret_val<num_tests> ret;
for (std::size_t i = 0; i < test_arr.size(); ++i) {
ret[i] = test_arr[i]();
}
return ret;
};
template<std::size_t Nr>
void report(const ret_val<Nr>& ret_val) {
for (std::size_t i = 0; i < ret_val.size(); ++i) {
std::cout << "Result of Test \"" << ret_val[i].test_name << "\" (number: " << i << "): "
<< (ret_val[i].val == ReturnCode::PASSED ? "PASSED" : "FAILED") << "\n"
<< "\t" << ret_val[i].msg << "\n";
}
}
#endif //CONST_CONTAINER_TEST_UTIL_H_

View File

@ -0,0 +1,8 @@
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)
target_link_libraries(test_const_vector_constructor const_container test_common)
add_test(NAME "const_vector constructor" COMMAND test_const_vector_constructor)

View File

@ -0,0 +1,27 @@
#include <any>
#include <iostream>
#include <const_vector.hpp>
#include "test_ret_val.h"
constexpr auto test_func() {
auto ret_val = run_tests([](){
return TEST_PASS("Test1", "PASS");
});
return ret_val;
}
int main() {
auto ret = test_func();
auto cret = consteval_caller();
report(ret);
return 0;
}

View File

@ -0,0 +1,149 @@
#ifndef UDIFF_TEST_ARGS_H_
#define UDIFF_TEST_ARGS_H_
#include <array>
#include <initializer_list>
#include "test/common_helper/test_util.h"
struct test_defs {
#define GEN_TEST_STRUCT_ARR(nr, i) TestStruct(C_VI_TEST_ARR##nr[i], C_VC_TEST_ARR##nr[i], C_VS_TEST_ARR##nr[i])
#define TEST_VEC1_CAPACITY 5
#define TEST_VEC2_CAPACITY 10
#define TEST_VEC1_SIZE 3
#define TEST_VEC2_SIZE 7
#define VI_TEST_VAL 5
#define VC_TEST_VAL 'T'
#define VS_TEST_VAL "Test string"
#define VO_TEST_VAL TestStruct(VI_TEST_VAL, VC_TEST_VAL, VS_TEST_VAL)
#define VI_TEST_ARR1 { 1, 2, 3, 4 }
#define VI_TEST_ARR2 { 5, 6, 7, 8, 9, 10, 11, 12 }
#define VC_TEST_ARR1 { 'a', 'B', 'c', 'D' }
#define VC_TEST_ARR2 { 'e', 'F', 'g', 'H', 'i', 'J', '\n', '\0' }
#define VS_TEST_ARR1 { "Lorem", "ipsum", "dolor", "sit" }
#define VS_TEST_ARR2 { "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "\0" }
#define VO_TEST_ARR1 { GEN_TEST_STRUCT_ARR(1, 0), \
GEN_TEST_STRUCT_ARR(1, 1), \
GEN_TEST_STRUCT_ARR(1, 2), \
GEN_TEST_STRUCT_ARR(1, 3) }
#define VO_TEST_ARR2 { GEN_TEST_STRUCT_ARR(2, 0), \
GEN_TEST_STRUCT_ARR(2, 1), \
GEN_TEST_STRUCT_ARR(2, 2), \
GEN_TEST_STRUCT_ARR(2, 3), \
GEN_TEST_STRUCT_ARR(2, 4), \
GEN_TEST_STRUCT_ARR(2, 5), \
GEN_TEST_STRUCT_ARR(2, 6), \
GEN_TEST_STRUCT_ARR(2, 7) }
static constexpr std::initializer_list<int> VI_I_LIST1 = VI_TEST_ARR1;
static constexpr std::initializer_list<int> VI_I_LIST2 = VI_TEST_ARR2;
static constexpr const int C_VI_TEST_ARR1[] = VI_TEST_ARR1;
static constexpr const int C_VI_TEST_ARR2[] = VI_TEST_ARR2;
static constexpr const std::array STD_VI_TEST_ARR1 = VI_TEST_ARR1;
static constexpr const std::array STD_VI_TEST_ARR2 = VI_TEST_ARR2;
static constexpr const std::size_t VI_TEST_ARR1_LEN = C_ARR_LEN(C_VI_TEST_ARR1);
static constexpr const std::size_t VI_TEST_ARR2_LEN = C_ARR_LEN(C_VI_TEST_ARR2);
static constexpr std::initializer_list<char> VC_I_LIST1 = VC_TEST_ARR1;
static constexpr std::initializer_list<char> VC_I_LIST2 = VC_TEST_ARR2;
static constexpr const char C_VC_TEST_ARR1[] = VC_TEST_ARR1;
static constexpr const char C_VC_TEST_ARR2[] = VC_TEST_ARR2;
static constexpr const std::array STD_VC_TEST_ARR1 = VC_TEST_ARR1;
static constexpr const std::array STD_VC_TEST_ARR2 = VC_TEST_ARR2;
static constexpr const std::size_t VC_TEST_ARR1_LEN = C_ARR_LEN(C_VC_TEST_ARR1);
static constexpr const std::size_t VC_TEST_ARR2_LEN = C_ARR_LEN(C_VC_TEST_ARR2);
static constexpr std::initializer_list<const char *> VS_I_LIST1 = VS_TEST_ARR1;
static constexpr std::initializer_list<const char *> VS_I_LIST2 = VS_TEST_ARR2;
static constexpr const char * C_VS_TEST_ARR1[] = VS_TEST_ARR1;
static constexpr const char * C_VS_TEST_ARR2[] = VS_TEST_ARR2;
static constexpr const std::array STD_VS_TEST_ARR1 = VS_TEST_ARR1;
static constexpr const std::array STD_VS_TEST_ARR2 = VS_TEST_ARR2;
static constexpr const std::size_t VS_TEST_ARR1_LEN = C_ARR_LEN(C_VS_TEST_ARR1);
static constexpr const std::size_t VS_TEST_ARR2_LEN = C_ARR_LEN(C_VS_TEST_ARR2);
static constexpr TestStruct C_VO_TEST_ARR1[] = VO_TEST_ARR1;
static constexpr TestStruct C_VO_TEST_ARR2[] = VO_TEST_ARR2;
static constexpr const std::array STD_VO_TEST_ARR1 = VO_TEST_ARR1;
static constexpr const std::array STD_VO_TEST_ARR2 = VO_TEST_ARR2;
static constexpr const std::size_t VO_TEST_ARR1_LEN = C_ARR_LEN(C_VO_TEST_ARR1);
static constexpr const std::size_t VO_TEST_ARR2_LEN = C_ARR_LEN(C_VO_TEST_ARR2);
static constexpr std::initializer_list<TestStruct> VO_I_LIST1 = VO_TEST_ARR1;
static constexpr std::initializer_list<TestStruct> VO_I_LIST2 = VO_TEST_ARR2;
template<typename T>
struct get {
template<std::size_t nr>
static consteval auto& i_list();
template<std::size_t nr>
static consteval auto & arr();
template<std::size_t nr>
static consteval std::size_t arr_len();
template<std::size_t nr>
static consteval std::size_t capacity();
};
};
template<typename T>
template<std::size_t nr>
consteval auto & test_defs::get<T>::i_list() { return nullptr; }
template<typename T>
template<std::size_t nr>
consteval auto & test_defs::get<T>::arr() { return nullptr; }
template<typename T>
template<std::size_t nr>
consteval std::size_t test_defs::get<T>::arr_len() { return 0; }
template<typename T>
template<std::size_t nr>
consteval std::size_t test_defs::get<T>::capacity() { return 0; }
template<> template<> consteval auto & test_defs::get<int>::i_list<1>() { return VI_I_LIST1; }
template<> template<> consteval auto & test_defs::get<int>::i_list<2>() { return VI_I_LIST2; }
template<> template<> consteval auto & test_defs::get<char>::i_list<1>() { return VC_I_LIST1; }
template<> template<> consteval auto & test_defs::get<char>::i_list<2>() { return VC_I_LIST2; }
template<> template<> consteval auto & test_defs::get<const char *>::i_list<1>() { return VS_I_LIST1; }
template<> template<> consteval auto & test_defs::get<const char *>::i_list<2>() { return VS_I_LIST2; }
template<> template<> consteval auto & test_defs::get<TestStruct>::i_list<1>() { return VO_I_LIST1; }
template<> template<> consteval auto & test_defs::get<TestStruct>::i_list<2>() { return VO_I_LIST2; }
template<> template<> consteval auto & test_defs::get<int>::arr<1>() { return C_VI_TEST_ARR1; }
template<> template<> consteval auto & test_defs::get<int>::arr<2>() { return C_VI_TEST_ARR2; }
template<> template<> consteval auto & test_defs::get<char>::arr<1>() { return C_VC_TEST_ARR1; }
template<> template<> consteval auto & test_defs::get<char>::arr<2>() { return C_VC_TEST_ARR2; }
template<> template<> consteval auto & test_defs::get<const char*>::arr<1>() { return C_VS_TEST_ARR1; }
template<> template<> consteval auto & test_defs::get<const char*>::arr<2>() { return C_VS_TEST_ARR2; }
template<> template<> consteval auto & test_defs::get<TestStruct>::arr<1>() { return C_VO_TEST_ARR1; }
template<> template<> consteval auto & test_defs::get<TestStruct>::arr<2>() { return C_VO_TEST_ARR2; }
template<> template<> consteval std::size_t test_defs::get<int>::arr_len<1>() { return VI_TEST_ARR1_LEN; }
template<> template<> consteval std::size_t test_defs::get<int>::arr_len<2>() { return VI_TEST_ARR2_LEN; }
template<> template<> consteval std::size_t test_defs::get<char>::arr_len<1>() { return VC_TEST_ARR1_LEN; }
template<> template<> consteval std::size_t test_defs::get<char>::arr_len<2>() { return VC_TEST_ARR2_LEN; }
template<> template<> consteval std::size_t test_defs::get<const char *>::arr_len<1>() { return VS_TEST_ARR1_LEN; }
template<> template<> consteval std::size_t test_defs::get<const char *>::arr_len<2>() { return VS_TEST_ARR2_LEN; }
template<> template<> consteval std::size_t test_defs::get<TestStruct>::arr_len<1>() { return VO_TEST_ARR1_LEN; }
template<> template<> consteval std::size_t test_defs::get<TestStruct>::arr_len<2>() { return VO_TEST_ARR2_LEN; }
template<> template<> consteval std::size_t test_defs::get<int>::capacity<1>() { return TEST_VEC1_CAPACITY; }
template<> template<> consteval std::size_t test_defs::get<int>::capacity<2>() { return TEST_VEC2_CAPACITY; }
template<> template<> consteval std::size_t test_defs::get<char>::capacity<1>() { return TEST_VEC1_CAPACITY; }
template<> template<> consteval std::size_t test_defs::get<char>::capacity<2>() { return TEST_VEC2_CAPACITY; }
template<> template<> consteval std::size_t test_defs::get<const char *>::capacity<1>() { return TEST_VEC1_CAPACITY; }
template<> template<> consteval std::size_t test_defs::get<const char *>::capacity<2>() { return TEST_VEC2_CAPACITY; }
template<> template<> consteval std::size_t test_defs::get<TestStruct>::capacity<1>() { return TEST_VEC1_CAPACITY; }
template<> template<> consteval std::size_t test_defs::get<TestStruct>::capacity<2>() { return TEST_VEC2_CAPACITY; }
#endif //UDIFF_TEST_ARGS_H_