UnitTest ++为什么CheckEqual()重载需要非const char *

时间:2011-03-28 23:34:27

标签: c++ const const-correctness unittest++

UnitTest++ framework中,在Checks.cpp模块中有4个重载版本的CheckEqual。每个重载都对预期或实际采用不同的常量。然后所有四个只调用CheckStringsEqual(),它将char const *作为预期和实际的。我可以删除重载并且所有内容编译都很好但是当使用数组语法声明字符串时,单元测试在传递除两个const字符串以外的任何内容时失败,即

char txt1[]="Hello";

澄清 原始的UnitTest ++代码展示了这种行为,而不仅仅是我的实现。此外,当移除过载导致我受阻时,测试失败。

四个重载是:

void CheckEqual(TestResults& results, 
                char const* expected, 
                char const* actual, 
                TestDetails const& details, 
                std::string const& msg) //msg defaults to ="" in .h
{
    CheckStringsEqual(results, expected, actual, details, msg);
}

void CheckEqual(TestResults& results, 
                char* expected, 
                char* actual, 
                TestDetails const& details)
{
    CheckStringsEqual(results, expected, actual, details);
}

void CheckEqual(TestResults& results, 
                char* expected, 
                char const* actual, 
                TestDetails const& details)
{
    CheckStringsEqual(results, expected, actual, details);
}

void CheckEqual(TestResults& results, 
                char const* expected, 
                char* actual, 
                TestDetails const& details)
{
    CheckStringsEqual(results, expected, actual, details);
}

CheckStringsEqual是

    void CheckStringsEqual(TestResults& results, 
                           char const* expected, 
                           char const* actual,
                           TestDetails const& details, std::string const& msg="")
    {
        using namespace std;

        if (strcmp(expected, actual))
        {
            UnitTest::MemoryOutStream stream;
            stream << msg;
            stream << " Expected " << expected << " but was " << actual;

            results.OnTestFailure(details, stream.GetText());
        }
    }

最后,这里有一些测试,如果你注释掉除了第一个CheckEqual之后的所有部分,那么其中一些会失败。

char txt1[] = "Hello"; // non-const on purpose so no folding of duplicate data

char txt2[] = "Hello";

TEST(CheckEqualsWithStringsWorksOnContentsNonConstNonConst)  
{
    char const* const p1 = txt1;
    char const* const p2 = txt2;
    TestResults results;
    CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
    CHECK_EQUAL(0, results.GetFailureCount());
}

TEST(CheckEqualsWithStringsWorksOnContentsConstConst) 
{
    char* const p1 = txt1;
    char* const p2 = txt2;
    TestResults results;
    CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
    CHECK_EQUAL(0, results.GetFailureCount());
}

TEST(CheckEqualsWithStringsWorksOnContentsNonConstConst)
{
char* const p1 = txt1;
char const* const p2 = txt2;
TestResults results;
CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
CHECK_EQUAL(0, results.GetFailureCount());
}

TEST(CheckEqualsWithStringsWorksOnContentsConstNonConst)
{
char const* const p1 = txt1;
char* const p2 = txt2;
TestResults results;
CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
CHECK_EQUAL(0, results.GetFailureCount());
}

1 个答案:

答案 0 :(得分:1)

第一个CheckEqual函数需要一个额外的参数,它没有默认值。我看到你的代码在没有std::string参数的情况下调用它。因此,我希望编译器说它找不到要调用的函数。

使用相同数量的参数,我不确定为什么它们会const以这种方式重载。您始终可以制作非const变量const,如果您不进行更改,则应执行此操作(CheckStringsEqual不会更改它。)