<commit_before>/** ***** QxtFileLock test ***** */
#include <QTest>
#include <QFile>
#include <QxtFileLock>
#include <QMutex>
#include <QWaitCondition>

class QxtFileLockTest: public QObject
{
    Q_OBJECT
private:
    QFile * file1;
    QFile * file2;


private slots:
    void initTestCase()
    {
        file1=new QFile("lock.file");
        file2=new QFile("lock.file");
        QVERIFY(file1->open(QIODevice::ReadWrite));
        QVERIFY(file2->open(QIODevice::ReadWrite));
    }


    ///read and write lock on the same handle
    void rw_same()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::ReadLock);
        QVERIFY(lock1.lock());
        QxtFileLock lock2(file1,0x10,20,QxtFileLock::WriteLock);
        QVERIFY(lock2.lock());
    }

    ///Trying to readlock the same region with DIFFERENT handles
    void rr_different()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file2,0x10,20,QxtFileLock::ReadLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }

    ///Trying to lock the same region with DIFFERENT handles and different locks
    void rw_different()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file2,0x10,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && !lock2.lock());
    }

    ///Trying to writelock the same region with DIFFERENT handles
    void ww_different()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::WriteLock);
        QxtFileLock lock2(file2,0x10,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && !lock2.lock());
    }

    ///Trying to writelock the different regions with DIFFERENT handles
    void ww_different_region()
    {
        QxtFileLock lock1(file1,0x10   ,20,QxtFileLock::WriteLock);
        QxtFileLock lock2(file2,0x10+21,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }

    ///different region, different handles, different locks
    void rw_different_region()
    {
        QxtFileLock lock1(file1,0x10   ,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file2,0x10+21,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }
    ///different region, same handles, different locks
    void rw_same_region()
    {
        QxtFileLock lock1(file1,0x10   ,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file1,0x10+21,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }
    void cleanupTestCase()
    {
        delete file1;
        delete file2;
    }

};
#include <QThread>
#include <qxtsignalwaiter.h>

class Q43Thread : public QThread
{
public:
    void run()
    {
        exec();
    }
}
; /// qt < 4.3 backwards compatibility

///this is a job hack, not part of the testcase, ignore it if you don't what it is
class Job : public QObject
{
    Q_OBJECT
public:

    virtual void run()=0;
    void exec(QThread * onthread)
    {
        moveToThread(onthread);
        connect(this,SIGNAL(inwrap_s()),this,SLOT(inwrap_d()));
        emit(inwrap_s());

        mutex.lock();
        QVERIFY(waiter.wait(&mutex));
        mutex.unlock();
    }
public slots:
    void inwrap_d()
    {
        run();
        emit(jobDone());
        waiter.wakeAll();
    }

signals:
    void inwrap_s();

    void jobDone();
private:
    QMutex mutex;
    QWaitCondition waiter;
};

///here is the interesting part of the job. this executes one lock on a spefic thread and asserts the result
class LockJob : public Job
{
public:
    LockJob(QxtFileLock*f,bool expectedresult)
    {
        lock =f;
    expected=expectedresult;
}
QxtFileLock*lock ;
bool expected;
virtual void run()
    {
        qDebug("locking on %p",QThread::currentThread ());
        QVERIFY(lock ->lock ()==expected);
    }
};

class QxtFileLockThreadTest : public QObject
{
    Q_OBJECT
private:
    Q43Thread t1;
    Q43Thread t2;

private slots:
    void initTestCase()
    {
        qDebug("main thread is %p",QThread::currentThread ());
        t1.start();
        QVERIFY(t1.isRunning());
        t2.start();
        QVERIFY(t2.isRunning());
    }


    ///Trying to writelock the same region twice
    void ww_same()
    {
        QFile file1("lock.file");
        QVERIFY(file1.open(QIODevice::ReadWrite));<commit_msg>trying not to reyl on isStarted beeing blocking<commit_after>/** ***** QxtFileLock test ***** */
#include <QTest>
#include <QFile>
#include <QxtFileLock>
#include <QMutex>
#include <QWaitCondition>

class QxtFileLockTest: public QObject
{
    Q_OBJECT
private:
    QFile * file1;
    QFile * file2;


private slots:
    void initTestCase()
    {
        file1=new QFile("lock.file");
        file2=new QFile("lock.file");
        QVERIFY(file1->open(QIODevice::ReadWrite));
        QVERIFY(file2->open(QIODevice::ReadWrite));
    }


    ///read and write lock on the same handle
    void rw_same()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::ReadLock);
        QVERIFY(lock1.lock());
        QxtFileLock lock2(file1,0x10,20,QxtFileLock::WriteLock);
        QVERIFY(lock2.lock());
    }

    ///Trying to readlock the same region with DIFFERENT handles
    void rr_different()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file2,0x10,20,QxtFileLock::ReadLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }

    ///Trying to lock the same region with DIFFERENT handles and different locks
    void rw_different()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file2,0x10,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && !lock2.lock());
    }

    ///Trying to writelock the same region with DIFFERENT handles
    void ww_different()
    {
        QxtFileLock lock1(file1,0x10,20,QxtFileLock::WriteLock);
        QxtFileLock lock2(file2,0x10,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && !lock2.lock());
    }

    ///Trying to writelock the different regions with DIFFERENT handles
    void ww_different_region()
    {
        QxtFileLock lock1(file1,0x10   ,20,QxtFileLock::WriteLock);
        QxtFileLock lock2(file2,0x10+21,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }

    ///different region, different handles, different locks
    void rw_different_region()
    {
        QxtFileLock lock1(file1,0x10   ,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file2,0x10+21,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }
    ///different region, same handles, different locks
    void rw_same_region()
    {
        QxtFileLock lock1(file1,0x10   ,20,QxtFileLock::ReadLock);
        QxtFileLock lock2(file1,0x10+21,20,QxtFileLock::WriteLock);
        QVERIFY(lock1.lock() && lock2.lock());
    }
    void cleanupTestCase()
    {
        delete file1;
        delete file2;
    }

};
#include <QThread>
#include <qxtsignalwaiter.h>

class Q43Thread : public QThread
{
public:
    void run()
    {
        exec();
    }
}
; /// qt < 4.3 backwards compatibility

///this is a job hack, not part of the testcase, ignore it if you don't what it is
class Job : public QObject
{
    Q_OBJECT
public:

    virtual void run()=0;
    void exec(QThread * onthread)
    {
        moveToThread(onthread);
        connect(this,SIGNAL(inwrap_s()),this,SLOT(inwrap_d()));
        emit(inwrap_s());

        mutex.lock();
        QVERIFY(waiter.wait(&mutex));
        mutex.unlock();
    }
public slots:
    void inwrap_d()
    {
        run();
        emit(jobDone());
        waiter.wakeAll();
    }

signals:
    void inwrap_s();

    void jobDone();
private:
    QMutex mutex;
    QWaitCondition waiter;
};

///here is the interesting part of the job. this executes one lock on a spefic thread and asserts the result
class LockJob : public Job
{
public:
    LockJob(QxtFileLock*f,bool expectedresult)
    {
        lock =f;
    expected=expectedresult;
}
QxtFileLock*lock ;
bool expected;
virtual void run()
    {
        qDebug("locking on %p",QThread::currentThread ());
        QVERIFY(lock ->lock ()==expected);
    }
};

class QxtFileLockThreadTest : public QObject
{
    Q_OBJECT
private:
    Q43Thread t1;
    Q43Thread t2;

private slots:
    void initTestCase()
    {
        qDebug("main thread is %p",QThread::currentThread ());

        QxtSignalWaiter w1(&t1,SIGNAL(started()));
        t1.start();
        QVERIFY(w1.wait());

        QxtSignalWaiter w2(&t2,SIGNAL(started()));
        t2.start();
        QVERIFY(w2.wait());
    }


    ///Trying to writelock the same region twice
    void ww_same()
    {
        QFile file1("lock.file");
        QVERIFY(file1.open(QIODevice::ReadWrite));
{'fraction_non_alphanumeric': 0.1281048758049678, 'fraction_numerical': 0.033348666053357866, 'mean_word_length': 2.8791257805530774, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3676231', 'n_tokens_mistral': 3127, 'n_tokens_neox': 2996, 'n_words': 614}