单元测试++为什么需要使用非 const char* 的 CheckEqual() 重载
在 UnitTest++ 框架中,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 is
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());
}
In the UnitTest++ framework, in the Checks.cpp module there are 4 overloaded versions of CheckEqual. Each overload takes a different constness for expected or actual. Then all four just call CheckStringsEqual() which takes char const* for both expected and actual. I can remove the overloads and everything compiles fine but the unit tests fail when passed anything other than two const strings when the strings are declared using array syntax i.e.
char txt1[]="Hello";
Clarification
The original UnitTest++ code exhibits this behavior, not just my implementation of it. Also it's the fact that the tests fail when the overloads are removed that has me stymied.
The four overloads are:
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 is
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());
}
}
Finally here are the tests where some of them fail if you comment out all but the first 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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个
CheckEqual
函数需要一个额外的参数,并且没有默认值。我看到您的代码在没有std::string
参数的情况下调用它。因此,我希望编译器说它找不到要调用的函数。使用相同数量的参数,我不确定为什么它们会以这种方式重载。您始终可以创建一个非
const
变量const
,如果您不更改它,则应该执行此操作(CheckStringsEqual
不会更改它)。The first
CheckEqual
function takes an extra parameter and it does not have a default value. I see your code calling it without thatstd::string
parameter. Therefor, I would expect the compiler to say that it cannot find a function to call.With the same number of parameters, I am not sure why they would be
const
overloaded in that way. You can always make a non-const
variableconst
, and if you aren't changing it, you should do this (CheckStringsEqual
is not changing it).