I'm trying to load a test sequence in Google Test. I have several test sequences, so I'm trying to make a parameterised test that takes the directory to the test sequence (multiple files), but I'm getting segmentation faults in the destructor when I try to free the resources.
// 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")));
I have two tests written. I've added a breakpoint to the constructor and destructor of the test sequence and at the first line of each test. This results in the following:
- Sequence constructor is called once for each directory (expected)
- Sequence destructor is called once for each directory, in reverse order (unexpected)
- Sequence destructor is called again on the last directory (segmentation fault on
delete
)
Test is never reached.
- I have tried setting the variable to
nullptr
after deleting it and checking for it before deleting it, but didn't help. - If I replace the pointers to the
ifstream
s, I get compiling error (error: call to implicitly-deleted copy constructor of 'TestSequence')
I assume there is something I've misunderstood about either how Google Test is using the created parameters, or how I'm supposed to deal with the resources in C++.
Grateful for any input on this!
The stack trace:
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)