summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/text/qstringtokenizer/tst_bench_qstringtokenizer.cpp
blob: 291b314a7f4665d72beb91a2795468eb37e11c12 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QTest>

#include <QStringTokenizer>

class tst_QStringTokenizer : public QObject
{
    Q_OBJECT

    void tokenize_data() const;
    template <typename T, typename U>
    void tokenize() const;
private slots:
    void tokenize_qlatin1string_qlatin1string_data() const { tokenize_data(); }
    void tokenize_qlatin1string_qlatin1string() const { tokenize<QLatin1String, QLatin1String>(); }
    void tokenize_qstring_qstring_data() const { tokenize_data(); }
    void tokenize_qstring_qstring() const { tokenize<QString, QString>(); }
    void tokenize_qlatin1string_qstring_data() const { tokenize_data(); }
    void tokenize_qlatin1string_qstring() const { tokenize<QLatin1String, QString>(); }
    void tokenize_qstring_qlatin1string_data() const { tokenize_data(); }
    void tokenize_qstring_qlatin1string() const { tokenize<QString, QLatin1String>(); }
};

template<typename T>
T fromByteArray(QByteArrayView v);

template<>
QString fromByteArray<QString>(QByteArrayView v)
{
    return QString::fromLatin1(v);
}

template<>
QLatin1String fromByteArray<QLatin1String>(QByteArrayView v)
{
    return QLatin1String(v.data(), v.size());
}

void tst_QStringTokenizer::tokenize_data() const
{
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QByteArray>("separator");
    QTest::addColumn<bool>("caseSensitive");
    QTest::addColumn<int>("expectedCount");

    QByteArray shortSentence = "A seriously short sentence.";
    QTest::addRow("short-sentence-spaces") << shortSentence << QByteArray(" ") << true << 4;
    QTest::addRow("short-sentence-spaces-case-insensitive")
            << shortSentence << QByteArray(" ") << false << 4;

    QTest::addRow("short-sentence-se") << shortSentence << QByteArray("se") << true << 3;
    QTest::addRow("short-sentence-se-case-insensitive")
            << shortSentence << QByteArray("Se") << false << 3;

    QFile file(":/data/lorem.txt");
    if (!file.open(QFile::ReadOnly))
        qFatal("Can't open lorem.txt");

    const QByteArray content = file.readAll();
    QTest::addRow("lorem-ipsum-spaces") << content << QByteArray(" ") << true << 3250;
    QTest::addRow("lorem-ipsum-spaces-case-insensitive")
            << content << QByteArray(" ") << false << 3250;

    QTest::addRow("lorem-ipsum-l") << content << QByteArray("l") << true << 771;
    QTest::addRow("lorem-ipsum-l-case-insensitive")
            << content << QByteArray("l") << false << 772;

    QTest::addRow("lorem-ipsum-lo") << content << QByteArray("lo") << true << 130;
    QTest::addRow("lorem-ipsum-lo-case-insensitive")
            << content << QByteArray("lo") << false << 131;

    QTest::addRow("lorem-ipsum-lor") << content << QByteArray("lor") << true << 122;
    QTest::addRow("lorem-ipsum-lor-case-insensitive")
            << content << QByteArray("lor") << false << 123;

    QTest::addRow("lorem-ipsum-lore") << content << QByteArray("lore") << true << 73;
    QTest::addRow("lorem-ipsum-lore-case-insensitive")
            << content << QByteArray("lore") << false << 74;

    QTest::addRow("lorem-ipsum-lorem") << content << QByteArray("lorem") << true << 34;
    QTest::addRow("lorem-ipsum-lorem-case-insensitive")
            << content << QByteArray("lorem") << false << 35;

    QTest::addRow("lorem-ipsum-lorem i") << content << QByteArray("lorem i") << true << 5;
    QTest::addRow("lorem-ipsum-lorem i-case-insensitive")
            << content << QByteArray("lorem i") << false << 6;

    QTest::addRow("lorem-ipsum-et explicabo s") << content << QByteArray("et explicabo s") << true << 3;
    QTest::addRow("lorem-ipsum-et explicabo s-case-insensitive")
            << content << QByteArray("et explicabo s") << false << 3;
}

template<typename T, typename U>
void tst_QStringTokenizer::tokenize() const
{
    QFETCH(QByteArray, input);
    QFETCH(QByteArray, separator);
    QFETCH(bool, caseSensitive);
    QFETCH(int, expectedCount);

    T haystack = fromByteArray<T>(input);
    U needle = fromByteArray<U>(separator);

    const Qt::CaseSensitivity sensitivity = caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
    QBENCHMARK {
        QStringTokenizer tok(haystack, needle, sensitivity);
        qsizetype count = 0;
        for (auto res : tok) {
            count++;
            Q_UNUSED(res);
        }
        QCOMPARE(count, expectedCount);
    }
}

QTEST_MAIN(tst_QStringTokenizer)

#include "tst_bench_qstringtokenizer.moc"