1、示例代码 #
#include <stdint.h>
typedef uint8_t Std_ReturnType;
#define RTE_E_OK 0
#define DEM_EVENT_STATUS_PREPASSED 0
#define DEM_EVENT_STATUS_PREFAILED 1
#define RTE_SWC_ADC_ETHDIAG_SWC_INU_LinkUpStatus_u8_Signal 1
typedef struct {
uint8_t inu_LinkUpSts;
uint8_t ccu_LinkUpSts;
} LinkStatus;
Std_ReturnType Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal(uint8_t* data) {
*data = RTE_SWC_ADC_ETHDIAG_SWC_INU_LinkUpStatus_u8_Signal;
return RTE_E_OK;
}
void Rte_Call_Event_DTC_0xD70188_Event_SetSts(uint8_t status) {
// 添加实际的实现,根据 DEM 事件设置状态的需求
// 此处仅为示例
// ...
}
void EthLinkupStsDTCMainfunction(void) {
uint8_t inu_LinkUpSts, ccu_LinkUpSts;
Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal(&inu_LinkUpSts);
if (inu_LinkUpSts != 1) {
/* 1inuDown */
Rte_Call_Event_DTC_0xD70188_Event_SetSts(DEM_EVENT_STATUS_PREFAILED);
} else {
Rte_Call_Event_DTC_0xD70188_Event_SetSts(DEM_EVENT_STATUS_PREPASSED);
}
}
int main(void) {
/* 空的 main 函数 */
return 0;
}
2、测试函数 EthLinkupStsDTCMainfunction #
3、测试用例设计 #
对被调函数Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal打桩,因为Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal的逻辑中,其实就是对inu_LinkUpSts重新赋值了。
Testcase1:对Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal打桩,将inu_LinkUpSts的值修改成不等于1的值,来满足 line31 的条件。
Testcase2:对Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal打桩,将inu_LinkUpSts的值修改成等于1的值,来满足 line34 的条件。
桩函数代码如下:
#include "cpptest.h"
#include <stdint.h>
typedef uint8_t Std_ReturnType;
/**
* This file contains user stub definitions.
*
* To create a user stub:
* 1. Open stub template list:
* - Eclipse IDE: type 'stub' and press <Ctrl> <Space>
* - Microsoft Visual Studio IDE: use context menu C++test->Insert Snippet...
* 2. Choose the stub template from the list.
* 3. #include the header file where the function you want to stub is originally
* declared as well as any supporting header files as necessary.
* 4. Fill out the stub signature to match that of the original function,
* while keeping the CppTest_Stub prefix when it applies.
* Note: for C++ operators, use appropriate stub names - e.g.:
* CppTest_Stub_operator_new for operator new
* CppTest_Stub_operator_delete for operator delete
* CppTest_Stub_operator_plus for operator +
* Refer to C++test User's Guide for a complete list of stub names for operators.
* 5. Fill out the body of the stub according to intent.
*
* Available C++test API functions (see C++test Users Guide for details):
* void CppTest_Assert(bool test, const char * message)
* void CppTest_Break()
* const char* CppTest_GetCurrentTestCaseName()
* const char* CppTest_GetCurrentTestSuiteName()
* bool CppTest_IsCurrentTestCase(const char* testCaseName)
*/
/**
* Header files where the stubbed functions are originally declared.
* Verify #include directives and add any additional header files as necessary.
*/
/** User stub definition for function: Std_ReturnType Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal(uint8_t *) */
EXTERN_C_LINKAGE Std_ReturnType Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal (uint8_t * data) ;
EXTERN_C_LINKAGE Std_ReturnType CppTest_Stub_Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal (uint8_t * data)
{
CPPTEST_STUB_CALLED("Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal");
Std_ReturnType __return = (unsigned char)0;
/**
* This section enables Dynamic Stub Configuration with Stub Callbacks.
*
* IMPORTANT: THIS COMMENT BLOCK SHOULD NOT BE DELETED OR MODIFIED
*
* 1. Define stub callback function in test suite file - use the following signature:
* void CppTest_StubCallback_SomeName(CppTest_StubCallInfo* stubCallInfo, Std_ReturnType* __return, uint8_t * data)
*
* 2. Register stub callback in test case function - use the following code:
* CPPTEST_REGISTER_STUB_CALLBACK("Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal", &CppTest_StubCallback_SomeName);
*
* 3. Fill out the body of the stub callback function according to intent.
* The following line may be used to call original function inside stub callback:
* *__return = Rte_Read_SWC_ADC_FM_SWC_INU_LinkUpStatus_u8_Signal(data);
*/
if (CPPTEST_STUB_HAS_CALLBACK()) {
CPPTEST_STUB_CALLBACK_PARAMS(Std_ReturnType* __return, uint8_t * data);
CPPTEST_STUB_INVOKE_CALLBACK(&__return, data);
} else {
if(CppTest_IsCurrentTestCase("TestSuite_main_c_136bf872_test_EthLinkupStsDTCMainfunction_1"))
{
*data = 1;
}
else
{
*data = 2;
}
}
return __return;
}