From 4ec2eb4646a19d95e346a6191a69f884fc16e3b0 Mon Sep 17 00:00:00 2001 From: cobrapitz <12397702+cobrapitz@users.noreply.github.com> Date: Thu, 6 Apr 2023 14:49:03 +0200 Subject: [PATCH] added tests --- CMakeLists.txt | 29 +++++++++++++++++++++++------ cmake/CMakeSettings.cmake | 4 ++++ include/cb.h | 7 ++++++- include/cb/test/test.h | 10 +--------- include/cb/time/time.h | 13 +++++++++++-- include/cb/types/types.h | 7 ------- src/cb/time/time.cpp | 19 ++++++++++++++++--- tests/CMakeLists.txt | 29 +++++++++++++++++++++++++++++ tests/test_tests.cpp | 37 +++++++++++++++++++++++++++++++++++++ 9 files changed, 127 insertions(+), 28 deletions(-) create mode 100644 cmake/CMakeSettings.cmake create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ecf1863..38b130c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.24) ####################################################################################################################### project(cblib VERSION 1.0.0 DESCRIPTION "CB library") -set(CMAKE_CXX_STANDARD 17) +include(cmake/CMakeSettings.cmake) ####################################################################################################################### @@ -25,15 +25,32 @@ add_library(${PROJECT_NAME} STATIC # Buildling library ####################################################################################################################### -target_include_directories(${PROJECT_NAME} PUBLIC include) - +target_include_directories(${PROJECT_NAME} + PUBLIC + $ + $ + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src + ) ####################################################################################################################### # Target properties ####################################################################################################################### -set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) -set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR}) -set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER source/cb.h) +#set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) +#set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR}) +#set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER source/cb.h) + + + +####################################################################################################################### +# Tests +####################################################################################################################### +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + include(CTest) +endif() +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) + add_subdirectory(tests) +endif() \ No newline at end of file diff --git a/cmake/CMakeSettings.cmake b/cmake/CMakeSettings.cmake new file mode 100644 index 0000000..2ebb9e1 --- /dev/null +++ b/cmake/CMakeSettings.cmake @@ -0,0 +1,4 @@ +set(CMAKE_CXX_STANDARD 20) + +# Export compile command -> better tool integration. +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) \ No newline at end of file diff --git a/include/cb.h b/include/cb.h index 4a98d00..011e0a7 100644 --- a/include/cb.h +++ b/include/cb.h @@ -1,7 +1,12 @@ - +/** + * define INCLUDE_CB_DEFINES in order to include keyword mappings + */ #ifndef GODOT_HUB_MAP_CB_H #define GODOT_HUB_MAP_CB_H +#define assertm(exp, msg) = assert(((void)msg, exp)) + + #include "cb/test/test.h" #include "cb/time/time.h" #include "cb/types/types.h" diff --git a/include/cb/test/test.h b/include/cb/test/test.h index 5fbbe6f..4bd2ac0 100644 --- a/include/cb/test/test.h +++ b/include/cb/test/test.h @@ -2,17 +2,9 @@ #ifndef GODOT_HUB_MAP_TEST_H #define GODOT_HUB_MAP_TEST_H -#include -#include -#include -#include -#include -#include -#include -namespace test { - #define assertm(exp, msg) = assert(((void)msg, exp)) +namespace test { } diff --git a/include/cb/time/time.h b/include/cb/time/time.h index e6af430..69535a2 100644 --- a/include/cb/time/time.h +++ b/include/cb/time/time.h @@ -1,21 +1,30 @@ #ifndef GODOT_HUB_MAP_TIME_H #define GODOT_HUB_MAP_TIME_H -#include +#include #include #include + namespace cb::time { using internal_clock = std::chrono::high_resolution_clock; using time_point = std::chrono::time_point; + using time_point_map = std::map; - static std::map _name_to_measurement; + inline static time_point_map _name_to_measurement; + inline static constexpr long long NO_MEASUREMENT = -1; time_point chrono_now(); void start_measure(const std::string_view& id); + long long get_duration(const std::string_view &id); + void measure(const std::string_view& id); + + bool has_measurement(const std::string_view &id); + + const time_point_map& get_measurements(); } #endif //GODOT_HUB_MAP_TIME_H diff --git a/include/cb/types/types.h b/include/cb/types/types.h index 9a3c8cc..98f2d80 100644 --- a/include/cb/types/types.h +++ b/include/cb/types/types.h @@ -2,13 +2,6 @@ #define GODOT_HUB_MAP_TYPES_H #include -#include -#include -#include -#include -#include -#include - using i64 = int64_t; using u64 = uint64_t; diff --git a/src/cb/time/time.cpp b/src/cb/time/time.cpp index b65b6d6..a83bade 100644 --- a/src/cb/time/time.cpp +++ b/src/cb/time/time.cpp @@ -11,11 +11,24 @@ namespace cb::time { } void start_measure(const std::string_view &id) { - _name_to_measurement[id] = chrono_now(); + _name_to_measurement.insert({id, chrono_now()}); + } + + long long get_duration(const std::string_view &id) { + if (!has_measurement(id)) { + return -1; + } + return (chrono_now() - _name_to_measurement.at(id)).count(); + } + + bool has_measurement(const std::string_view &id) { + return _name_to_measurement.contains(id); } void measure(const std::string_view &id) { - const std::chrono::duration duration = chrono_now() - _name_to_measurement[id]; - std::cout << "measurement: " << id << " took: " << duration.count() << "\n"; + std::cout << "measurement: " << id << " took: " << get_duration(id) << "\n"; + } + const time_point_map &get_measurements() { + return _name_to_measurement; } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..94db483 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,29 @@ +set(TEST_PROJECT_NAME ${CMAKE_PROJECT_NAME}_test) + + +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.13.0 +) +FetchContent_MakeAvailable(googletest) + + +add_executable(${TEST_PROJECT_NAME} + test_tests.cpp + ) + +include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) + +target_include_directories(${TEST_PROJECT_NAME} + SYSTEM PRIVATE "${PROJECT_SOURCE_DIR}/tests" + PRIVATE "${PROJECT_SOURCE_DIR}/include" + ) + +target_link_libraries(${TEST_PROJECT_NAME} gtest_main cblib) + + +add_test(NAME Test.tests COMMAND ${TEST_PROJECT_NAME}) + diff --git a/tests/test_tests.cpp b/tests/test_tests.cpp new file mode 100644 index 0000000..da6db56 --- /dev/null +++ b/tests/test_tests.cpp @@ -0,0 +1,37 @@ +#include + +#include "gtest/gtest.h" + +TEST (Time, TestStart) { + using namespace cb; + + const auto mehligs = "mehligs"; + const auto mehl = "mehl"; + + time::start_measure(mehl); + + EXPECT_TRUE(time::has_measurement(mehl)); + EXPECT_FALSE(time::has_measurement(mehligs)); + + const auto measured_time_mehl = time::get_duration(mehl); + const auto measured_time_mehligs = time::get_duration(mehligs); + + EXPECT_TRUE(time::has_measurement(mehl)); + EXPECT_FALSE(time::has_measurement(mehligs)); + + EXPECT_GT(measured_time_mehl, 0); + EXPECT_EQ(measured_time_mehligs, time::NO_MEASUREMENT); +} + +TEST (Time, NoTimer) { + using namespace cb; + + const auto mehligs = "mehligs"; + const auto measured_time_mehligs = time::get_duration(mehligs); + + EXPECT_FALSE(time::has_measurement(mehligs)); + EXPECT_EQ(measured_time_mehligs, time::NO_MEASUREMENT); + +} + +