From 9a87cb5347a888a104cab47c17f05f1f0aa7a133 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 22 Mar 2010 17:34:13 +0100 Subject: Use function binding generator from QtConcurrent. * Copy tools/codegenerator and tools/generatebuild from QtConcurrent and adapt for Coroutines. This allows coroutines from various callables to be created with a call to build(...). * Add initial tests. * Fix incorrect use of QThreadStorage. * Fix severe typo in Coroutine::yieldHelper. --- tests/auto/auto.pro | 2 ++ tests/auto/basic/basic.pro | 3 +++ tests/auto/basic/tst_basic.cpp | 37 +++++++++++++++++++++++++++ tests/auto/build/build.pro | 3 +++ tests/auto/build/tst_build.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++ tests/tests.pro | 2 ++ 6 files changed, 105 insertions(+) create mode 100644 tests/auto/auto.pro create mode 100644 tests/auto/basic/basic.pro create mode 100644 tests/auto/basic/tst_basic.cpp create mode 100644 tests/auto/build/build.pro create mode 100644 tests/auto/build/tst_build.cpp create mode 100644 tests/tests.pro (limited to 'tests') diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro new file mode 100644 index 0000000..675ee12 --- /dev/null +++ b/tests/auto/auto.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS = basic build diff --git a/tests/auto/basic/basic.pro b/tests/auto/basic/basic.pro new file mode 100644 index 0000000..c0d2002 --- /dev/null +++ b/tests/auto/basic/basic.pro @@ -0,0 +1,3 @@ +QT += testlib +SOURCES += tst_basic.cpp +include(../../../use_coroutine.pri) diff --git a/tests/auto/basic/tst_basic.cpp b/tests/auto/basic/tst_basic.cpp new file mode 100644 index 0000000..c33ef40 --- /dev/null +++ b/tests/auto/basic/tst_basic.cpp @@ -0,0 +1,37 @@ +#include +#include + +class tst_basic : public QObject +{ +Q_OBJECT + +private slots: + void noYield(); +}; + +class NoYieldCoro : public Coroutine +{ +public: + virtual void run() + { + i++; + QCOMPARE(status(), Running); + QCOMPARE(currentCoroutine(), this); + } + + int i; +}; + +void tst_basic::noYield() +{ + NoYieldCoro coro; + coro.i = 0; + QCOMPARE(coro.status(), Coroutine::NotStarted); + QCOMPARE(coro.cont(), false); + QCOMPARE(coro.i, 1); + QCOMPARE(coro.status(), Coroutine::Terminated); + Q_ASSERT(Coroutine::currentCoroutine() != &coro); +} + +QTEST_MAIN(tst_basic) +#include "tst_basic.moc" diff --git a/tests/auto/build/build.pro b/tests/auto/build/build.pro new file mode 100644 index 0000000..9b60fc4 --- /dev/null +++ b/tests/auto/build/build.pro @@ -0,0 +1,3 @@ +QT += testlib +SOURCES += tst_build.cpp +include(../../../use_coroutine.pri) diff --git a/tests/auto/build/tst_build.cpp b/tests/auto/build/tst_build.cpp new file mode 100644 index 0000000..c557d34 --- /dev/null +++ b/tests/auto/build/tst_build.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +class tst_build: public QObject +{ +Q_OBJECT + +private slots: + void staticFn(); +}; + +static int fnCounter = -99; +static void fnNoArg() +{ + fnCounter = 0; + Coroutine::yield(); + fnCounter++; + Coroutine::yield(); + fnCounter++; + +} + +static void fnArg(int start) +{ + fnCounter = start; + Coroutine::yield(); + fnCounter++; + Coroutine::yield(); + fnCounter++; +} + + +void tst_build::staticFn() +{ + Coroutine* c1 = build(32000, &fnNoArg); + QCOMPARE(fnCounter, -99); + QCOMPARE(c1->cont(), true); + QCOMPARE(fnCounter, 0); + QCOMPARE(c1->cont(), true); + QCOMPARE(fnCounter, 1); + QCOMPARE(c1->cont(), false); + QCOMPARE(fnCounter, 2); + delete c1; + + Coroutine* c2 = build(32000, &fnArg, 40); + QCOMPARE(c2->cont(), true); + QCOMPARE(fnCounter, 40); + QCOMPARE(c2->cont(), true); + QCOMPARE(fnCounter, 41); + QCOMPARE(c2->cont(), false); + QCOMPARE(fnCounter, 42); + delete c2; +} + + +QTEST_MAIN(tst_build) +#include "tst_build.moc" diff --git a/tests/tests.pro b/tests/tests.pro new file mode 100644 index 0000000..7fbc8a9 --- /dev/null +++ b/tests/tests.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS = auto -- cgit v1.2.3