Cours:ClasseTestTcpClient

De troyesGEII
Révision datée du 17 octobre 2025 à 16:44 par Bjacquot (discussion | contributions) (Page créée avec « {|style="vertical-align:middle; width:100%; text-align:left; " |- | {{boîte déroulante/début|titre=testtcpclient.h}} <source lang=cpp> #ifndef... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à : navigation, rechercher

testtcpclient.h

#ifndef TESTTCPCLIENT_H
#define TESTTCPCLIENT_H

#include <QtTest/QTest>
#include <QObject>
#include <tcpclient.h>

class TestTcpClient : public QObject
{
    Q_OBJECT
public:
    explicit TestTcpClient(QObject *parent = nullptr);
private :
    QString data;
    QDataStream dataIn;

private slots:
    void readyRead_data();
    void readyRead();

    void readClientValue(QString message);
    void readServeurValue();

    QString getTcpClientData(QTcpSocket * serveurSocket,QString message);
    QString sendTcpClientData(TcpClient &monClient,QString message);

signals:
};

#endif // TESTTCPCLIENT_H

testtcpclient.cpp

#include "testtcpclient.h"
#include "tcpclient.h"
#include <QTcpServer>

TestTcpClient::TestTcpClient(QObject *parent)
    : QObject{parent}
{}

void TestTcpClient::readyRead_data()
{
    QTest::addColumn<QString>("message");

    QTest::newRow("") << "hello";
    QTest::newRow("") << "couocu";
    QTest::newRow("") << "100";
    QTest::newRow("") << "10.4";
    QTest::newRow("") << "0.5;-0.3";

}

void TestTcpClient::readyRead()
{
    QTcpServer monServeur;
    int port=rand()%1000+9000;
    monServeur.listen(QHostAddress("127.0.0.1"),port);
    TcpClient monClient{"127.0.0.1",port};
    QTest::qWaitFor([&monServeur]() { return monServeur.hasPendingConnections(); },1000);
    QVERIFY(monServeur.hasPendingConnections());
    QTcpSocket * serveurSocket=monServeur.nextPendingConnection();

    dataIn.setDevice(serveurSocket);

    QFETCH(QString, message);

    connect(&monClient,&TcpClient::newDatas,
            this,&TestTcpClient::readClientValue);
    QCOMPARE(getTcpClientData(serveurSocket,message),message);
    disconnect(&monClient,&TcpClient::newDatas,
               this,&TestTcpClient::readClientValue);

    connect(serveurSocket,&QTcpSocket::readyRead,
            this,&TestTcpClient::readServeurValue);
    QCOMPARE(sendTcpClientData(monClient,message),message);
    disconnect(serveurSocket,&QTcpSocket::readyRead,
            this,&TestTcpClient::readServeurValue);

    dataIn.setDevice(nullptr);

}

void TestTcpClient::readClientValue(QString message)
{
    data=message;
}

void TestTcpClient::readServeurValue()
{
    dataIn.startTransaction();
    if (!dataIn.commitTransaction()) return;
    dataIn >> data;
}

QString TestTcpClient::getTcpClientData(QTcpSocket *serveurSocket, QString message)
{
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_5_0);
    out << message;
    serveurSocket->write(block);
    QTest::qWait(100);
    return data;
}

QString TestTcpClient::sendTcpClientData(TcpClient &monClient, QString message)
{
    monClient.sendData(message);
    QTest::qWait(100);
    return data;
}
//main.cpp
#include "testtcpclient.h"
QTEST_MAIN(TestTcpClient)