Split test clocks from test_chrono_clocks.cpp to clocks_for_unit_tests.cpp

This makes clocks available for other tests when needed.
This commit is contained in:
Julien Hecht 2026-09-02 15:55:24 +02:00
parent 6f0c02c397
commit 573a224292
7 changed files with 125 additions and 27 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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<int>
#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

View File

@ -1,5 +1,6 @@
etl_test_sources = files(
'main.cpp',
'clocks_for_unit_tests.cpp',
'murmurhash3.cpp',
'test_algorithm.cpp',
'test_algorithm_ranges.cpp',

View File

@ -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 <type_traits>
//*****************************************************************************
// 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
{

View File

@ -4047,6 +4047,7 @@
<ClInclude Include="..\etl_profile.h" />
<ClInclude Include="..\murmurhash3.h" />
<ClInclude Include="..\iterators_for_unit_tests.h" />
<ClInclude Include="..\clocks_for_unit_tests.h" />
<ClInclude Include="..\unittest++\AssertException.h" />
<ClInclude Include="..\unittest++\CheckMacros.h" />
<ClInclude Include="..\unittest++\Checks.h" />
@ -4083,6 +4084,7 @@
<ClInclude Include="..\unit_test_framework.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\clocks_for_unit_tests.cpp" />
<ClCompile Include="..\etl_error_handler\assert_function\assert_function.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Clang C++20|Win32'">true</ExcludedFromBuild>

View File

@ -1101,6 +1101,12 @@
<ClInclude Include="..\iterators_for_unit_tests.h">
<Filter>Tests\Test Support</Filter>
</ClInclude>
<ClInclude Include="..\clocks_for_unit_tests.h">
<Filter>Tests\Test Support</Filter>
</ClInclude>
<ClInclude Include="..\clocks_for_unit_tests.cpp">
<Filter>Tests\Test Support</Filter>
</ClInclude>
<ClInclude Include="..\etl_profile.h">
<Filter>Tests\Test Support</Filter>
</ClInclude>