File indexing completed on 2024-05-12 04:41:21

0001 /*
0002     SPDX-FileCopyrightText: 2019 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include <QObject>
0008 #include <QTest>
0009 
0010 #define KASYNC_TEST
0011 
0012 #include "../src/continuations_p.h"
0013 
0014 namespace KAsync
0015 {
0016 // Simplified definition of KAsync::Job so we can return it from
0017 // the test continuations
0018 template<typename T, typename ... U>
0019 class Job {};
0020 }
0021 
0022 Q_DECLARE_METATYPE(std::function<bool()>)
0023 
0024 #define STATIC_COMPARE(actual, expected) \
0025     static_assert((actual) == (expected), "Check failed: " #actual " == " #expected)
0026 
0027 class ContinuationHolderTest : public QObject
0028 {
0029     Q_OBJECT
0030 
0031     using TestedHolder = KAsync::Private::ContinuationHolder<void>;
0032     template<typename Continuation, typename T>
0033     void testContinuation(T &&func, int index)
0034     {
0035 
0036         #define CHECK(Cont) \
0037             QCOMPARE(KAsync::Private::continuationIs<KAsync::Cont<void>>(holder), (std::is_same<Continuation, KAsync::Cont<void>>::value))
0038 
0039         TestedHolder holder(Continuation(std::move(func)));
0040         QCOMPARE(holder.mIndex, index);
0041         CHECK(SyncContinuation);
0042         CHECK(SyncErrorContinuation);
0043         CHECK(AsyncContinuation);
0044         CHECK(AsyncErrorContinuation);
0045         CHECK(JobContinuation);
0046         CHECK(JobErrorContinuation);
0047 
0048         //KAsync::Private::continuation_get<Continuation>(holder)();
0049     }
0050 
0051 private Q_SLOTS:
0052     void testTupleMax()
0053     {
0054         using TestHolder = KAsync::Private::ContinuationHolder<int>;
0055         using Tuple = std::tuple<uint8_t, uint32_t, uint16_t>;
0056 
0057         STATIC_COMPARE(TestHolder::tuple_max<Tuple>::size, sizeof(uint32_t));
0058         STATIC_COMPARE(TestHolder::tuple_max<Tuple>::alignment, alignof(uint32_t));
0059     }
0060 
0061     void testTupleIndex()
0062     {
0063         using TestHolder = KAsync::Private::ContinuationHolder<int>;
0064         using Tuple = std::tuple<uint8_t, uint32_t, uint16_t>;
0065 
0066         STATIC_COMPARE((TestHolder::tuple_index<uint8_t, Tuple>::value), 0);
0067         STATIC_COMPARE((TestHolder::tuple_index<uint32_t, Tuple>::value), 1);
0068         STATIC_COMPARE((TestHolder::tuple_index<uint16_t, Tuple>::value), 2);
0069     }
0070 
0071     void testContinuationHolder_data()
0072     {
0073         QTest::addColumn<std::function<bool()>>("func");
0074 
0075 #define ADD_ROW(name, lambda, index) \
0076         QTest::newRow(#name) << std::function<bool()>([this]() { \
0077             bool called = true; \
0078             testContinuation<KAsync::name<void>>(lambda, index); \
0079             return called; \
0080         });
0081 
0082         ADD_ROW(AsyncContinuation, [&called](KAsync::Future<void> &) mutable { called = true; }, 0);
0083         ADD_ROW(AsyncErrorContinuation, [&called](const KAsync::Error &, KAsync::Future<void> &) mutable { called = true; }, 1);
0084 ADD_ROW(SyncContinuation, [&called]() mutable { called = true; }, 2)
0085         ADD_ROW(SyncErrorContinuation, [&called](const KAsync::Error &) mutable { called = true; }, 3);
0086         ADD_ROW(JobContinuation, [&called]() mutable { called = true; return KAsync::Job<void>(); }, 4);
0087         ADD_ROW(JobErrorContinuation, [&called](const KAsync::Error &) mutable { called = true; return KAsync::Job<void>(); }, 5);
0088     }
0089 
0090     void testContinuationHolder()
0091     {
0092         QFETCH(std::function<bool()>, func);
0093 
0094         QVERIFY(func());
0095     }
0096 };
0097 
0098 QTEST_GUILESS_MAIN(ContinuationHolderTest)
0099 
0100 #include "continuationstest.moc"
0101