[英]While loop inside unit testing framework
查看Qt Test Framework的一些宏,例如QCOMPARE
,这是代码:
#define QCOMPARE(actual, expected) \
do {\
if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
return;\
} while (0)
如您所见,有一个while循环。 我也在CryEngine单元测试框架中找到了同样的东西。 我的问题很简单:是否有任何理由使用该循环,或者旧的实现中还剩下什么?
您会注意到while
条件始终为false,因此没有实际的循环。 在预处理器宏中包含块并在末尾仍然需要分号是一个常见的技巧(因此,使用宏就像使用函数一样,不要混淆某些压头)。 也就是说,
QCOMPARE(foo, bar); // <-- works
QCOMPARE(foo, bar) // <-- will not work.
这在if
和else
,where的上下文中最有用
if(something)
QCOMPARE(foo, bar);
else
do_something();
将扩展到
if(something)
do stuff() while(0);
else
do_something();
这有效,而带有块且没有循环构造的多行宏将扩展为
if(something)
{ stuff() }; // <-- if statement ends here
else // <-- and this is at best a syntax error.
do_something();
哪个没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.