应用程序中出现奇怪的执行错误
我正在编写一个单元测试,它似乎存在某种指针问题。 基本上,它正在测试一个类,该类一旦构造完毕,就会返回有关文件的信息。 如果检测到所有预期的文件,则测试运行正确。 如果预期的文件多于检测到的文件,则例程会正确报告错误。 但是,如果检测到的文件多于预期,可执行文件就会崩溃。 这很难遵循,因为当我尝试使用调试器单步执行时,当前代码点会跳过整个方法 - 它不会像您期望的那样逐行遵循。
关于我做错了什么有什么想法吗?
这是我的代码:
#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()
谢谢!
Billy3
我使用以下代码解决了该问题:
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
}
I've got a unit test I'm writing which seems to have some kind of a pointer problem. Basically, it's testing a class that, once constructed, returns information about a file. If all the files expected are detected, then the test operates correctly. If there are more files expected than detected, then the routine correctly reports error. But if there are more files detected than expected, the executable crashes. This has been difficult to follow because when I try to step through with the debugger, the current code point jumps all over the method -- it doesn't follow line by line like you'd expect.
Any ideas as to what I'm doing incorrectly?
Here's my code:
#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()
Thanks!
Billy3
I solved the problem using the following code instead:
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果文件意外出现,您认为这会返回什么? 看起来您希望它是一个有效的文件名。
事实上,它是一个位于数组末尾的迭代器 - 您不能将其视为有效指针:
正常情况下,您将结果与
end()
的值进行比较(固定):请参阅在这里的示例中,
lower_bound
将返回-then-end 值:您无法安全地取消引用结束值,但您可以将其用于比较或作为插入点。
你为什么要使用
lower_bound
? 出于搜索的目的,您肯定应该使用find
吗? 如果您使用lower_bound
,它将返回一个可以插入文件名的位置,但不一定等于您要查找的文件名。 换句话说,您不仅需要与结束值进行比较,还需要与文件名进行比较,以防它返回有效的内容。这是使用
find
的版本。 正如您所看到的,它比上面的固定版本更简单。What do you think this will return, in the event that the file was unexpected? It looks like you expect it to be a valid file name.
It will in fact be an iterator at one past the end of the array - you cannot treat it as a valid pointer:
Normall, you compare the result with the value of
end()
(fixed):See in this example here,
lower_bound
will return off-then-end values:You cannot safely dereference the off-the-end value, but you can use it for the purpose of comparison, or as an insertion point.
Why are you using
lower_bound
anyway? For the purposes of search, surely you should usefind
? If you are usinglower_bound
, it will return a position where the file name could be inserted, but not necessarily equal to the file name you are looking for. In other words, you not only need to compare with the off-the-end value, but also with the file name, in case it returned something valid.Here is a version that uses
find
. As you can see it is simpler than the fixed version above.这通常意味着您一次有超过 1 个线程调用该方法。当我的 aspx 页面上有超过 1 个对象同时导致回发时,我通常会看到这种情况。
This usually means you have more than 1 thread hitting the method at a time. I usually see this when more than 1 object on my aspx page causes a postback at the same time.
“当前代码点跳过整个方法 - 它不像您期望的那样逐行执行”
检查您的编译器选项! 你是否开启了调试并关闭了优化?
优化编译器通常会完全“重写”您的代码,改变指令的顺序,
展开循环等,使得调试器难以将可执行指令映射到原始代码行。
"the current code point jumps all over the method -- it doesn't follow line by line like you'd expect"
Check your compiler options! Have you got debug on and optimise off?
Optimising compilers often completly "rewrite" your code changing the order of instructions,
unrolling loops etc. etc. so that the debugger has difficulty in mapping an executable instruction to the original line of code.