Valgrind: Move test object creation closer to tested code

Change-Id: I896c7f7c1d99a62bc869f3511c036123a78afc29
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2024-01-19 17:37:27 +01:00
parent eea8f42ea4
commit cd4f62ebf8
5 changed files with 106 additions and 97 deletions

View File

@@ -11,10 +11,13 @@
#include "xmlprotocol/status.h"
#include "xmlprotocol/suppression.h"
#include <utils/process.h>
#include <utils/processinterface.h>
#include <QFileInfo>
#include <QStringList>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTest>
using namespace Utils;
@@ -24,14 +27,14 @@ QT_BEGIN_NAMESPACE
namespace QTest {
template<>
inline bool qCompare(int const &t1, MemcheckError const &t2,
char const *actual, char const *expected, char const *file, int line)
bool qCompare(int const &t1, MemcheckError const &t2,
char const *actual, char const *expected, char const *file, int line)
{
return qCompare(t1, int(t2), actual, expected, file, line);
}
inline bool qCompare(const QString &t1, char const *t2,
char const *actual, char const *expected, char const *file, int line)
bool qCompare(const QString &t1, char const *t2,
char const *actual, char const *expected, char const *file, int line)
{
return qCompare(t1, QString::fromLatin1(t2), actual, expected, file, line);
}
@@ -39,7 +42,37 @@ inline bool qCompare(const QString &t1, char const *t2,
} // namespace QTest
QT_END_NAMESPACE
namespace Valgrind::Test {
namespace Valgrind::Internal {
class ValgrindMemcheckParserTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanup();
void testMemcheckSample1();
void testMemcheckSample2();
void testMemcheckSample3();
void testMemcheckCharm();
void testHelgrindSample1();
void testValgrindCrash();
void testValgrindGarbage();
void testParserStop();
void testRealValgrind();
void testValgrindStartError_data();
void testValgrindStartError();
private:
void initTest(const QString &testfile, const QStringList &otherArgs = {});
QTcpServer *m_server = nullptr;
std::unique_ptr<Utils::Process> m_process;
std::unique_ptr<QTcpSocket> m_socket;
};
static void dumpError(const Error &e)
{
@@ -555,4 +588,11 @@ void ValgrindMemcheckParserTest::testValgrindStartError()
// just finish without deadlock and we are fine
}
} // namespace Valgrind::Test
QObject *createValgrindMemcheckParserTest()
{
return new ValgrindMemcheckParserTest;
}
} // namespace Valgrind::Internal
#include "valgrindmemcheckparsertest.moc"

View File

@@ -3,45 +3,10 @@
#pragma once
#include <utils/process.h>
#include <QObject>
#include <QStringList>
#include <QTcpSocket>
namespace Valgrind::Internal {
QT_BEGIN_NAMESPACE
class QTcpServer;
QT_END_NAMESPACE
QObject *createValgrindMemcheckParserTest();
namespace Valgrind::Test {
class ValgrindMemcheckParserTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanup();
void testMemcheckSample1();
void testMemcheckSample2();
void testMemcheckSample3();
void testMemcheckCharm();
void testHelgrindSample1();
void testValgrindCrash();
void testValgrindGarbage();
void testParserStop();
void testRealValgrind();
void testValgrindStartError_data();
void testValgrindStartError();
private:
void initTest(const QString &testfile, const QStringList &otherArgs = {});
QTcpServer *m_server = nullptr;
std::unique_ptr<Utils::Process> m_process;
std::unique_ptr<QTcpSocket> m_socket;
};
} // namespace Valgrind::Test
} // Valgrind::Internal

View File

@@ -54,8 +54,8 @@ public:
RunConfiguration::registerAspect<ValgrindRunConfigurationAspect>();
#ifdef WITH_TESTS
addTest<Test::ValgrindMemcheckParserTest>();
addTest<Test::ValgrindTestRunnerTest>();
addTestCreator(createValgrindMemcheckParserTest);
addTestCreator(createValgrindTestRunnerTest);
#endif
}
};

View File

@@ -4,6 +4,7 @@
#include "valgrindtestrunnertest.h"
#include "valgrindprocess.h"
#include "xmlprotocol/error.h"
#include "xmlprotocol/frame.h"
#include "xmlprotocol/stack.h"
@@ -12,6 +13,7 @@
#include <QDebug>
#include <QDir>
#include <QStringList>
#include <QTest>
#define HEADER_LENGTH 3
@@ -19,9 +21,7 @@
using namespace Valgrind::XmlProtocol;
using namespace Utils;
namespace Valgrind::Test {
//BEGIN Test Helpers and boilerplate code
namespace Valgrind::Internal {
static const QString appSrcDir(TESTRUNNER_SRC_DIR);
static const QString appBinDir(TESTRUNNER_APP_DIR);
@@ -36,6 +36,46 @@ static QString srcDirForApp(const QString &app)
return QDir::cleanPath(appSrcDir + '/' + app);
}
class ValgrindTestRunnerTest : public QObject
{
Q_OBJECT
public:
explicit ValgrindTestRunnerTest(QObject *parent = nullptr);
private slots:
void init();
void cleanup();
void testLeak1();
void testLeak2();
void testLeak3();
void testLeak4();
void testUninit1();
void testUninit2();
void testUninit3();
void testFree1();
void testFree2();
void testInvalidjump();
void testSyscall();
void testOverlap();
void logMessageReceived(const QByteArray &message);
void internalError(const QString &error);
void error(const Valgrind::XmlProtocol::Error &error);
private:
QString runTestBinary(const QString &binary, const QStringList &vArgs = QStringList());
ValgrindProcess *m_runner = nullptr;
QList<QByteArray> m_logMessages;
QList<XmlProtocol::Error> m_errors;
bool m_expectCrash = false;
};
ValgrindTestRunnerTest::ValgrindTestRunnerTest(QObject *parent)
: QObject(parent)
{
@@ -749,4 +789,11 @@ void ValgrindTestRunnerTest::testOverlap()
}
}
} // namespace Valgrind::Test
QObject *createValgrindTestRunnerTest()
{
return new ValgrindTestRunnerTest;
}
} // namespace Valgrind::Internal
#include "valgrindtestrunnertest.moc"

View File

@@ -3,53 +3,10 @@
#pragma once
#include "xmlprotocol/error.h"
#include <QObject>
#include <QStringList>
namespace Valgrind { class ValgrindProcess; }
namespace Valgrind::Internal {
namespace Valgrind::Test {
QObject *createValgrindTestRunnerTest();
class ValgrindTestRunnerTest : public QObject
{
Q_OBJECT
public:
explicit ValgrindTestRunnerTest(QObject *parent = nullptr);
private slots:
void init();
void cleanup();
void testLeak1();
void testLeak2();
void testLeak3();
void testLeak4();
void testUninit1();
void testUninit2();
void testUninit3();
void testFree1();
void testFree2();
void testInvalidjump();
void testSyscall();
void testOverlap();
void logMessageReceived(const QByteArray &message);
void internalError(const QString &error);
void error(const Valgrind::XmlProtocol::Error &error);
private:
QString runTestBinary(const QString &binary, const QStringList &vArgs = QStringList());
ValgrindProcess *m_runner = nullptr;
QList<QByteArray> m_logMessages;
QList<XmlProtocol::Error> m_errors;
bool m_expectCrash = false;
};
} // namespace Valgrind::Test
} // Valgrind::Internal