【发布时间】:2016-05-20 12:06:45
【问题描述】:
我正在尝试测试我用 GoogleTest 编写的 dll,当我调用其中一个测试时,它会抛出这个错误:
我得出的结论是,问题在于将内存分配给向量,但我不知道如何解决这个问题,因为我对 C++ 编程还很陌生。代码如下:
#ArraysCPP11.h
#ifdef ARRAYSCP11_EXPORTS
#define ARRAYSCP11_API __declspec(dllexport)
#else
#define ARRAYSCP11_API __declspec(dllimport)
#endif
__declspec(dllexport) void removeWhiteSpaces(std::vector<std::string> v, std::vector<std::string> &output);
#ArraysCPP11.cpp
void removeWhiteSpaces(std::vector<std::string> v, std::vector<std::string> &output) { //odstranjevanje presledkov iz vector-ja (vsak drugi element je bil presledek)
for (std::vector<std::string>::iterator it = v.begin(); it != v.end(); it++) {
std::string buffer = *it;
if (isdigit(buffer[0])){;
output.push_back(*it);
}
}
}
#TestTemp.h
template<class T>
class TestTemp
{
public:
TestTemp();
void SetValue(T obj_i);
T GetValue();
bool alwaysTrue();
bool TestTemp<T>::formattingTest(std::string input, std::vector<std::string> realVector, std::vector<std::string> formattedInput);
private:
T m_Obj;
};
template<class T>
inline bool TestTemp<T>::formattingTest(std::string input, std::vector<std::string> realVector, std::vector<std::string> formattedVector) {
std::string input2 = input;
// std::vector<std::string> fResult;
std::string first;
std::string second;
bool endResult = true;
std::vector<std::string> end;
//std::vector<std::string> result = split(input2, ' ');
removeWhiteSpaces(formattedVector,end);
std::vector<std::string>::iterator yt = realVector.begin();
for (std::vector<std::string>::iterator it = end.begin(); it != end.end(); it++, yt++) {
first = *it;
second = *yt;
if (first.compare(second) != 0) {
endResult = false;
break;
}
}
return endResult;
}
#ArraysCPP11-UnitTest.cpp
struct formattingTesting{
// formattingTesting* test;
std::string start;
std::vector<std::string> endResult;
formattingTesting() {
}
explicit formattingTesting(const std::string start, const std::vector<std::string> endResult)
: start{start}, endResult{endResult}
{
}
};
struct fTest : testing::Test {
formattingTesting* test;
fTest() {
test = new formattingTesting;
}
~fTest() {
delete test;
}
};
struct format {
std::string start;
std::vector<std::string> end;
};
struct formTest : fTest, testing::WithParamInterface<format> {
formTest() {
test->start = GetParam().start;
test->endResult = GetParam().end;
}
};
TEST_P(formTest, test1) {
bool endResult = true;
TestTemp<int> TempObj;
std::string first;
std::string second;
//std::string start ("1 2 3 4 5 6 7 8 9 10");
//std::vector<std::string> end = { "1","2","3","4","5","6","7","8","9","10" };
std::vector<std::string> start2 = { "1","","2","3","4","5","6","7","8","9","10" };
std::string start = GetParam().start;
std::vector<std::string> end = GetParam().end;
bool result = TempObj.formattingTest(start,end,start2);
EXPECT_TRUE(result);
}
INSTANTIATE_TEST_CASE_P(Default, formTest, testing::Values(
format{ "1", {"1"} },
format{ " ", {} },
format{ "1 2 3 4 5",{"1","2","3","4","5"} },
format{ "1 2 3 4 5 6", {"1","2","3","4","5","6"} }
));
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
return 0;
}
【问题讨论】:
-
您的代码中有未定义的行为。
start2向量中有一个空字符串,您最终将其传递给removeWhiteSpace函数,您可以在其中访问向量中字符串的第一个字符。如果字符串为空,则它没有第一个字符,因此您的索引超出范围。removeWhiteSpace这个名字也不是很好,因为该函数实际上并没有删除空格,它只是检查字符串的第一个字符是否是数字(正如我刚刚指出的错误)。 -
@JoachimPilebog 向量
start2主要用于调试测试的比较部分(调用removeWhiteSpaces之后的代码部分),将在我修复问题后删除.此外,removeWhiteSpaces函数实际上确实删除了空白,因为它仅将数字元素推入新向量 -
顺便说一句。为什么要全部复制向量而不是通过 const 引用传递它们(例如在
removeWhiteSpaces(std::vector<std::string> v, ...)中,您只查看向量 v 所以它不需要是非常量)。 -
@axalis 好吧,根据我的理解来评论你的例子(我是一个非常完整的 C++ 初学者)我使函数工作,所以向量
v是一个输入,函数推动向量output中的数字值。我确实知道可能有更好的方法来处理这个问题,但我还没有真正找到一个,所以任何建议都会非常感激 -
重点是您可以使用
void removeWhiteSpaces(const std::vector<std::string> & v, std::vector<std::string> &output);不复制第一个向量(并在内部使用std::vector<std::string>::const_iterator而不是std::vector<std::string>::iterator)。其他一些不需要修改输入参数的函数也是类似的。