diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cc33ba..a877001 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.26) project(const_container) set(CMAKE_CXX_STANDARD 20) +set(CONST_CONTAINER_TEST ON) add_library(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include/const_vector.hpp" "${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") target_include_directories(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include") -add_executable(const_container_test test/test_main.cpp) -target_link_libraries(const_container_test PRIVATE const_container) -add_executable(const_list_test test/test_const_list.cpp) -target_link_libraries(const_list_test PRIVATE const_container) +if (DEFINED CONST_CONTAINER_TEST AND CONST_CONTAINER_TEST) + + include(CTest) + enable_testing() + + add_subdirectory(test) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..022643b --- /dev/null +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/common_helper/test_ret_val.h b/test/common_helper/test_ret_val.h new file mode 100644 index 0000000..7764a93 --- /dev/null +++ b/test/common_helper/test_ret_val.h @@ -0,0 +1,41 @@ + +#ifndef CONST_CONTAINER_TEST_RETURN_VAL_H_ +#define CONST_CONTAINER_TEST_RETURN_VAL_H_ + +#include + +#include + +#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 { + ReturnCode val = FAILED; + const char *msg = ""; + }; + + cc::const_list 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(e.val); }); } +};*/ + +struct ret_val_s { + const char *test_name = ""; + ReturnCode val = FAILED; + const char *msg = ""; +}; + +template +using ret_val = std::array; + +#endif //CONST_CONTAINER_TEST_RETURN_VAL_H_ diff --git a/test/common_helper/test_util.hpp b/test/common_helper/test_util.hpp new file mode 100644 index 0000000..897810e --- /dev/null +++ b/test/common_helper/test_util.hpp @@ -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 +#include +#include + +#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 +consteval auto consteval_caller(F &func) { + return func(); +} + +template +constexpr auto run_tests(TestFuncs... tests) { + constexpr std::size_t num_tests = sizeof...(tests); + + std::array test_arr = { tests... }; + + ret_val ret; + for (std::size_t i = 0; i < test_arr.size(); ++i) { + ret[i] = test_arr[i](); + } + + return ret; +}; + +template +void report(const ret_val& 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_ diff --git a/test/const_vector/CMakeLists.txt b/test/const_vector/CMakeLists.txt new file mode 100644 index 0000000..67ab7c5 --- /dev/null +++ b/test/const_vector/CMakeLists.txt @@ -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) diff --git a/test/const_vector/const_vector_constructor.test.cpp b/test/const_vector/const_vector_constructor.test.cpp new file mode 100644 index 0000000..8cdc09d --- /dev/null +++ b/test/const_vector/const_vector_constructor.test.cpp @@ -0,0 +1,27 @@ +#include +#include + +#include + +#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; +} \ No newline at end of file diff --git a/test/const_vector/test_args.h b/test/const_vector/test_args.h new file mode 100644 index 0000000..ee2a433 --- /dev/null +++ b/test/const_vector/test_args.h @@ -0,0 +1,149 @@ +#ifndef UDIFF_TEST_ARGS_H_ +#define UDIFF_TEST_ARGS_H_ + +#include +#include + +#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 VI_I_LIST1 = VI_TEST_ARR1; + static constexpr std::initializer_list 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 VC_I_LIST1 = VC_TEST_ARR1; + static constexpr std::initializer_list 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 VS_I_LIST1 = VS_TEST_ARR1; + static constexpr std::initializer_list 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 VO_I_LIST1 = VO_TEST_ARR1; + static constexpr std::initializer_list VO_I_LIST2 = VO_TEST_ARR2; + + template + struct get { + template + static consteval auto& i_list(); + + template + static consteval auto & arr(); + + template + static consteval std::size_t arr_len(); + + template + static consteval std::size_t capacity(); + + }; + +}; + +template +template +consteval auto & test_defs::get::i_list() { return nullptr; } +template +template +consteval auto & test_defs::get::arr() { return nullptr; } +template +template +consteval std::size_t test_defs::get::arr_len() { return 0; } +template +template +consteval std::size_t test_defs::get::capacity() { return 0; } + + +template<> template<> consteval auto & test_defs::get::i_list<1>() { return VI_I_LIST1; } +template<> template<> consteval auto & test_defs::get::i_list<2>() { return VI_I_LIST2; } +template<> template<> consteval auto & test_defs::get::i_list<1>() { return VC_I_LIST1; } +template<> template<> consteval auto & test_defs::get::i_list<2>() { return VC_I_LIST2; } +template<> template<> consteval auto & test_defs::get::i_list<1>() { return VS_I_LIST1; } +template<> template<> consteval auto & test_defs::get::i_list<2>() { return VS_I_LIST2; } +template<> template<> consteval auto & test_defs::get::i_list<1>() { return VO_I_LIST1; } +template<> template<> consteval auto & test_defs::get::i_list<2>() { return VO_I_LIST2; } + +template<> template<> consteval auto & test_defs::get::arr<1>() { return C_VI_TEST_ARR1; } +template<> template<> consteval auto & test_defs::get::arr<2>() { return C_VI_TEST_ARR2; } +template<> template<> consteval auto & test_defs::get::arr<1>() { return C_VC_TEST_ARR1; } +template<> template<> consteval auto & test_defs::get::arr<2>() { return C_VC_TEST_ARR2; } +template<> template<> consteval auto & test_defs::get::arr<1>() { return C_VS_TEST_ARR1; } +template<> template<> consteval auto & test_defs::get::arr<2>() { return C_VS_TEST_ARR2; } +template<> template<> consteval auto & test_defs::get::arr<1>() { return C_VO_TEST_ARR1; } +template<> template<> consteval auto & test_defs::get::arr<2>() { return C_VO_TEST_ARR2; } + +template<> template<> consteval std::size_t test_defs::get::arr_len<1>() { return VI_TEST_ARR1_LEN; } +template<> template<> consteval std::size_t test_defs::get::arr_len<2>() { return VI_TEST_ARR2_LEN; } +template<> template<> consteval std::size_t test_defs::get::arr_len<1>() { return VC_TEST_ARR1_LEN; } +template<> template<> consteval std::size_t test_defs::get::arr_len<2>() { return VC_TEST_ARR2_LEN; } +template<> template<> consteval std::size_t test_defs::get::arr_len<1>() { return VS_TEST_ARR1_LEN; } +template<> template<> consteval std::size_t test_defs::get::arr_len<2>() { return VS_TEST_ARR2_LEN; } +template<> template<> consteval std::size_t test_defs::get::arr_len<1>() { return VO_TEST_ARR1_LEN; } +template<> template<> consteval std::size_t test_defs::get::arr_len<2>() { return VO_TEST_ARR2_LEN; } + +template<> template<> consteval std::size_t test_defs::get::capacity<1>() { return TEST_VEC1_CAPACITY; } +template<> template<> consteval std::size_t test_defs::get::capacity<2>() { return TEST_VEC2_CAPACITY; } +template<> template<> consteval std::size_t test_defs::get::capacity<1>() { return TEST_VEC1_CAPACITY; } +template<> template<> consteval std::size_t test_defs::get::capacity<2>() { return TEST_VEC2_CAPACITY; } +template<> template<> consteval std::size_t test_defs::get::capacity<1>() { return TEST_VEC1_CAPACITY; } +template<> template<> consteval std::size_t test_defs::get::capacity<2>() { return TEST_VEC2_CAPACITY; } +template<> template<> consteval std::size_t test_defs::get::capacity<1>() { return TEST_VEC1_CAPACITY; } +template<> template<> consteval std::size_t test_defs::get::capacity<2>() { return TEST_VEC2_CAPACITY; } + + +#endif //UDIFF_TEST_ARGS_H_