问题描述
typdef enum{
type1 = 0,type2,type3,type4,type5
}
#define config (uint8) ((type1_enble()) ? (type1) :
(((type2_enable())? (type2) :
((type3_enable())? (type3):
(type4_enable())?(type4):(type5)))))
int process_input(void)
{
int result = type1;
if( (config == type1) || (config == type2) )
{
result = type1;
}
else if(config == type3)
{
result = type3;
}
else
{
/**/
}
return result;
}
//这是输入集
make_tuple(0,0)
make_tuple(0,1,1)
make_tuple(1,1)
//测试代码
TEST_P(test_config,test1)
{
ON_CALL(mock,type1_enble()).WillByDefault(input1);
ON_CALL(mock,type2_enble()).WillByDefault(input2);
ON_CALL(mock,type3_enble()).WillByDefault(input3);
ON_CALL(mock,type4_enble()).WillByDefault(input4);
process_input();
}
但在这里我不知道如何充分覆盖代码。尤其是三元变量。当我运行测试用例时,我没有获得 100% 的代码覆盖率。这里有什么问题,尤其是三元变量?
解决方法
每次扩展 config
宏时,该复合表达式的每个分支都会注入代码中,这意味着大约有 125 个不同的代码路径通过该函数。
因此,要完全覆盖所编写的函数,您需要查看 typex_enble()
的所有排列以进行重复调用。即您需要考虑 type1_enble()
在第一次调用时返回 0,在第二次调用时返回 1。
如果重复调用 typex_enble()
总是返回相同的值,那么您应该只计算一次表达式:
int process_input(void)
{
auto cfg = config;
int result = type1;
if( (cfg == type1) || (cfg== type2) )
// ...