forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87c302c
commit ae7dbd4
Showing
2 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |