簡體   English   中英

提升單元測試不會失敗

[英]boost Unit test doesn't fail

我正在學習有關增強單元測試的知識,我很高興地發現它可以檢測內存泄漏,因此我正在對其進行測試。 我創建了以下可怕的方法:

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);  
}

我顯然在這里創建2個內存泄漏。 測試人員為什么不在乎? 我的考試通過得很成功。

我不想修改實際的代碼,就像在這里看到的那樣: http : //www.boost.org/doc/libs/1_35_0/libs/test/example/exec_mon_example.cpp

為什么我沒有收到錯誤消息?

我不確定是否要進行增強,但是要使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
}

由於泄漏,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).

可能boost使用了相同的方法來檢測內存泄漏。

RELEASE版本無法檢測到內存泄漏,因為Visual Studio的“調試堆管理器”在RELEASE版本中不起作用。 您如何看待他們為什么將其命名為“調試堆管理器”?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM