From c81a611f668c093a2f12232e32f3db0d17a6dbf0 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Fri, 16 Feb 2024 04:31:40 -0800 Subject: [PATCH] Backup:DeivceManager test code. --- .../src/MissionStateMachine.cpp | 16 +++--- .../MissionManager/src/MissionStateMachine.h | 4 +- hal/src/HalMakePtr.cpp | 4 +- test/hal/tool/CMakeLists.txt | 2 - test/hal/tool/include/HalTestTool.h | 47 ++++++++++++++++ test/hal/tool/src/HalMakePtrTest.cpp | 55 ++++++++++++++++++ test/hal/tool/src/HalMakePtrTest.h | 31 ++++++++++ test/hal/tool/src/HalTestTool.cpp | 33 ++++++++++- test/middleware/DeviceManager/CMakeLists.txt | 9 ++- .../DeviceManager/src/DeviceManager_Test.cpp | 10 ++-- .../tool/src/McuManagerTestTool.cpp | 1 - test/utils/Config/CMakeLists.txt | 5 -- utils/StatusCode/CMakeLists.txt | 13 ++++- utils/StatusCode/include/StatusCode.h | 56 ++++++++----------- utils/StatusCode/src/StatusCode.c | 29 +++------- 15 files changed, 232 insertions(+), 83 deletions(-) create mode 100644 test/hal/tool/src/HalMakePtrTest.cpp create mode 100644 test/hal/tool/src/HalMakePtrTest.h diff --git a/application/MissionManager/src/MissionStateMachine.cpp b/application/MissionManager/src/MissionStateMachine.cpp index 336a28a2..7685a542 100644 --- a/application/MissionManager/src/MissionStateMachine.cpp +++ b/application/MissionManager/src/MissionStateMachine.cpp @@ -38,15 +38,15 @@ void MissionStateMachine::Init(void) LogError("State machine init failed.\n"); return; } - mStartMission = IDeviceManager::GetInstance()->GetIpcMission(); - RunStateMachine(mStartMission); + // mStartMission = IDeviceManager::GetInstance()->GetIpcMission(); + // RunStateMachine(mStartMission); } void MissionStateMachine::UnInit(void) { } -void MissionStateMachine::RunStateMachine(const IpcMission &mission) -{ - LogInfo("Make all states and start the state machine.\n"); - mStateTree[MissionState::TOP_STATE] = MissionManagerMakeImpl::GetInstance()->CreateTopState(); - mStateMachine->SetTopState(mStateTree[MissionState::TOP_STATE].get()); -} +// void MissionStateMachine::RunStateMachine(const IpcMission &mission) +// { +// LogInfo("Make all states and start the state machine.\n"); +// mStateTree[MissionState::TOP_STATE] = MissionManagerMakeImpl::GetInstance()->CreateTopState(); +// mStateMachine->SetTopState(mStateTree[MissionState::TOP_STATE].get()); +// } diff --git a/application/MissionManager/src/MissionStateMachine.h b/application/MissionManager/src/MissionStateMachine.h index df1f6030..a9b3ba2e 100644 --- a/application/MissionManager/src/MissionStateMachine.h +++ b/application/MissionManager/src/MissionStateMachine.h @@ -33,11 +33,11 @@ public: void UnInit(void); private: - void RunStateMachine(const IpcMission &mission); + // void RunStateMachine(const IpcMission &mission); private: std::shared_ptr mStateMachine; std::map> mStateTree; - IpcMission mStartMission; + // IpcMission mStartMission; }; #endif \ No newline at end of file diff --git a/hal/src/HalMakePtr.cpp b/hal/src/HalMakePtr.cpp index d0391883..ec3d6ec8 100644 --- a/hal/src/HalMakePtr.cpp +++ b/hal/src/HalMakePtr.cpp @@ -31,7 +31,7 @@ StatusCode CreateHalModule(void) auto instance = std::make_shared(); StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance); if (IsCodeOK(code2)) { - LogInfo("IHal manager instance is ok.\n"); + LogInfo("Hal instance is ok.\n"); IHalCpp::GetInstance(&instance); } return code2; @@ -46,7 +46,7 @@ void CreateHalCppModule(void) auto instance = std::make_shared(); StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance); if (IsCodeOK(code2)) { - LogInfo("IHal manager instance is ok.\n"); + LogInfo("Hal instance is ok.\n"); IHalCpp::GetInstance(&instance); } } diff --git a/test/hal/tool/CMakeLists.txt b/test/hal/tool/CMakeLists.txt index 14fb1f6f..f29c7d04 100644 --- a/test/hal/tool/CMakeLists.txt +++ b/test/hal/tool/CMakeLists.txt @@ -17,8 +17,6 @@ include_directories( # ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs # ) - - aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET HalTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h index 841cd059..a258c666 100644 --- a/test/hal/tool/include/HalTestTool.h +++ b/test/hal/tool/include/HalTestTool.h @@ -15,16 +15,63 @@ #ifndef HAL_TEST_TOOL_H #define HAL_TEST_TOOL_H #include "HalCpp.h" +#include +#include +using ::testing::_; +using ::testing::Action; +using ::testing::ActionInterface; +using ::testing::AnyNumber; +using ::testing::Assign; +using ::testing::AtLeast; +using ::testing::ByMove; +using ::testing::ByRef; +using ::testing::DefaultValue; +using ::testing::DoAll; +using ::testing::DoDefault; +using ::testing::IgnoreResult; +using ::testing::Invoke; +using ::testing::InvokeWithoutArgs; +using ::testing::MakePolymorphicAction; +using ::testing::PolymorphicAction; +using ::testing::Return; +using ::testing::ReturnNew; +using ::testing::ReturnNull; +using ::testing::ReturnPointee; +using ::testing::ReturnRef; +using ::testing::ReturnRefOfCopy; +using ::testing::ReturnRoundRobin; +using ::testing::SaveArg; +using ::testing::SetArgPointee; +using ::testing::SetArgumentPointee; +using ::testing::Unused; +using ::testing::WithArgs; +using ::testing::internal::BuiltInDefaultValue; class HalCppTest : public HalCpp { public: HalCppTest() = default; virtual ~HalCppTest() = default; + StatusCode GetLedHals(std::vector> &ledHals) override; + +protected: + virtual StatusCode GetLedHalsTrace(std::vector> &ledHals); +}; +class HalCppMock : public HalCppTest +{ +public: + HalCppMock() = default; + virtual ~HalCppMock() = default; + MOCK_METHOD1(GetLedHalsTrace, StatusCode(std::vector> &)); }; class HalTestTool { public: HalTestTool() = default; virtual ~HalTestTool() = default; + void Init(void); + void UnInit(void); + +private: + std::shared_ptr mHalMock; }; #endif \ No newline at end of file diff --git a/test/hal/tool/src/HalMakePtrTest.cpp b/test/hal/tool/src/HalMakePtrTest.cpp new file mode 100644 index 00000000..04ff2b9c --- /dev/null +++ b/test/hal/tool/src/HalMakePtrTest.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "HalMakePtrTest.h" +#include "ILog.h" +void OverrideHalMakePtrObject(std::shared_ptr &halMock) +{ + std::shared_ptr impl = std::make_shared(); + std::shared_ptr test = std::dynamic_pointer_cast(impl); + if (test) { + test->mHalCppMock = halMock; + } + HalMakePtr::GetInstance(&impl); +} +void CancelOverrideHalMakePtrObject(void) +{ + std::shared_ptr tmp = HalMakePtr::GetInstance(); + std::shared_ptr test = std::dynamic_pointer_cast(tmp); + if (test) { + test->mHalCppMock.reset(); + } + std::shared_ptr impl = std::make_shared(); + HalMakePtr::GetInstance(&impl); +} +HalMakePtrTest::HalMakePtrTest() +{ + // +} +HalMakePtrTest::~HalMakePtrTest() +{ + // + mHalCppMock.reset(); +} +StatusCode HalMakePtrTest::CreateHalSharePtr(std::shared_ptr &impl) +{ + if (mHalCppMock) { + LogInfo("CreateHalSharePtr mHalCppMock\n"); + impl = mHalCppMock; + } + else { + LogWarning("CreateMcuManager failed:mHalCppMock is nullptr.\n"); + } + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/test/hal/tool/src/HalMakePtrTest.h b/test/hal/tool/src/HalMakePtrTest.h new file mode 100644 index 00000000..1fc7ed16 --- /dev/null +++ b/test/hal/tool/src/HalMakePtrTest.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HAL_MAKE_PTR_TEST_H +#define HAL_MAKE_PTR_TEST_H +#include "HalMakePtr.h" +#include "HalTestTool.h" +void OverrideHalMakePtrObject(std::shared_ptr &halMock); +void CancelOverrideHalMakePtrObject(void); +class HalMakePtrTest : public HalMakePtr +{ +public: + HalMakePtrTest(); + virtual ~HalMakePtrTest(); + StatusCode CreateHalSharePtr(std::shared_ptr &impl) override; + +public: + std::shared_ptr mHalCppMock; +}; +#endif \ No newline at end of file diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index 6f566131..f7140252 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -12,4 +12,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "HalTestTool.h" \ No newline at end of file +#include "HalTestTool.h" +#include "HalMakePtrTest.h" +#include "ILog.h" +StatusCode HalCppTest::GetLedHals(std::vector> &ledHals) +{ + LogInfo("HalCppTest::GetLedHals\n"); + StatusCode code = GetLedHalsTrace(ledHals); + if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { + return HalCpp::GetLedHals(ledHals); + } + return code; +} +StatusCode HalCppTest::GetLedHalsTrace(std::vector> &ledHals) +{ + // + LogInfo("HalCppTest::GetLedHalsTrace\n"); + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +void HalTestTool::Init(void) +{ + mHalMock = std::make_shared(); + EXPECT_CALL(*mHalMock.get(), GetLedHalsTrace(_)) + .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + OverrideHalMakePtrObject(mHalMock); +} +void HalTestTool::UnInit(void) +{ + // std::shared_ptr impl = std::make_shared(); + // IHalCpp::GetInstance(&impl); + mHalMock.reset(); + CancelOverrideHalMakePtrObject(); +} \ No newline at end of file diff --git a/test/middleware/DeviceManager/CMakeLists.txt b/test/middleware/DeviceManager/CMakeLists.txt index a2d97ecf..05d254c4 100644 --- a/test/middleware/DeviceManager/CMakeLists.txt +++ b/test/middleware/DeviceManager/CMakeLists.txt @@ -10,13 +10,16 @@ include_directories( ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/UartDevice/include ${UTILS_SOURCE_PATH}/McuProtocol/include + ${HAL_SOURCE_PATH}/include + ${HAL_SOURCE_PATH}/src ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include - ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include - ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include - ${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/include + ${TEST_SOURCE_PATH}/hal/tool/include + # ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include + # ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include + # ${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include ) diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp index 47c6864a..2364e2c0 100644 --- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "HalTestTool.h" #include "IDeviceManager.h" #include "ILog.h" #include @@ -19,7 +20,7 @@ #include namespace DeviceManagerTest { -class DeviceManagerTest : public testing::Test +class DeviceManagerTest : public testing::Test, public HalTestTool { public: DeviceManagerTest() {} @@ -32,12 +33,13 @@ public: static void TearDownTestCase() { ILogUnInit(); } virtual void SetUp() { - CreateDeviceManagerModule(); // mLinuxTest = LinuxTest::CreateLinuxTest(); // std::shared_ptr test = mLinuxTest; // LinuxApiMock::GetInstance(&test); // LinuxApiMock::GetInstance()->Init(); - // McuManagerTestTool::Init(mLinuxTest); + HalTestTool::Init(); + CreateHalCppModule(); + CreateDeviceManagerModule(); // CreateMcuManager(); } virtual void TearDown() @@ -46,7 +48,7 @@ public: // mLinuxTest = std::make_shared(); // std::shared_ptr test = std::make_shared(); // LinuxApiMock::GetInstance(&test); - // McuManagerTestTool::UnInit(); + HalTestTool::UnInit(); // DestroyMcuManager(); DestroyDeviceManagerModule(); } diff --git a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp index d9771fcc..2db7bd85 100644 --- a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp +++ b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp @@ -28,7 +28,6 @@ void McuManagerTestTool::Init(std::shared_ptr &mock) { LogInfo("McuManagerTestTool::Init\n"); mMcuManagerMock = std::make_shared(); - // EXPECT_CALL(*mMcuManagerMock.get(), DeleteMcuAsk(_)).Times(500); OverrideMcuManagerMakePtrObject(); std::shared_ptr tmp = McuManagerMakePtr::GetInstance(); std::shared_ptr test = std::dynamic_pointer_cast(tmp); diff --git a/test/utils/Config/CMakeLists.txt b/test/utils/Config/CMakeLists.txt index 30499ab0..09c31af2 100644 --- a/test/utils/Config/CMakeLists.txt +++ b/test/utils/Config/CMakeLists.txt @@ -12,16 +12,11 @@ include_directories( ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include ) -message("=========================================${EXTERNAL_LIBS_OUTPUT_PATH}") link_directories( ${LIBS_OUTPUT_PATH} ${EXTERNAL_LIBS_OUTPUT_PATH} ) - - - - aux_source_directory(. SRC_FILES) aux_source_directory(./src SRC_FILES) diff --git a/utils/StatusCode/CMakeLists.txt b/utils/StatusCode/CMakeLists.txt index 8cf4c191..d1b3f98f 100644 --- a/utils/StatusCode/CMakeLists.txt +++ b/utils/StatusCode/CMakeLists.txt @@ -9,12 +9,11 @@ include_directories( ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include ) + #do not rely on any other library #link_directories( #) - - aux_source_directory(./src SRC_FILES) set(TARGET_NAME StatusCode) @@ -33,11 +32,21 @@ add_custom_target( -p ${PLATFORM_PATH}/cmake-shell WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/StatusCode ) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + StatusCode_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/StatusCode +) add_custom_command( TARGET ${TARGET_NAME} PRE_BUILD COMMAND make StatusCode_code_check + COMMAND make StatusCode_code_format WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ ) endif() + define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/utils/StatusCode/include/StatusCode.h b/utils/StatusCode/include/StatusCode.h index 50a1e90b..c36c9bc9 100644 --- a/utils/StatusCode/include/StatusCode.h +++ b/utils/StatusCode/include/StatusCode.h @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,38 +16,28 @@ #define STATUSCODE_H #include #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif - enum STATUS_CODE - { - STATUS_CODE_OK = 0, - STATUS_CODE_NOT_OK, - STATUS_CODE_VIRTUAL_FUNCTION, - STATUS_CODE_INVALID_PARAMENTER, - STATUS_CODE_MAKE_SHARED_PTR_FAILED, - STATUS_CODE_END - }; - typedef struct status_code StatusCode; - typedef struct status_code - { - const char *(*mPrintStringCode)(const StatusCode); - const bool (*mCodeEqual)(const StatusCode, const char *); - const long int mStatusCode; - } StatusCode; - const StatusCode CreateStatusCode(const long int code); - static inline const char *PrintStringCode(const StatusCode code) - { - return code.mPrintStringCode(code); - } - static inline bool IsCodeOK(const StatusCode code) - { - return STATUS_CODE_OK == code.mStatusCode ? true : false; - } - static inline bool StatusCodeEqual(const StatusCode code, const char *value) - { - return code.mCodeEqual(code, value); - } +enum STATUS_CODE +{ + STATUS_CODE_OK = 0, + STATUS_CODE_NOT_OK, + STATUS_CODE_VIRTUAL_FUNCTION, + STATUS_CODE_INVALID_PARAMENTER, + STATUS_CODE_MAKE_SHARED_PTR_FAILED, + STATUS_CODE_END +}; +typedef struct status_code StatusCode; +typedef struct status_code +{ + const char *(*mPrintStringCode)(const StatusCode); + const bool (*mCodeEqual)(const StatusCode, const char *); + const long int mStatusCode; +} StatusCode; +const StatusCode CreateStatusCode(const long int code); +static inline const char *PrintStringCode(const StatusCode code) { return code.mPrintStringCode(code); } +static inline bool IsCodeOK(const StatusCode code) { return STATUS_CODE_OK == code.mStatusCode ? true : false; } +static inline bool StatusCodeEqual(const StatusCode code, const char *value) { return code.mCodeEqual(code, value); } #ifdef __cplusplus } #endif diff --git a/utils/StatusCode/src/StatusCode.c b/utils/StatusCode/src/StatusCode.c index 2ab2fa28..e9106385 100644 --- a/utils/StatusCode/src/StatusCode.c +++ b/utils/StatusCode/src/StatusCode.c @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,14 +17,10 @@ #include #include static const char *StatusCodeString[STATUS_CODE_END + 1] = { - "STATUS_CODE_OK", - "STATUS_CODE_NOT_OK", - "STATUS_CODE_INVALID_PARAMENTER", - "STATUS_CODE_END"}; + "STATUS_CODE_OK", "STATUS_CODE_NOT_OK", "STATUS_CODE_INVALID_PARAMENTER", "STATUS_CODE_END"}; static const char *_PrintStringCode_(const StatusCode this) { - if (STATUS_CODE_OK <= this.mStatusCode && this.mStatusCode <= STATUS_CODE_END) - { + if (STATUS_CODE_OK <= this.mStatusCode && this.mStatusCode <= STATUS_CODE_END) { LogInfo("Status code = [ %s ]\n", StatusCodeString[this.mStatusCode]); return StatusCodeString[this.mStatusCode]; } @@ -33,32 +29,25 @@ static const char *_PrintStringCode_(const StatusCode this) } static const bool CodeEqual(const StatusCode code, const char *value) { - if (STATUS_CODE_OK >= code.mStatusCode || code.mStatusCode >= STATUS_CODE_END) - { + if (STATUS_CODE_OK >= code.mStatusCode || code.mStatusCode >= STATUS_CODE_END) { return false; } - if (strlen(value) != strlen(StatusCodeString[code.mStatusCode])) - { + if (strlen(value) != strlen(StatusCodeString[code.mStatusCode])) { return false; } - if (memcmp(value, StatusCodeString[code.mStatusCode], strlen(value)) == 0) - { + if (memcmp(value, StatusCodeString[code.mStatusCode], strlen(value)) == 0) { return true; } return false; } static StatusCode NewStatusCode(const long int code) { - StatusCode result = { - _PrintStringCode_, - CodeEqual, - code}; + StatusCode result = {_PrintStringCode_, CodeEqual, code}; return result; } const StatusCode CreateStatusCode(const long int code) { - if (STATUS_CODE_OK <= code && code <= STATUS_CODE_END) - { + if (STATUS_CODE_OK <= code && code <= STATUS_CODE_END) { return NewStatusCode(code); } LogError("undefined code.\n");