使用 assert c++ 测试一个函数

test a function using assert c++

我想使用断言来测试gcd函数,但我不知道如何捕获异常(并防止程序崩溃)。

int gcd(int a, int b) {
if(a<0 || b<0) {
    throw "Illegal argument";
}
if(a==0 || b==0)
    return a+b;
while(a!=b) {
    if(a>b) {
        a = a - b;
    }
    else {
        b = b - a;
    }
}
return a;

}

void test_gcd() {
assert(gcd(16,24) == 8);
assert(gcd(0, 19) == 19);
try {
    gcd(5, -15);
    assert(false);
} catch (char* s) {
    assert(true);
    cout << "Illegal";
}

}

"I want to test the gcd function using assertions, but I don't know how to catch the exception (and prevent the program from crashing)."

reference documentation 所述 assert() 是一个实现定义的宏(强调我的):

#ifdef NDEBUG
#define assert(condition) ((void)0)
#else
#define assert(condition) /*implementation defined*/
#endif

If NDEBUG is not defined, then assert checks if its argument (which must have scalar type) compares equal to zero. If it does, assert outputs implementation-specific diagnostic information on the standard error output and calls std::abort. The diagnostic information is required to include the text of expression, as well as the values of the standard macros __FILE__, __LINE__, and the standard variable __func__.

因此assert()不会抛出异常你可以捕捉到。要测试您的代码并正确使用异常,您应该有如下内容,其中 expect_true() 是打印消息的内容,如果参数计算结果为 false 而不是 abort() 您的程序:

int gcd(int a, int b) {
    if(a<0 || b<0) {
        throw std::invalid_argument("a and b must be negative values");
    }
    // ...
}

#define expect_true(arg) \
        do { \
            if(!(arg)) { \
                std::cout << "Unexpected false at " \
                          << __FILE__ << ", " << __LINE__ << ", " << __func__ << ": " \
                          << #arg \
                          << std::endl; } \
        } while(false);

void test_gcd() {
    expect_true(gcd(16,24) == 8);
    expect_true(gcd(0, 19) == 19);
    bool exceptionCaught = false;
    try {
        gcd(5, -15);
    } catch (const std::invalid_argument& ex) {
        cout << "Illegal as expected" << endl;
        exceptionCaught = true;
    }
    expect_true(exceptionCaught);
}

这是一个 fully working version. And another sample 失败的测试用例。


此外,由于 assert() 将始终中止您的 test_gcd() 函数,因此进行单元测试有点乏味。我建议使用一个像样的单元测试框架,您可以在其中更好地控制测试期望和 运行 各种测试用例。例如。使用类似 Google Test 的东西(它将有 EXPECT_TRUE() 的实现)。