gpt4 book ai didi

c - 使用 Google Test 测试 C 函数

转载 作者:行者123 更新时间:2023-11-30 16:15:49 25 4
gpt4 key购买 nike

我正在尝试使用 Google Test 来测试一些 C 代码,但我很困难。我真的不明白如何测试代码。我尝试过一些东西,但我不知道它是否可以。有人可以向我解释一下我所做的事情是否可以以及如何测试开关部分吗?

这是我写的测试:

    TEST_F(MPS_Tests, Wai_for_discharging_of_several_rails) {


MPS__tenPowerUpTrigger enTrigger = MPS__enGetPowerUpTrigger();
EXPECT_EQ(enPowerUpTriggerFstp, enTrigger);
MPS__vOnEnterStateAcPu1();
EXPECT_EQ(MPS__enGetInternalState(enAC), enStateAcPu1);
EXPECT_EQ(MPS__u16GetSmEvent(enAC), enStateAcPu1);

这是经过测试的代码:

MPS__tenPowerUpTrigger MPS__enGetPowerUpTrigger(void)
{
/* TODO: rework after startup trigger is considerd by design */
return enPowerUpTriggerFstp;
}

/* Wait for discharging of several rails */
void MPS__vOnEnterStateAcPu1(void)
{
MPS__tenPowerUpTrigger enTrigger = MPS__enGetPowerUpTrigger();
MPS__vSetInternalState(enAC, enStateAcPu1);

switch (enTrigger)
{
case enPowerUpTriggerFstp:
MPS__vSetWait(enAC, u16Time_AC1ASU_t4);
break;
case enPowerUpTriggerWakeUp:
MPS__vSetWait(enAC, MPS_TIMER_NOWAIT);
break;
case enPowerUpTriggerReset:
MPS__vSetWait(enAC, u16Time_AC1CSU_t2);
break;
default:
/* error handling */
break;
}
}

谢谢!

最佳答案

覆盖开关中所有选项的简单方法是将MPS__enGetPowerUpTrigger更改为如下所示:

MPS__tenPowerUpTrigger MPS__enGetPowerUpTrigger(void)
{
const MPS__tenPowerUpTrigger return_values[3] = {
enPowerUpTriggerFstp,
enPowerUpTriggerWakeUp,
enPowerUpTriggerReset
};
static int idx = 0;

MPS__tenPowerUpTrigger return_value = return_values[idx];
idx += 1;
if (idx >= 3) idx = 0;
return return_value;
}

...然后调用 MPS__vOnEnterStateAcPu1 3 次?

关于c - 使用 Google Test 测试 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56965708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com