Skip to content

Commit

Permalink
added test for lock free queue
Browse files Browse the repository at this point in the history
  • Loading branch information
canpadawan committed Jun 15, 2016
1 parent 87c302c commit ae7dbd4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 10 deletions.
16 changes: 10 additions & 6 deletions test/test.pro
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
QT += core
QT += widgets testlib
QT -= gui

CONFIG += c++11

TARGET = test
CONFIG += console
CONFIG -= app_bundle
INCLUDEPATH += ../utils

TEMPLATE = app
SOURCES += \
tst_lfqueue.cpp

SOURCES += main.cpp

#HEADERS += \
# ../utils/lfqueue.h

target.path= .
INSTALLS += target
98 changes: 94 additions & 4 deletions test/tst_lfqueue.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,98 @@
#include <QCoreApplication>
#include <QtTest/QtTest>
#include <QtConcurrent/qtconcurrentrun.h>

int main(int argc, char *argv[])
#include "lfqueue.h"

class TestLFQueue: public QObject
{
Q_OBJECT
private:

private slots:
void setSize_data();
void setSize();
void exchange_data();
void exchange();
};


void TestLFQueue::setSize_data()
{
QTest::addColumn<int>("size");
QTest::addColumn<bool>("result");

QTest::newRow("-1") << -1 << false;
QTest::newRow("0") << 0 << true;
QTest::newRow("10") << 10 << true;
QTest::newRow("2000") << 20000 << true;

}


void TestLFQueue::setSize()
{
QFETCH(int, size);
QFETCH(bool, result);

LFQueue<void*> queue;
QCOMPARE(queue.setSize(size), result);
}


void readerThread(LFQueue<int>* pQueue_p, int pSize, bool pSleep) {
int* val_p;

for(int i=0; i<pSize ; i++) {
while(! (val_p = pQueue_p->peek()) );
QVERIFY(val_p);

QCOMPARE(*val_p, i);
pQueue_p->dequeue();

if(pSleep)
QThread::msleep(1);
}
}


void TestLFQueue::exchange_data()
{
QCoreApplication a(argc, argv);
QTest::addColumn<int>("size");
QTest::addColumn<bool>("writerSleep");
QTest::addColumn<bool>("readerSleep");

return a.exec();
QTest::newRow("nosleep") << 1000 << false << false;
QTest::newRow("readersleep") << 1000 << false << true;
QTest::newRow("writersleep") << 1000 << true << false;
}


void TestLFQueue::exchange()
{
LFQueue<int> queue;
QFETCH(int, size);
QFETCH(bool, writerSleep);
QFETCH(bool, readerSleep);

int* val_p;
QCOMPARE(queue.setSize(2), true);

QFuture<void> thread = QtConcurrent::run(readerThread, &queue, size, readerSleep);

for(int i=0; i<size ; i++) {
while(! (val_p = queue.get()) );
QVERIFY(val_p);

*val_p = i;
queue.queue();

if(writerSleep)
QThread::msleep(1);
}

thread.waitForFinished();
}


QTEST_MAIN(TestLFQueue)
#include "tst_lfqueue.moc"

0 comments on commit ae7dbd4

Please sign in to comment.