QTest and std::unique_ptr is a bit confusing
-
wrote on 22 Jan 2023, 18:00 last edited by
Hello together,
i'll try to implement a QTest on an function, which expects two std::unique_ptr
bool find(FindSpecifier specifier, const QString &stream, std::unique_ptr<int> &startAt, std::unique_ptr<int> &endAt);
This function search in a cpp file for ctor/dtor or a function and returns the position of the start and end.
So i have implement a testcase for this functionvoid TestClassparser::testFind() { ClassParser cp; std::unique_ptr<int> start(new int(0)), end(new int(0)); bool state = cp.find(ClassParser::FindSpecifier::ctor, m_stream, start, end); QVERIFY(state); QCOMPARE(*start, 3); QCOMPARE(*end, 8); state = cp.find(ClassParser::FindSpecifier::dtor, m_stream, start, end); QVERIFY(state); QCOMPARE(*start, 22); QCOMPARE(*end, 25); }
Now i'm a bit confuced because the test fails. Qt says, that the start value for "dtor" is still 3.
But in the debugger everything works perfectly and the expected values come back.
Can you tell me why this is not the case in the specific test case?
Even if I split the two passages into two different functions, the test still fails. I do not understand this.void TestClassparser::testCtor() { ClassParser cp; std::unique_ptr<int> start(new int(0)), end(new int(0)); bool state = cp.find(ClassParser::FindSpecifier::ctor, m_stream, start, end); QVERIFY(state); QCOMPARE(*start, 3); QCOMPARE(*end, 8); } void TestClassparser::testDtor() { ClassParser cp; std::unique_ptr<int> start(new int(0)), end(new int(0)); bool state = cp.find(ClassParser::FindSpecifier::dtor, m_stream, start, end); QVERIFY(state); QCOMPARE(*start, 22); // test failed with "compare values not the same" -> *start = 3 QCOMPARE(*end, 25); }
It is also strange that in this case the value of StartAt is still 3.
-
I don't see what QtTest can do here - I would guess your ClassParser function does not work and the unittest revealed it.
-
wrote on 22 Jan 2023, 20:16 last edited by
Perhaps your
find()
function makes the assumption thatstart
andend
are zero at invocation and behaves oddly if that is not the case.As the for the split test case still returning 3, this is quite possibly because you are still running the original test executable. Did you ensure you have a clean build of the test cases?
1/3