summaryrefslogtreecommitdiffstats
path: root/tests/manual/corelib/qdatastream/tst_manualqdatastream.cpp
blob: 37e5b8095089cf8a23863d7f39df91b74b527b9c (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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QDataStream>
#include <QHash>
#include <QList>
#include <QMap>
#include <QScopeGuard>
#include <QSet>
#include <QTest>

// These tests are way too slow to be part of automatic unit tests
class tst_QDataStream : public QObject
{
    Q_OBJECT

    template <class T>
    void fill(T &input);

    void fill(QSet<qsizetype> &input);
    void fill(QMap<qsizetype, qsizetype> &input);
    void fill(QHash<qsizetype, qsizetype> &input);

    template <class T>
    void stream_big();

public slots:
    void initTestCase();

private slots:
    void stream_bigQString();
    void stream_bigQByteArray();
    void stream_bigQList();
    void stream_bigQSet();
    void stream_bigQMap();
    void stream_bigQHash();
    void stream_bigCString();
};

void tst_QDataStream::initTestCase()
{
    qputenv("QTEST_FUNCTION_TIMEOUT", "9000000");

    QTest::addColumn<QDataStream::Version>("streamVersion");
    QTest::addRow("current") << QDataStream::Qt_DefaultCompiledVersion;
    QTest::addRow("Qt_6_6") << QDataStream::Qt_6_6;
}

template <class T>
void tst_QDataStream::fill(T &input)
{
    constexpr qsizetype GiB = 1024 * 1024 * 1024;
    constexpr qsizetype BaseSize = 4 * GiB + 1;
    qDebug("Filling container with %lld entries", qint64(BaseSize));
    QElapsedTimer timer;
    timer.start();
    try {
        input.reserve(BaseSize);
        input.resize(BaseSize, 'a');
    } catch (const std::bad_alloc &) {
        QSKIP("Could not allocate 4 GiB of RAM.");
    }
    qDebug("Created dataset in %lld ms", timer.elapsed());
}

void tst_QDataStream::fill(QSet<qsizetype> &input)
{
    constexpr qsizetype GiB = 1024 * 1024 * 1024;
    constexpr qsizetype BaseSize = 4 * GiB + 1;
    qDebug("Filling container with %lld entries", qint64(BaseSize));
    QElapsedTimer timer;
    timer.start();
    try {
        input.reserve(BaseSize);
        for (qsizetype i = 0; i < BaseSize; ++i)
            input.insert(i);
    } catch (const std::bad_alloc &) {
        QSKIP("Could not allocate 4 Gi entries.");
    }
    qDebug("Created dataset in %lld ms", timer.elapsed());
}

void tst_QDataStream::fill(QMap<qsizetype, qsizetype> &input)
{
    constexpr qsizetype GiB = 1024 * 1024 * 1024;
    constexpr qsizetype BaseSize = 4 * GiB + 1;
    qDebug("Filling container with %lld entries", qint64(BaseSize));
    QElapsedTimer timer;
    timer.start();
    try {
        for (qsizetype i = 0; i < BaseSize; ++i)
            input.insert(i, i);
    } catch (const std::bad_alloc &) {
        QSKIP("Could not allocate 4 Gi entries.");
    }
    qDebug("Created dataset in %lld ms", timer.elapsed());
}

void tst_QDataStream::fill(QHash<qsizetype, qsizetype> &input)
{
    constexpr qsizetype GiB = 1024 * 1024 * 1024;
    constexpr qsizetype BaseSize = 4 * GiB + 1;
    qDebug("Filling container with %lld entries", qint64(BaseSize));
    QElapsedTimer timer;
    timer.start();
    try {
        input.reserve(BaseSize);
        for (qsizetype i = 0; i < BaseSize; ++i)
            input.emplace(i, i);
    } catch (const std::bad_alloc &) {
        QSKIP("Could not allocate 4 Gi entries.");
    }
    qDebug("Created dataset in %lld ms", timer.elapsed());
}

template <class T>
void tst_QDataStream::stream_big()
{
#if QT_POINTER_SIZE > 4
    QFETCH_GLOBAL(const QDataStream::Version, streamVersion);
    QElapsedTimer timer;
    T input;
    fill(input);
    QByteArray ba;
    QDataStream inputstream(&ba, QIODevice::WriteOnly);
    inputstream.setVersion(streamVersion);
    timer.start();
    try {
        inputstream << input;
    } catch (const std::bad_alloc &) {
        QSKIP("Not enough memory to copy into QDataStream.");
    }
    qDebug("Streamed into QDataStream in %lld ms", timer.elapsed());
    if (streamVersion < QDataStream::Qt_6_7) {
        // old versions do not support data size more than 4 GiB
        QCOMPARE(inputstream.status(), QDataStream::SizeLimitExceeded);
        QVERIFY(ba.isEmpty());
    } else {
        T output;
        QDataStream outputstream(ba);
        timer.start();
        try {
            outputstream >> output;
        } catch (const std::bad_alloc &) {
            QSKIP("Not enough memory to copy out of QDataStream.");
        }
        qDebug("Streamed out of QDataStream in %lld ms", timer.elapsed());
        QCOMPARE(input.size(), output.size());
        QCOMPARE(input, output);
    }
#else
    QSKIP("This test is 64-bit only.");
#endif
}

void tst_QDataStream::stream_bigQString()
{
    stream_big<QString>();
}

void tst_QDataStream::stream_bigQByteArray()
{
    stream_big<QByteArray>();
}
void tst_QDataStream::stream_bigQList()
{
    stream_big<QList<char>>();
}

void tst_QDataStream::stream_bigQSet()
{
    stream_big<QSet<qsizetype>>();
}

void tst_QDataStream::stream_bigQMap()
{
    stream_big<QMap<qsizetype, qsizetype>>();
}
void tst_QDataStream::stream_bigQHash()
{
    stream_big<QHash<qsizetype, qsizetype>>();
}

void tst_QDataStream::stream_bigCString()
{
    if constexpr (sizeof(void*) == sizeof(int))
        QSKIP("This test is 64-bit only.");

    QFETCH_GLOBAL(const QDataStream::Version, streamVersion);
    constexpr qint64 GiB = 1024 * 1024 * 1024;
    constexpr qint64 BaseSize = 4 * GiB + 1;
    std::string input;
    qDebug("Creating an array with %lld entries", BaseSize);
    QElapsedTimer timer;
    timer.start();
    try {
        input.resize(BaseSize, 'a');
    } catch (const std::bad_alloc &) {
        QSKIP("Could not allocate 4 Gi + 2 entries.");
    }
    qDebug("Created dataset in %lld ms", timer.elapsed());
    QByteArray ba;
    QDataStream inputstream(&ba, QIODevice::WriteOnly);
    inputstream.setVersion(streamVersion);
    timer.start();
    try {
        inputstream << input.data();
    } catch (const std::bad_alloc &) {
        QSKIP("Not enough memory to copy into QDataStream.");
    }
    qDebug("Streamed into QDataStream in %lld ms", timer.elapsed());
    if (streamVersion < QDataStream::Qt_6_7) {
        // old versions do not support data size more than 4 GiB
        QCOMPARE(inputstream.status(), QDataStream::SizeLimitExceeded);
        QVERIFY(ba.isEmpty());
    } else {
        char *output = nullptr;
        auto cleanup = qScopeGuard([&output] { delete [] output; });
        QDataStream outputstream(ba);
        timer.start();
        try {
            outputstream >> output;
        } catch (const std::bad_alloc &) {
            QSKIP("Not enough memory to copy out of QDataStream.");
        }
        qDebug("Streamed out of QDataStream in %lld ms", timer.elapsed());
        QCOMPARE(qstrlen(output), input.size());
        QVERIFY(memcmp(input.data(), output, BaseSize + 1) == 0);
    }
}

QTEST_MAIN(tst_QDataStream)

#include "tst_manualqdatastream.moc"