summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/serialization/qcborvalue/tst_bench_qcborvalue.cpp
blob: b9978fa25c6d452dbfab65c6784d8b8f3374e224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QCborMap>
#include <QCborValue>

#include <QTest>

template <typename Char>
struct SampleStrings
{
    static constexpr char key[] = "hello";
};

template <>
struct SampleStrings<char16_t>
{
    static constexpr char16_t key[] = u"hello";
};

template <>
struct SampleStrings<QChar>
{
    static const QChar *const key;
};
const QChar *const SampleStrings<QChar>::key =
        reinterpret_cast<const QChar *>(SampleStrings<char16_t>::key);

template <typename T, typename = void>
constexpr bool hasValueType = false;

template <typename T>
constexpr bool hasValueType<T, std::void_t<typename T::value_type>> = true;

class tst_QCborValue : public QObject
{
    Q_OBJECT
private:
    template <typename Type>
    void doKeyLookup();

    template <typename Type>
    void doConstruct();

private slots:
    void keyLookupLatin1() { doKeyLookup<QLatin1StringView>(); }
    void keyLookupString() { doKeyLookup<QString>(); }
    void keyLookupConstCharPtr() { doKeyLookup<char>(); };

    void constructLatin1() { doConstruct<QLatin1StringView>(); }
    void constructString() { doConstruct<QString>(); }
    void constructStringView() { doConstruct<QStringView>(); }
    void constructConstCharPtr() { doConstruct<char>(); }
};

template <typename Type>
void tst_QCborValue::doKeyLookup()
{
    const QCborMap m{{"hello", "world"}, {1, 2}};
    const QCborValue v = m;

    if constexpr (hasValueType<Type>) {
        using Char = std::remove_cv_t<typename Type::value_type>;
        using Strings = SampleStrings<Char>;
        const Type s(Strings::key);
        QBENCHMARK {
            [[maybe_unused]] const QCborValue r = v[s];
        }
    } else {
        QBENCHMARK {
            [[maybe_unused]] const QCborValue r = v[SampleStrings<Type>::key];
        }
    }
}

template<typename Type>
void tst_QCborValue::doConstruct()
{
    if constexpr (hasValueType<Type>) {
        using Char = std::remove_cv_t<typename Type::value_type>;
        using Strings = SampleStrings<Char>;
        const Type s(Strings::key);
        QBENCHMARK {
            [[maybe_unused]] const auto v = QCborValue{s};
        }
    } else {
        QBENCHMARK {
            [[maybe_unused]] const auto v = QCborValue{SampleStrings<Type>::key};
        }
    }
}

QTEST_MAIN(tst_QCborValue)

#include "tst_bench_qcborvalue.moc"