微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为三元变量编写谷歌测试?

如何解决如何为三元变量编写谷歌测试?

我正在检查一些配置条件以返回类型值。这是一段函数代码

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) )
    // ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。