我正在编写一个单元测试,它似乎有某种指针问题。基本上,它正在测试一个类,该类一旦构建,就会返回有关文件的信息。如果检测到所有预期的文件,则测试正常运行。如果预期的文件多于检测到的文件,则例程会正确报告错误。但是,如果检测到的文件多于预期,则可执行文件会崩溃。这很难遵循,因为当我尝试使用调试器单步执行时,当前代码点会在整个方法中跳转——它不会像您期望的那样逐行遵循。
关于我做错了什么的任何想法?
这是我的代码:
#include "stdafx.h"
#include "boostUnitTest.h"
#include "../pevFind/fileData.h" //Header file for tested code.
#include "../pevFind/stringUtil.h" //convertUnicode()
struct fileDataLoadingFixture
{
std::vector<fileData> testSuiteCandidates;
fileDataLoadingFixture()
{
WIN32_FIND_DATA curFileData;
HANDLE fData = FindFirstFile(L"testFiles\\*", &curFileData);
if (fData == INVALID_HANDLE_VALUE)
throw std::runtime_error("Failed to load file list!");
do {
if(boost::algorithm::equals(curFileData.cFileName, L".")) continue;
if(boost::algorithm::equals(curFileData.cFileName, L"..")) continue;
fileData theFile(curFileData, L".\\testFiles\\");
testSuiteCandidates.push_back(theFile);
} while (FindNextFile(fData, &curFileData));
FindClose(fData);
};
};
BOOST_FIXTURE_TEST_SUITE( fileDataTests, fileDataLoadingFixture )
BOOST_AUTO_TEST_CASE( testPEData )
{
std::vector<std::wstring> expectedResults;
expectedResults.push_back(L"a.cfexe");
expectedResults.push_back(L"b.cfexe");
//More files....
expectedResults.push_back(L"c.cfexe");
std::sort(expectedResults.begin(), expectedResults.end());
for (std::vector<fileData>::const_iterator it = testSuiteCandidates.begin(); it != testSuiteCandidates.end(); it++)
{
if (it->isPE())
{
std::wstring theFileString(it->getFileName().substr(it->getFileName().find_last_of(L'\\') + 1 ));
std::vector<std::wstring>::const_iterator target = std::lower_bound(expectedResults.begin(), expectedResults.end(), theFileString);
BOOST_REQUIRE_MESSAGE(*target == theFileString, std::string("The file ") + convertUnicode(theFileString) + " was unexpected." );
if (*target == theFileString)
{
expectedResults.erase(target);
}
}
}
BOOST_CHECK_MESSAGE(expectedResults.size() == 0, "Some expected results were not found." );
}
BOOST_AUTO_TEST_SUITE_END()
谢谢!
比利3
我使用以下代码解决了这个问题:
BOOST_AUTO_TEST_CASE( testPEData )
{
std::vector<std::wstring> expectedResults;
expectedResults.push_back(L"blah.cfexe");
//files
expectedResults.push_back(L"tail.cfexe");
expectedResults.push_back(L"zip.cfexe");
std::vector<std::wstring> actualResults;
for(std::vector<fileData>::const_iterator it = testSuiteCandidates.begin(); it != testSuiteCandidates.end(); it++)
{
if (it->isPE()) actualResults.push_back(it->getFileName().substr(it->getFileName().find_last_of(L'\\') + 1 ));
}
std::sort(expectedResults.begin(), expectedResults.end());
std::sort(actualResults.begin(), actualResults.end());
std::vector<std::wstring> missed;
std::set_difference(expectedResults.begin(), expectedResults.end(), actualResults.begin(), actualResults.end(), std::back_inserter(missed));
std::vector<std::wstring> incorrect;
std::set_difference(actualResults.begin(), actualResults.end(), expectedResults.begin(), expectedResults.end(), std::back_inserter(incorrect));
for(std::vector<std::wstring>::const_iterator it = missed.begin(); it != missed.end(); it++)
{
BOOST_ERROR(std::string("The file ") + convertUnicode(*it) + " was expected but not returned.");
}
for(std::vector<std::wstring>::const_iterator it = incorrect.begin(); it != incorrect.end(); it++)
{
BOOST_ERROR(std::string("The file ") + convertUnicode(*it) + " was returned but not expected.");
}
BOOST_CHECK(true); //Suppress commandline "No assertions" warning
}