简体   繁体   English

提升单元测试不会失败

[英]boost Unit test doesn't fail

I am learning about boost unit tests, I found out, happily, that it can detect memory leaks, so I am testing it. 我正在学习有关增强单元测试的知识,我很高兴地发现它可以检测内存泄漏,因此我正在对其进行测试。 I created the following horrible method: 我创建了以下可怕的方法:

int ForTest::Compare(const ForTest item)
{
    ForTest* existing_item = this;
    char* x=new char[1024];
    m_name = std::string(x);
    if (existing_item->m_count * existing_item->m_price == item.m_count * item.m_price) return 0;
    if (existing_item->m_count * existing_item->m_price > item.m_count * item.m_price) return 1;    
    return -1;
}
BOOST_AUTO_TEST_CASE( a_test_case)
{
    BOOST_TEST_CHECKPOINT("weird...");

    ForTest alpha("Pen", 4, 4.3);
    ForTest beta;

    BOOST_CHECK_EQUAL(alpha.Compare(beta), 1);  
}

I am obviously creating 2 memory leaks here. 我显然在这里创建2个内存泄漏。 Why doesn't the tester care ? 测试人员为什么不在乎? My test passes with flying colors. 我的考试通过得很成功。

I don't want to have to modify actual code, as I saw here: http://www.boost.org/doc/libs/1_35_0/libs/test/example/exec_mon_example.cpp 我不想修改实际的代码,就像在这里看到的那样: http : //www.boost.org/doc/libs/1_35_0/libs/test/example/exec_mon_example.cpp

Why am I not getting an error ? 为什么我没有收到错误消息?

I am not sure about boost but to get debug heap manager of Visual Studio to work you have to write something like that: 我不确定是否要进行增强,但是要使Visual Studio的调试堆管理器正常工作,您必须编写如下内容:

#include <crtdbg.h>

#ifdef _DEBUG
static char THIS_FILE[] = __FILE__;
#define new new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
#endif

int main()
{
    _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) );
    new int(2036427631); // deliberate leak
}

Since it leaks the output of the DEBUG version will look something like that: 由于泄漏,DEBUG版本的输出将类似于以下内容:

Detected memory leaks!
Dumping objects ->
d:\fun\try\try.cpp(11) : {66} normal block at 0x00345C40, 4 bytes long.
 Data: <okay> 6F 6B 61 79 
Object dump complete.
The program '[3216] try.exe: Native' has exited with code 0 (0x0).

Probably boost uses very same thing to detect the memory leaks. 可能boost使用了相同的方法来检测内存泄漏。

The RELEASE version does not detect memory leaks because the "debug heap manager" of Visual Studio does not work in RELEASE version. RELEASE版本无法检测到内存泄漏,因为Visual Studio的“调试堆管理器”在RELEASE版本中不起作用。 What you think why they named it as "debug heap manager"? 您如何看待他们为什么将其命名为“调试堆管理器”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM