【发布时间】:2018-11-28 08:17:42
【问题描述】:
我正在尝试在 Google Test 中加载测试序列。我有几个测试序列,所以我正在尝试进行参数化测试,将目录带到测试序列(多个文件),但是当我尝试释放资源时,我在析构函数中遇到分段错误。
// Test sequence class
class TestSequence {
public:
TestSequence(const std::string& dir)
: TestSequence(dir + "/Values1.csv", dir + "/Values2.csv",
dir + "/Values3.csv") {}
TestSequence(const std::string& val1_file, const std::string& val2_file,
const std::string& val3_file)
: val1_file_path(val1_file),
val2_file_path(val2_file),
val3_file_path(val3_file) {
mp_val1_file = new std::ifstream(m_val1_file_path);
mp_val2_file = new std::ifstream(m_val2_file_path);
mp_val3_file = new std::ifstream(m_val3_file_path);
}
virtual ~TestSequence() {
delete mp_flows_file; // <- Segmentation fault
delete mp_pres_file;
delete mp_params_file;
}
bool NextValue(MyValueType * p_value) {
// Do some parsing on the file
...
}
private:
std::string val1_file_path;
std::string val2_file_path;
std::string val3_file_path;
std::ifstream *mp_val1_file;
std::ifstream *mp_val1_file;
std::ifstream *mp_val1_file;
}
// Test case class
class AlgorithmTests
: public testing::TestWithParam<TestSequence> {
protected:
// Unit under test, mocks, etc...
public:
VentilationDetectionAlgorithmTests(void) {
// Setting up unit under tests...
}
};
// Instantiate parameterised tests with paths to directories
INSTANTIATE_TEST_CASE_P(
SomeSequences, AlgorithmTests,
::testing::Values(TestSequence("test/support/sequence1"),
TestSequence("test/support/sequence2")));
我写了两个测试。我在测试序列的构造函数和析构函数以及每个测试的第一行添加了一个断点。结果如下:
- 为每个目录调用一次序列构造函数(预期)
- 为每个目录调用一次序列析构函数,以相反的顺序(意外)
- 在最后一个目录上再次调用序列析构函数(
delete上的分段错误)
从未达到测试。
- 我尝试在删除变量后将其设置为
nullptr,并在删除前检查它,但没有帮助。 - 如果我替换指向
ifstreams 的指针,我会收到编译错误(错误:调用 'TestSequence' 的隐式删除的复制构造函数)
我认为我误解了 Google Test 如何使用创建的参数,或者我应该如何处理 C++ 中的资源。
感谢您对此的任何意见!
堆栈跟踪:
test.out!TestSequence::~TestSequence()
(/path/to/project/test/test_Algorithm.cpp:60)
test.out!TestSequence::~TestSequence() (/path/to/project/test/test_Algorithm.cpp:58)
test.out!testing::internal::ValueArray2<TestSequence, TestSequence>::operator testing::internal::ParamGenerator<TestSequence><TestSequence>() const (/path/to/project/vendor/googletest/include/gtest/internal/gtest-param-util-generated.h:103)
test.out!gtest_LongSequencesAlgorithmTests_EvalGenerator_() (/path/to/project/test/test_Algorithm.cpp:170)
test.out!testing::internal::ParameterizedTestCaseInfo<AlgorithmTests>::RegisterTests() (/path/to/project/vendor/googletest/include/gtest/internal/gtest-param-util.h:554)
test.out!testing::internal::ParameterizedTestCaseRegistry::RegisterTests() (/path/to/project/vendor/googletest/include/gtest/internal/gtest-param-util.h:714)
test.out!testing::internal::UnitTestImpl::RegisterParameterizedTests() (/path/to/project/vendor/googletest/src/gtest.cc:2620)
test.out!testing::internal::UnitTestImpl::PostFlagParsingInit() (/path/to/project/vendor/googletest/src/gtest.cc:4454)
test.out!void testing::internal::InitGoogleTestImpl<char>(int*, char**) (/path/to/project/vendor/googletest/src/gtest.cc:5356)
test.out!testing::InitGoogleTest(int*, char**) (/path/to/project/vendor/googletest/src/gtest.cc:5374)
test.out!void testing::internal::InitGoogleMockImpl<char>(int*, char**) (/path/to/project/vendor/googlemock/src/gmock.cc:131)
test.out!testing::InitGoogleMock(int*, char**) (/path/to/project/vendor/googlemock/src/gmock.cc:174)
test.out!main (/path/to/project/test/test_Main.cpp:13)
libdyld.dylib!start (Unknown Source:0)
libdyld.dylib!start (Unknown Source:0)
【问题讨论】:
标签: c++ googletest