我正试图为我正在制作的库编写一个测试程序。这个测试程序应该测试进入某些函数的所有可能值。代码如下:

void test_int8_smr()
{
    for (int i = INT8_MIN; i <= INT8_MAX; i++)
    {
        int8_t testval = i;
        int8_t result = from_int8_smr(to_int8_smr(testval));

        if (testval != result)
        {
            if (testval == 0x80) // This if statement gets ignored.
            {
                continue;
            }
            printf("test_int8_smr()     failed: testval = 0x%02hhX, result = 0x%02hhX\n", testval, result);
            return;
        }
    }

    printf("test_int8_smr()     succeeded for all possible values. \n");
}

输出如下:
test_int8_smr()失败:testval=0x80,result=0x00
此if语句似乎被忽略:
            if (testval == 0x80)
            {
                continue;
            }

这很令人费解。知道为什么会这样吗?怎么解决?

最佳答案

testval具有类型int8_t,因此其可能值的范围是-0x800x7f。它永远不能等于0x80,因此等式关系总是错误的,并且会受到不断折叠和死代码删除的影响。

关于c - 比较操作被完全忽略,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24984187/

10-13 08:16