File: test_indexedstring.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (181 lines) | stat: -rw-r--r-- 6,079 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
    SPDX-FileCopyrightText: 2012-2013 Milian Wolff <mail@milianw.de>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "test_indexedstring.h"

#include <serialization/indexedstring.h>
#include <serialization/referencecounting.h>

#include <QTest>

#include <cstddef>
#include <utility>

QTEST_GUILESS_MAIN(TestIndexedString)

using namespace KDevelop;

void TestIndexedString::testUrl_data()
{
    QTest::addColumn<QUrl>("url");
    QTest::addColumn<QString>("string");
    QTest::newRow("empty") << QUrl() << QString();
    QTest::newRow("/") << QUrl::fromLocalFile(QStringLiteral("/")) << QStringLiteral("/");
    QTest::newRow("/foo/bar") << QUrl::fromLocalFile(QStringLiteral("/foo/bar")) << QStringLiteral("/foo/bar");
    QTest::newRow("http://foo.com/") << QUrl(QStringLiteral("http://foo.com/")) << QStringLiteral("http://foo.com/");
    QTest::newRow("http://foo.com/bar/asdf") << QUrl(QStringLiteral("http://foo.com/bar/asdf")) << QStringLiteral(
        "http://foo.com/bar/asdf");
    QTest::newRow("file:///bar/asdf") << QUrl(QStringLiteral("file:///bar/asdf")) << QStringLiteral("/bar/asdf");

#ifdef Q_OS_WIN
    // Make sure we're not running into https://bugreports.qt.io/browse/QTBUG-41729
    QTest::newRow("file:///C:/bar/asdf") << QUrl("file:///C:/bar/asdf") << QStringLiteral("C:/bar/asdf");
#endif
}

void TestIndexedString::testUrl()
{
    QFETCH(QUrl, url);
    IndexedString indexed(url);
    QCOMPARE(indexed.toUrl(), url);
    QCOMPARE(indexed.index(), IndexedString::indexForUrl(url));
    QTEST(indexed.str(), "string");
}

void TestIndexedString::test()
{
    QFETCH(QString, data);

    IndexedString indexed(data);
    QCOMPARE(indexed.str(), data);
    QCOMPARE(indexed.index(), IndexedString::indexForString(data));
    const auto byteArrayData = data.toUtf8();
    QEXPECT_FAIL("char-utf8", "UTF-8 gets decoded and the char data is stored internally", Continue);
    QEXPECT_FAIL("string-utf8", "UTF-8 gets decoded and the char data is stored internally", Continue);
    QCOMPARE(indexed.length(), data.length());
    // fallback until we rely on internal utf8 encoding
    QCOMPARE(indexed.length(), byteArrayData.length());

    QCOMPARE(indexed.byteArray(), byteArrayData);
    QVERIFY(!strncmp(indexed.c_str(), byteArrayData.data(), byteArrayData.length()));
    QCOMPARE(indexed.index(), IndexedString::indexForString(byteArrayData.data(), byteArrayData.length()));

    IndexedString moved = std::move(indexed);
    QCOMPARE(moved.str(), data);
}

void TestIndexedString::test_data()
{
    QTest::addColumn<QString>("data");

    QTest::newRow("empty") << QString();
    QTest::newRow("char-ascii") << QStringLiteral("a");
    QTest::newRow("char-utf8") << QStringLiteral("ä");
    QTest::newRow("string-ascii") << QStringLiteral("asdf()?=");
    QTest::newRow("string-utf8") << QStringLiteral("æſðđäöü");
}

void TestIndexedString::testMoveAssignment()
{
    // Deliberately make no assumptions about the value of the moved-from object.

    const QString text = QStringLiteral("random text");
    IndexedString movedFrom(text);
    QCOMPARE(movedFrom.str(), text);

    IndexedString notRefCounted;
    notRefCounted = std::move(movedFrom);
    QCOMPARE(notRefCounted.str(), text);

    movedFrom = std::move(notRefCounted);
    QCOMPARE(movedFrom.str(), text);

    const QString toBeDerefed = QStringLiteral("to-be-derefed");

    // Enable reference counting throughout refCounted's lifetime.
    std::byte indexedStringData[sizeof(IndexedString)];
    const DUChainReferenceCountingEnabler rcEnabler(indexedStringData, sizeof(IndexedString));
    IndexedString* const refCounted = new (indexedStringData) IndexedString(toBeDerefed);
    QCOMPARE(refCounted->str(), toBeDerefed);

    *refCounted = std::move(movedFrom);
    QCOMPARE(refCounted->str(), text);
    refCounted->~IndexedString();
}

void TestIndexedString::testSwap()
{
    QFETCH(bool, lhsRcEnabled);
    QFETCH(bool, rhsRcEnabled);

    class OptionallyRcString
    {
    public:
        explicit OptionallyRcString(bool rcEnabled, const QString& initText)
            : m_rcEnabled{rcEnabled}
            , m_initText{initText}
            , m_notRefCounted{m_initText}
            , m_refCountedData{}
            , m_rcEnabler{m_refCountedData, sizeof(IndexedString)}
            , m_string{m_rcEnabled ? new (m_refCountedData) IndexedString(m_initText) : &m_notRefCounted}
        {
            QCOMPARE(m_string->str(), m_initText);
        }

        ~OptionallyRcString()
        {
            if (m_rcEnabled) {
                m_string->~IndexedString();
            }
        }

        const QString& initText() const { return m_initText; }
        IndexedString& string() { return *m_string; }

    private:
        const bool m_rcEnabled;
        const QString m_initText;

        IndexedString m_notRefCounted;
        std::byte m_refCountedData[sizeof(IndexedString)];
        const DUChainReferenceCountingEnabler m_rcEnabler;

        IndexedString* const m_string = nullptr;
    };

    OptionallyRcString lhs(lhsRcEnabled, QStringLiteral("1st text"));
    OptionallyRcString rhs(rhsRcEnabled, QStringLiteral("another string"));

    using std::swap;

    swap(lhs.string(), rhs.string());
    QCOMPARE(lhs.string().str(), rhs.initText());
    QCOMPARE(rhs.string().str(), lhs.initText());

    swap(lhs.string(), rhs.string());
    QCOMPARE(lhs.string().str(), lhs.initText());
    QCOMPARE(rhs.string().str(), rhs.initText());
}

void TestIndexedString::testSwap_data()
{
    QTest::addColumn<bool>("lhsRcEnabled");
    QTest::addColumn<bool>("rhsRcEnabled");

    QTest::newRow("no reference counting") << false << false;
    QTest::newRow("lhs reference-counted") << true << false;
    QTest::newRow("rhs reference-counted") << false << true;
    QTest::newRow("both reference-counted") << true << true;
}

void TestIndexedString::testCString()
{
    IndexedString str(nullptr);
    QCOMPARE(str.index(), 0u);
    QVERIFY(str.isEmpty());
}

#include "moc_test_indexedstring.cpp"