From 6f50addc1cf7e61604d1adb500288700ab59df86 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 31 Aug 2023 20:10:31 +0200 Subject: [PATCH] TaskTree: Add a test for Storage input/output StorageIO struct serves for an input/output interface to the recipe. This makes the recipe even more universal and reusable. Change-Id: Ifbd27452d2838e890eadb5e72e7e7c934c4ba840 Reviewed-by: Reviewed-by: hjk --- tests/auto/solutions/tasking/tst_tasking.cpp | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/auto/solutions/tasking/tst_tasking.cpp b/tests/auto/solutions/tasking/tst_tasking.cpp index 707e1f1b42d..cd476670c8f 100644 --- a/tests/auto/solutions/tasking/tst_tasking.cpp +++ b/tests/auto/solutions/tasking/tst_tasking.cpp @@ -67,6 +67,8 @@ private slots: void validConstructs(); // compile test void testTree_data(); void testTree(); + void storageIO_data(); + void storageIO(); void storageOperators(); void storageDestructor(); }; @@ -2257,6 +2259,52 @@ void tst_Tasking::testTree() QCOMPARE(result, testData.onDone); } +struct StorageIO +{ + int value = 0; +}; + +static Group inputOutputRecipe(const TreeStorage &storage) +{ + return Group { + Storage(storage), + onGroupSetup([storage] { ++storage->value; }), + onGroupDone([storage] { storage->value *= 2; }) + }; +} + +void tst_Tasking::storageIO_data() +{ + QTest::addColumn("input"); + QTest::addColumn("output"); + + QTest::newRow("-1 -> 0") << -1 << 0; + QTest::newRow("0 -> 2") << 0 << 2; + QTest::newRow("1 -> 4") << 1 << 4; + QTest::newRow("2 -> 6") << 2 << 6; +} + +void tst_Tasking::storageIO() +{ + QFETCH(int, input); + QFETCH(int, output); + + int actualOutput = 0; + + const TreeStorage storage; + TaskTree taskTree(inputOutputRecipe(storage)); + + const auto setInput = [input](StorageIO &storage) { storage.value = input; }; + const auto getOutput = [&actualOutput](const StorageIO &storage) { actualOutput = storage.value; }; + + taskTree.onStorageSetup(storage, setInput); + taskTree.onStorageDone(storage, getOutput); + taskTree.runBlocking(); + + QCOMPARE(taskTree.isRunning(), false); + QCOMPARE(actualOutput, output); +} + void tst_Tasking::storageOperators() { TreeStorageBase storage1 = TreeStorage();