From 573a224292bc6c29b991634f94eb66704b55c83d Mon Sep 17 00:00:00 2001 From: Julien Hecht Date: Wed, 2 Sep 2026 15:55:24 +0200 Subject: [PATCH] Split test clocks from test_chrono_clocks.cpp to clocks_for_unit_tests.cpp This makes clocks available for other tests when needed. --- test/CMakeLists.txt | 1 + test/clocks_for_unit_tests.cpp | 59 +++++++++++++++++++++++++++++++++ test/clocks_for_unit_tests.h | 54 ++++++++++++++++++++++++++++++ test/meson.build | 1 + test/test_chrono_clocks.cpp | 29 ++-------------- test/vs2022/etl.vcxproj | 2 ++ test/vs2022/etl.vcxproj.filters | 6 ++++ 7 files changed, 125 insertions(+), 27 deletions(-) create mode 100644 test/clocks_for_unit_tests.cpp create mode 100644 test/clocks_for_unit_tests.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fbbf091e..84fcfbcd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,6 +13,7 @@ project(etl_unit_tests LANGUAGES CXX) add_executable(etl_tests main.cpp murmurhash3.cpp + clocks_for_unit_tests.cpp test_algorithm.cpp test_algorithm_ranges.cpp test_alignment.cpp diff --git a/test/clocks_for_unit_tests.cpp b/test/clocks_for_unit_tests.cpp new file mode 100644 index 00000000..157ef3a4 --- /dev/null +++ b/test/clocks_for_unit_tests.cpp @@ -0,0 +1,59 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2026 Julien Hecht + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "clocks_for_unit_tests.h" +#include "etl/atomic.h" +#include "etl/chrono.h" + +namespace test +{ + namespace clocks + { + CLOCK_TYPE nanoseconds{0}; + CLOCK_TYPE milliseconds{0}; + CLOCK_TYPE seconds{0}; + } // namespace clocks +} // namespace test + +extern "C" +{ + etl::chrono::system_clock::rep etl_get_system_clock() + { + return etl::chrono::system_clock::rep(test::clocks::milliseconds); + } + + etl::chrono::high_resolution_clock::rep etl_get_high_resolution_clock() + { + return etl::chrono::high_resolution_clock::rep(test::clocks::nanoseconds); + } + + etl::chrono::steady_clock::rep etl_get_steady_clock() + { + return etl::chrono::steady_clock::rep(test::clocks::seconds); + } +} diff --git a/test/clocks_for_unit_tests.h b/test/clocks_for_unit_tests.h new file mode 100644 index 00000000..b2630a4e --- /dev/null +++ b/test/clocks_for_unit_tests.h @@ -0,0 +1,54 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2026 Julien Hecht + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_TEST_CLOCKS_INCLUDED +#define ETL_TEST_CLOCKS_INCLUDED + +#include "etl/platform.h" +#include "etl/atomic.h" + +#if defined(ETL_HAS_ATOMIC) && ETL_HAS_ATOMIC == 1 + // In general it is more correct to use atomics to keep this value since it + // may be used and modified across multiple threads (see test_semaphore.cpp). + #define CLOCK_TYPE etl::atomic +#else + // However, in practice, a data race here isn't really an issue so int is fine too. + #define CLOCK_TYPE int +#endif + +namespace test +{ + namespace clocks + { + extern CLOCK_TYPE nanoseconds; + extern CLOCK_TYPE milliseconds; + extern CLOCK_TYPE seconds; + } // namespace clocks +} // namespace test + +#endif diff --git a/test/meson.build b/test/meson.build index add9b29b..7aa82aa7 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,5 +1,6 @@ etl_test_sources = files( 'main.cpp', + 'clocks_for_unit_tests.cpp', 'murmurhash3.cpp', 'test_algorithm.cpp', 'test_algorithm_ranges.cpp', diff --git a/test/test_chrono_clocks.cpp b/test/test_chrono_clocks.cpp index 6fefc751..982332d5 100644 --- a/test/test_chrono_clocks.cpp +++ b/test/test_chrono_clocks.cpp @@ -30,39 +30,14 @@ SOFTWARE. #include "etl/platform.h" +#include "clocks_for_unit_tests.h" #include "unit_test_framework.h" #include "etl/chrono.h" #include -//***************************************************************************** -// Global clock functions. -//***************************************************************************** -namespace -{ - int nanoseconds = 0; - int milliseconds = 0; - int seconds = 0; -} // namespace - -extern "C" -{ - etl::chrono::system_clock::rep etl_get_system_clock() - { - return etl::chrono::system_clock::rep(milliseconds); - } - - etl::chrono::high_resolution_clock::rep etl_get_high_resolution_clock() - { - return etl::chrono::high_resolution_clock::rep(nanoseconds); - } - - etl::chrono::steady_clock::rep etl_get_steady_clock() - { - return etl::chrono::steady_clock::rep(seconds); - } -} +using namespace test::clocks; namespace { diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index 4ea07f78..7923fc41 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -4047,6 +4047,7 @@ + @@ -4083,6 +4084,7 @@ + true true diff --git a/test/vs2022/etl.vcxproj.filters b/test/vs2022/etl.vcxproj.filters index 80dc8585..b74f6e1c 100644 --- a/test/vs2022/etl.vcxproj.filters +++ b/test/vs2022/etl.vcxproj.filters @@ -1101,6 +1101,12 @@ Tests\Test Support + + Tests\Test Support + + + Tests\Test Support + Tests\Test Support