mirror of
https://github.com/eclipse-openvehicle-api/openvehicle-api.git
synced 2026-08-30 12:15:12 +00:00
connection between 2 systems and bug fixes (#14)
This commit is contained in:
41
tests/unit_tests/unique_id/CMakeLists.txt
Normal file
41
tests/unit_tests/unique_id/CMakeLists.txt
Normal file
@@ -0,0 +1,41 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
#
|
||||
# This program and the accompanying materials are made available under the
|
||||
# terms of the Apache License Version 2.0 which is available at
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# Contributors:
|
||||
# Erik Verhoeven - initial API and implementation
|
||||
#*******************************************************************************
|
||||
|
||||
# Define project
|
||||
project(UniqueIDTests VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_executable(UnitTest_UniqueID unique_id_test.cpp)
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||
target_link_libraries(UnitTest_UniqueID GTest::GTest)
|
||||
if (WIN32)
|
||||
target_link_libraries(UnitTest_UniqueID Ws2_32 Winmm Rpcrt4.lib)
|
||||
else()
|
||||
target_link_libraries(UnitTest_UniqueID ${CMAKE_DL_LIBS} rt)
|
||||
endif()
|
||||
else()
|
||||
target_link_libraries(UnitTest_UniqueID GTest::GTest Rpcrt4.lib)
|
||||
endif()
|
||||
|
||||
# Add the test
|
||||
add_test(NAME UnitTest_UniqueID COMMAND UnitTest_UniqueID WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
|
||||
|
||||
# Execute the test
|
||||
add_custom_command(TARGET UnitTest_UniqueID POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E env TEST_EXECUTION_MODE=CMake "$<TARGET_FILE:UnitTest_UniqueID>" --gtest_output=xml:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/UnitTest_UniqueID.xml
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
# The unit-test project depends on a proper compilation of sdv components before
|
||||
add_dependencies(UnitTest_UniqueID dependency_sdv_components)
|
||||
85
tests/unit_tests/unique_id/unique_id_test.cpp
Normal file
85
tests/unit_tests/unique_id/unique_id_test.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Erik Verhoeven - initial API and implementation
|
||||
********************************************************************************/
|
||||
|
||||
#include <fstream>
|
||||
#include <gtest/gtest.h>
|
||||
#include "../../../global/process_watchdog.h"
|
||||
#include "../../../global/unique_id.h"
|
||||
|
||||
#if defined(_WIN32) && defined(_UNICODE)
|
||||
extern "C" int wmain(int argc, wchar_t* argv[])
|
||||
#else
|
||||
extern "C" int main(int argc, char* argv[])
|
||||
#endif
|
||||
{
|
||||
CProcessWatchdog watchdog;
|
||||
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
TEST(UniqueID, TestSmallID)
|
||||
{
|
||||
CUniqueID<int, 4> generator;
|
||||
|
||||
// Max 15 ids possible
|
||||
std::set<int> setUsed;
|
||||
int iNumber;
|
||||
for (size_t n = 0; n < 15; n++)
|
||||
{
|
||||
iNumber = generator.Generate();
|
||||
EXPECT_NE(iNumber, 0);
|
||||
EXPECT_EQ(setUsed.find(iNumber), setUsed.end());
|
||||
setUsed.insert(iNumber);
|
||||
}
|
||||
|
||||
// One more ID should fail
|
||||
iNumber = generator.Generate();
|
||||
EXPECT_EQ(iNumber, 0);
|
||||
}
|
||||
|
||||
TEST(UniqueID, TestSmallID2)
|
||||
{
|
||||
CUniqueID<uint8_t> generator;
|
||||
|
||||
// Max 255 ids possible
|
||||
std::set<uint32_t> setUsed;
|
||||
uint32_t uiNumber = 0;
|
||||
for (size_t n = 0; n < 255; n++)
|
||||
{
|
||||
uiNumber = generator.Generate();
|
||||
EXPECT_NE(uiNumber, 0u);
|
||||
EXPECT_EQ(setUsed.find(uiNumber), setUsed.end());
|
||||
setUsed.insert(uiNumber);
|
||||
}
|
||||
|
||||
// One more ID should fail
|
||||
uiNumber = generator.Generate();
|
||||
EXPECT_EQ(uiNumber, 0u);
|
||||
}
|
||||
|
||||
TEST(UniqueID, TestLargeID)
|
||||
{
|
||||
CUniqueID<uint64_t> generator;
|
||||
|
||||
// Run at the most 20000 times and check whether the number occurs multiple times
|
||||
std::set<uint64_t> setUsed;
|
||||
uint64_t uiNumber;
|
||||
for (size_t n = 0; n < 20000; n++)
|
||||
{
|
||||
uiNumber = generator.Generate();
|
||||
EXPECT_NE(uiNumber, 0u);
|
||||
EXPECT_EQ(setUsed.find(uiNumber), setUsed.end());
|
||||
setUsed.insert(uiNumber);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user