From 12a4464bd385ae88c3f1044d771341cd134cb18c Mon Sep 17 00:00:00 2001 From: Saya Sugiura Date: Wed, 28 Jul 2021 10:13:59 +0900 Subject: gtest: Refactor tests/CMakeLists.txt This also removed following tests to more appropriate place: - dlt-test-preregister-context.c - dlt-test-cpp-extension.cpp Signed-off-by: Saya Sugiura --- src/tests/CMakeLists.txt | 4 + src/tests/dlt-test-cpp-extension.cpp | 118 ++++++++++++++++++++++++++++ src/tests/dlt-test-preregister-context.c | 57 ++++++++++++++ tests/CMakeLists.txt | 131 ++++++++++++++++--------------- tests/dlt-test-cpp-extension.cpp | 118 ---------------------------- tests/dlt-test-preregister-context.c | 57 -------------- 6 files changed, 247 insertions(+), 238 deletions(-) create mode 100644 src/tests/dlt-test-cpp-extension.cpp create mode 100644 src/tests/dlt-test-preregister-context.c delete mode 100644 tests/dlt-test-cpp-extension.cpp delete mode 100644 tests/dlt-test-preregister-context.c diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 23ce836..76f89cb 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -22,6 +22,7 @@ set(TARGET_LIST ${TARGET_LIST} dlt-test-stress-client) set(TARGET_LIST ${TARGET_LIST} dlt-test-stress) set(TARGET_LIST ${TARGET_LIST} dlt-test-fork-handler) set(TARGET_LIST ${TARGET_LIST} dlt-test-init-free) +set(TARGET_LIST ${TARGET_LIST} dlt-test-preregister-context) set(TARGET_LIST ${TARGET_LIST} dlt-test-filetransfer) install(FILES dlt-test-filetransfer-file dlt-test-filetransfer-image.png DESTINATION share/dlt-filetransfer) @@ -33,6 +34,9 @@ endif() #TODO: Enable again once dlt-test-non-verbose is adapted to non-macro usage if (NOT WITH_DLT_DISABLE_MACRO) set(TARGET_LIST ${TARGET_LIST} dlt-test-non-verbose) + if(WITH_DLT_CXX11_EXT) + set(TARGET_LIST ${TARGET_LIST} dlt-test-cpp-extension) + endif() endif() if(WITH_DLT_QNX_SYSTEM) diff --git a/src/tests/dlt-test-cpp-extension.cpp b/src/tests/dlt-test-cpp-extension.cpp new file mode 100644 index 0000000..eef0d8e --- /dev/null +++ b/src/tests/dlt-test-cpp-extension.cpp @@ -0,0 +1,118 @@ +/* + * SPDX license identifier: MPL-2.0 + * + * Copyright (C) 2015 Intel Corporation + * + * This file is part of GENIVI Project DLT - Diagnostic Log and Trace. + * + * This Source Code Form is subject to the terms of the + * Mozilla Public License (MPL), v. 2.0. + * If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * For further information see http://www.genivi.org/. + */ + +/*! + * \author Stefan Vacek Intel Corporation + * + * \copyright Copyright © 2015 Intel Corporation. \n + * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/. + * + * \file dlt-test-cpp-extension.cpp + */ + +#include "dlt_cpp_extension.hpp" +#include +#include +#include "dlt_user_macros.h" + +struct MyStruct +{ + int64_t uuid; + int32_t interfaceId; + int32_t registrationState; +}; + +template<> +inline int logToDlt(DltContextData &log, MyStruct const &value) +{ + int result = 0; + + result += dlt_user_log_write_string(&log, "("); + result += logToDlt(log, value.uuid); + result += dlt_user_log_write_string(&log, ","); + result += logToDlt(log, value.interfaceId); + result += dlt_user_log_write_string(&log, ","); + result += logToDlt(log, value.registrationState); + result += dlt_user_log_write_string(&log, ")"); + + if (result != 0) + result = -1; + + return result; +} + +/** + * Sample code to show usage of the cpp-extension + * mainly the variadic templates + */ +int main() +{ + if (dlt_register_app("TCPP", "Test cpp extension") < 0) { + printf("Failed to register application\n"); + return -1; + } + + DltContext ctx; + + if (dlt_register_context_ll_ts(&ctx, "TCPP", "Test cpp extension", DLT_LOG_INFO, DLT_TRACE_STATUS_OFF) < 0) { + printf("Failed to register context\n"); + return -1; + } + + dlt_enable_local_print(); + dlt_verbose_mode(); + + DLT_LOG(ctx, DLT_LOG_WARN, DLT_STRING("a message")); /* the classic way to go */ + + int an_int = 42; + float a_float = 22.7; + DLT_LOG_FCN_CXX(ctx, DLT_LOG_WARN, "Testing DLT_LOG_CXX_FCN", an_int, a_float); + DLT_LOG_CXX(ctx, DLT_LOG_WARN, 1.0, 65); + + /* Example for logging user-defined types */ + MyStruct myData = { 1u, 2u, 3u }; + DLT_LOG_CXX(ctx, DLT_LOG_WARN, "MyStruct myData", myData); + + char *non_const_string = (char *)malloc(17); + memcpy(non_const_string, "non_const_string", 16); + non_const_string[16] = 0; + DLT_LOG_CXX(ctx, DLT_LOG_WARN, "char *", non_const_string); + + std::string aString = "std::string"; + DLT_LOG_CXX(ctx, DLT_LOG_WARN, "std::string", aString); + + std::vector intVector; + intVector.push_back(0); + intVector.push_back(1); + intVector.push_back(2); + DLT_LOG_CXX(ctx, DLT_LOG_WARN, "vector", intVector); + + std::vector doubleList; + doubleList.push_back(10.); + doubleList.push_back(11.); + doubleList.push_back(12.); + DLT_LOG_CXX(ctx, DLT_LOG_WARN, "list", doubleList); + + std::map testMap; + testMap["apple"] = 100; + testMap["plum"] = 200; + testMap["orange"] = 300; + DLT_LOG_CXX(ctx, DLT_LOG_WARN, "map", testMap); + + dlt_unregister_context(&ctx); + dlt_unregister_app(); + + return 0; +} diff --git a/src/tests/dlt-test-preregister-context.c b/src/tests/dlt-test-preregister-context.c new file mode 100644 index 0000000..caa77f0 --- /dev/null +++ b/src/tests/dlt-test-preregister-context.c @@ -0,0 +1,57 @@ +/* + * SPDX license identifier: MPL-2.0 + * + * Copyright (C) 2015 Intel Corporation + * + * This file is part of GENIVI Project DLT - Diagnostic Log and Trace. + * + * This Source Code Form is subject to the terms of the + * Mozilla Public License (MPL), v. 2.0. + * If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * For further information see http://www.genivi.org/. + */ + +/*! + * \author Stefan Vacek Intel Corporation + * + * \copyright Copyright © 2015 Intel Corporation. \n + * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/. + * + * \file dlt-test-preregister-context.c + */ + +#include /* for fork() */ + +#include "dlt.h" +#include "dlt_user_macros.h" + +/** + * @brief sample code for using pre-registered contexts + */ +int main() +{ + DltContext mainContext; + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 200000 * 1000; + + DLT_REGISTER_CONTEXT(mainContext, "CTXP", "main context"); + + DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("First message before app registered")); + nanosleep(&ts, NULL); + + DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("Second message before app registered")); + nanosleep(&ts, NULL); + + DLT_REGISTER_APP("PRNT", "Sample pre-register application"); + + DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("First message after app registered")); + nanosleep(&ts, NULL); + + DLT_UNREGISTER_APP() + ; + + return 0; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 26edca4..37de4cb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,30 +1,83 @@ -#add_compile_options(-g -fsanitize=address) -add_compile_options(-isystem ${gtest_SOURCE_DIR}/include) +# Setup testing +enable_testing() + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem ${gtest_SOURCE_DIR}/include") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${gtest_SOURCE_DIR}/include -std=gnu++0x") configure_file(${PROJECT_SOURCE_DIR}/tests/testfile.dlt ${PROJECT_BINARY_DIR}/tests COPYONLY) configure_file(${PROJECT_SOURCE_DIR}/tests/testfilter.txt ${PROJECT_BINARY_DIR}/tests COPYONLY) configure_file(${PROJECT_SOURCE_DIR}/tests/testfile_filetransfer.txt ${PROJECT_BINARY_DIR}/tests COPYONLY) +set(GTEST_LIBS gtest gtest_main) +set(GTEST_LIBS ${GTEST_LIBS} CACHE STRING "Gtest libraries") + if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - set(LIBRARIES gtest gtest_main) + set(LIBRARIES "") else() - set(LIBRARIES gtest socket) + set(LIBRARIES socket) endif() -set(DLT_LIBRARIES ${LIBRARIES} dlt) -set(DLT_DAEMON_LIBRARIES ${LIBRARIES} dlt_daemon) -set(DLT_CONTROL_LIBRARIES ${LIBRARIES} dlt dlt_control_common_lib) +set(DLT_LIBRARIES dlt ${GTEST_BOTH_LIBRARIES} ${LIBRARIES}) +set(DLT_DAEMON_LIBRARIES dlt_daemon ${GTEST_BOTH_LIBRARIES} ${LIBRARIES}) +set(DLT_CONTROL_LIBRARIES dlt dlt_control_common_lib ${GTEST_BOTH_LIBRARIES}) -if(WITH_SYSTEMD OR WITH_SYSTEMD_WATCHDOG OR WITH_SYSTEMD_JOURNAL) - add_definitions( -DSD_EXPORT_SYMBOLS ) +#Receiver used for QTs. add_test() is not required +add_executable(dlt_test_receiver dlt_test_receiver.c) +target_link_libraries(dlt_test_receiver ${DLT_LIBRARIES}) + +#################### +# DLT library tests +#################### +set(TARGET_LIST gtest_dlt_common + gtest_dlt_user + gtest_dlt_daemon_common + dlt_env_ll_unit_test + ) + +foreach(target IN LISTS TARGET_LIST) + set(target_SRCS ${target}) + if(${target} STREQUAL "gtest_dlt_daemon_common") + set(target_SRCS ${target_SRCS} ../src/daemon/dlt_daemon_common.c) + endif() + add_executable(${target} ${target_SRCS}) + target_link_libraries(${target} ${DLT_LIBRARIES}) + add_test(NAME ${target} + COMMAND ${target} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +endforeach() + +################### +# DLT daemon tests +################### +set(TARGET_LIST gtest_dlt_daemon_gateway + gtest_dlt_daemon_offline_log + gtest_dlt_daemon_filter + gtest_dlt_daemon_event_handler + ) +if(WITH_DLT_SHM_ENABLE) + list(APPEND TARGET_LIST gtest_dlt_shm) endif() -add_executable(gtest_dlt_common gtest_dlt_common.cpp) -target_link_libraries(gtest_dlt_common ${DLT_LIBRARIES}) -add_test(NAME gtest_dlt_common - COMMAND gtest_dlt_common - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - +foreach(target IN LISTS TARGET_LIST) + set(target_SRCS ${target}) + add_executable(${target} ${target_SRCS} ${systemd_SRCS}) + target_link_libraries(${target} ${DLT_DAEMON_LIBRARIES}) + if(${target} STREQUAL "gtest_dlt_daemon_event_handler" OR + ${target} STREQUAL "gtest_dlt_shm") + add_test(NAME ${target} + COMMAND ${target} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + else() + add_test(NAME ${target} + COMMAND /bin/sh "${target}.sh" + ${target} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + endif() +endforeach() + +##################### +# DLT conotrol tests +##################### if(WITH_EXTENDED_FILTERING) configure_file(${PROJECT_SOURCE_DIR}/tests/testfile_extended.dlt ${PROJECT_BINARY_DIR}/tests COPYONLY) configure_file(${PROJECT_SOURCE_DIR}/tests/testfilter.json ${PROJECT_BINARY_DIR}/tests COPYONLY) @@ -35,51 +88,3 @@ if(WITH_EXTENDED_FILTERING) WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) endif() -add_executable(gtest_dlt_user gtest_dlt_user.cpp) -target_link_libraries(gtest_dlt_user ${DLT_LIBRARIES}) -add_test(NAME gtest_dlt_user - COMMAND gtest_dlt_user - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -add_executable(gtest_dlt_daemon_common gtest_dlt_daemon_common.cpp ../src/daemon/dlt_daemon_common.c) -target_link_libraries(gtest_dlt_daemon_common ${DLT_LIBRARIES}) -add_test(NAME gtest_dlt_daemon_common - COMMAND gtest_dlt_daemon_common - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -add_executable(dlt_test_receiver dlt_test_receiver.c) -target_link_libraries(dlt_test_receiver dlt) - -add_executable(dlt_env_ll_unit_test dlt_env_ll_unit_test.cpp) -target_link_libraries(dlt_env_ll_unit_test ${DLT_LIBRARIES}) -add_test(NAME dlt_env_ll_unit_test - COMMAND gtest_dlt_common - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -add_executable(dlt-test-preregister-context dlt-test-preregister-context.c) -target_link_libraries(dlt-test-preregister-context ${DLT_LIBRARIES}) - -add_executable(gtest_dlt_daemon_gateway gtest_dlt_daemon_gateway.cpp ${systemd_SRCS}) -target_link_libraries(gtest_dlt_daemon_gateway ${DLT_DAEMON_LIBRARIES}) - -add_executable(gtest_dlt_daemon_event_handler gtest_dlt_daemon_event_handler.cpp ${systemd_SRCS}) -target_link_libraries(gtest_dlt_daemon_event_handler ${DLT_DAEMON_LIBRARIES}) -add_test(NAME gtest_dlt_daemon_event_handler - COMMAND gtest_dlt_daemon_event_handler - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -add_executable(gtest_dlt_daemon_offline_log gtest_dlt_daemon_offline_log.cpp ${systemd_SRCS}) -target_link_libraries(gtest_dlt_daemon_offline_log ${DLT_DAEMON_LIBRARIES}) - -if(WITH_DLT_SHM_ENABLE) - add_executable(gtest_dlt_shm gtest_dlt_shm.cpp) -endif(WITH_DLT_SHM_ENABLE) - -if(WITH_DLT_SHM_ENABLE) - target_link_libraries(gtest_dlt_shm ${DLT_DAEMON_LIBRARIES}) -endif(WITH_DLT_SHM_ENABLE) - -if(WITH_DLT_CXX11_EXT) - add_executable(dlt-test-cpp-extension dlt-test-cpp-extension.cpp) - target_link_libraries(dlt-test-cpp-extension ${DLT_LIBRARIES}) -endif() diff --git a/tests/dlt-test-cpp-extension.cpp b/tests/dlt-test-cpp-extension.cpp deleted file mode 100644 index eef0d8e..0000000 --- a/tests/dlt-test-cpp-extension.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/* - * SPDX license identifier: MPL-2.0 - * - * Copyright (C) 2015 Intel Corporation - * - * This file is part of GENIVI Project DLT - Diagnostic Log and Trace. - * - * This Source Code Form is subject to the terms of the - * Mozilla Public License (MPL), v. 2.0. - * If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. - * - * For further information see http://www.genivi.org/. - */ - -/*! - * \author Stefan Vacek Intel Corporation - * - * \copyright Copyright © 2015 Intel Corporation. \n - * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/. - * - * \file dlt-test-cpp-extension.cpp - */ - -#include "dlt_cpp_extension.hpp" -#include -#include -#include "dlt_user_macros.h" - -struct MyStruct -{ - int64_t uuid; - int32_t interfaceId; - int32_t registrationState; -}; - -template<> -inline int logToDlt(DltContextData &log, MyStruct const &value) -{ - int result = 0; - - result += dlt_user_log_write_string(&log, "("); - result += logToDlt(log, value.uuid); - result += dlt_user_log_write_string(&log, ","); - result += logToDlt(log, value.interfaceId); - result += dlt_user_log_write_string(&log, ","); - result += logToDlt(log, value.registrationState); - result += dlt_user_log_write_string(&log, ")"); - - if (result != 0) - result = -1; - - return result; -} - -/** - * Sample code to show usage of the cpp-extension - * mainly the variadic templates - */ -int main() -{ - if (dlt_register_app("TCPP", "Test cpp extension") < 0) { - printf("Failed to register application\n"); - return -1; - } - - DltContext ctx; - - if (dlt_register_context_ll_ts(&ctx, "TCPP", "Test cpp extension", DLT_LOG_INFO, DLT_TRACE_STATUS_OFF) < 0) { - printf("Failed to register context\n"); - return -1; - } - - dlt_enable_local_print(); - dlt_verbose_mode(); - - DLT_LOG(ctx, DLT_LOG_WARN, DLT_STRING("a message")); /* the classic way to go */ - - int an_int = 42; - float a_float = 22.7; - DLT_LOG_FCN_CXX(ctx, DLT_LOG_WARN, "Testing DLT_LOG_CXX_FCN", an_int, a_float); - DLT_LOG_CXX(ctx, DLT_LOG_WARN, 1.0, 65); - - /* Example for logging user-defined types */ - MyStruct myData = { 1u, 2u, 3u }; - DLT_LOG_CXX(ctx, DLT_LOG_WARN, "MyStruct myData", myData); - - char *non_const_string = (char *)malloc(17); - memcpy(non_const_string, "non_const_string", 16); - non_const_string[16] = 0; - DLT_LOG_CXX(ctx, DLT_LOG_WARN, "char *", non_const_string); - - std::string aString = "std::string"; - DLT_LOG_CXX(ctx, DLT_LOG_WARN, "std::string", aString); - - std::vector intVector; - intVector.push_back(0); - intVector.push_back(1); - intVector.push_back(2); - DLT_LOG_CXX(ctx, DLT_LOG_WARN, "vector", intVector); - - std::vector doubleList; - doubleList.push_back(10.); - doubleList.push_back(11.); - doubleList.push_back(12.); - DLT_LOG_CXX(ctx, DLT_LOG_WARN, "list", doubleList); - - std::map testMap; - testMap["apple"] = 100; - testMap["plum"] = 200; - testMap["orange"] = 300; - DLT_LOG_CXX(ctx, DLT_LOG_WARN, "map", testMap); - - dlt_unregister_context(&ctx); - dlt_unregister_app(); - - return 0; -} diff --git a/tests/dlt-test-preregister-context.c b/tests/dlt-test-preregister-context.c deleted file mode 100644 index caa77f0..0000000 --- a/tests/dlt-test-preregister-context.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SPDX license identifier: MPL-2.0 - * - * Copyright (C) 2015 Intel Corporation - * - * This file is part of GENIVI Project DLT - Diagnostic Log and Trace. - * - * This Source Code Form is subject to the terms of the - * Mozilla Public License (MPL), v. 2.0. - * If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. - * - * For further information see http://www.genivi.org/. - */ - -/*! - * \author Stefan Vacek Intel Corporation - * - * \copyright Copyright © 2015 Intel Corporation. \n - * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/. - * - * \file dlt-test-preregister-context.c - */ - -#include /* for fork() */ - -#include "dlt.h" -#include "dlt_user_macros.h" - -/** - * @brief sample code for using pre-registered contexts - */ -int main() -{ - DltContext mainContext; - struct timespec ts; - ts.tv_sec = 0; - ts.tv_nsec = 200000 * 1000; - - DLT_REGISTER_CONTEXT(mainContext, "CTXP", "main context"); - - DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("First message before app registered")); - nanosleep(&ts, NULL); - - DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("Second message before app registered")); - nanosleep(&ts, NULL); - - DLT_REGISTER_APP("PRNT", "Sample pre-register application"); - - DLT_LOG(mainContext, DLT_LOG_WARN, DLT_STRING("First message after app registered")); - nanosleep(&ts, NULL); - - DLT_UNREGISTER_APP() - ; - - return 0; -} -- cgit v1.2.1