douguan8940 2014-04-07 23:18
浏览 14
已采纳

C - 编写自定义处理程序

I've written a custom test method in PHP to give me a custom response when running simple tests. Is there a way to implement this in C without having to re-invent the wheel?

function test($assertion, $msg = null)
{
    assert_options(ASSERT_WARNING, false);

    if(assert($assertion))
    {
        echo "PASS: {$assertion}
";
    }
    else
    {
        echo $msg, "
", "FAIL: {$assertion}
";
    }
}

My solution: (edit)

void test(bool expected, bool actual)
{
    printf((expected == actual) ? "PASS" : "FAIL");
}
  • 写回答

1条回答 默认 最新

  • doucheng7534 2014-04-07 23:58
    关注

    I think this is what you're looking for:

    void test(bool expected, bool actual, char *msg)
    {
        printf(((expected == actual) ? "PASS: %s
    " : "FAIL: %s
    "), msg);
    }
    

    It has the same test-like characteristics and it outputs a simple message in the same format as your PHP function. Note that I have not tested this and I'm not sure if the ternary in the printf actually compiles, but my guess is that it does.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?