summaryrefslogtreecommitdiffstats
path: root/tests/auto/qoffsetvector/tst_qoffsetvector.cpp
blob: 439ea2c7516676073abc890dd2835f35b7c02080 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/****************************************************************************
**
** This file is part of the $PACKAGE_NAME$.
**
** Copyright (C) $THISYEAR$ $COMPANY_NAME$.
**
** $QT_EXTENDED_DUAL_LICENSE$
**
****************************************************************************/

#include <QObject>
#include <QTest>
#include <QList>
#include <QString>
#include <QCache>
#include <QOffsetVector>

#include <QDebug>
#include <stdio.h>


#if defined(FORCE_UREF)
template <class aT>
inline QDebug &operator<<(QDebug debug, const QOffsetVector<aT> &offsetVector)
#else
template <class aT>
inline QDebug operator<<(QDebug debug, const QOffsetVector<aT> &offsetVector)
#endif
{
    debug.nospace() << "QOffsetVector(";
    for (int i = offsetVector.firstIndex(); i <= offsetVector.lastIndex(); ++i) {
        debug << offsetVector[i];
        if (i != offsetVector.lastIndex())
            debug << ", ";
    }
    debug << ")";
    return debug.space();
}

#if defined(NO_BENCHMARK) and defined(QBENCHMARK)
#undef QBENCHMARK
#define QBENCHMARK
#endif

class tst_QOffsetVector : public QObject
{
    Q_OBJECT
public:
    tst_QOffsetVector() {}
    virtual ~tst_QOffsetVector() {}
private slots:
    void empty();
    void forwardBuffer();
    void scrollingList();

    void complexType();

    void operatorAt();

    void cacheBenchmark();
    void offsetVectorBenchmark();

    void setCapacity();
};

QTEST_MAIN(tst_QOffsetVector)

void tst_QOffsetVector::empty()
{
    QOffsetVector<int> c(10);
    QCOMPARE(c.capacity(), 10);
    QCOMPARE(c.count(), 0);
    QVERIFY(c.isEmpty());
    c.append(1);
    QVERIFY(!c.isEmpty());
    c.clear();
    QCOMPARE(c.capacity(), 10);
    QCOMPARE(c.count(), 0);
    QVERIFY(c.isEmpty());
    c.prepend(1);
    QVERIFY(!c.isEmpty());
    c.clear();
    QCOMPARE(c.count(), 0);
    QVERIFY(c.isEmpty());
    QCOMPARE(c.capacity(), 10);
}

void tst_QOffsetVector::forwardBuffer()
{
    int i;
    QOffsetVector<int> c(10);
    for(i = 1; i < 30; ++i) {
        c.append(i);
        QCOMPARE(c.first(), qMax(1, i-9));
        QCOMPARE(c.last(), i);
        QCOMPARE(c.count(), qMin(i, 10));
    }

    c.clear();

    for(i = 1; i < 30; ++i) {
        c.prepend(i);
        QCOMPARE(c.last(), qMax(1, i-9));
        QCOMPARE(c.first(), i);
        QCOMPARE(c.count(), qMin(i, 10));
    }
}

void tst_QOffsetVector::scrollingList()
{
    int i;
    QOffsetVector<int> c(10);

    // Once allocated QOffsetVector should not
    // allocate any additional memory for non
    // complex data types.
    QBENCHMARK {
        // simulate scrolling in a list of items;
        for(i = 0; i < 10; ++i)
            c.append(i);

        QCOMPARE(c.firstIndex(), 0);
        QCOMPARE(c.lastIndex(), 9);
        QVERIFY(c.containsIndex(0));
        QVERIFY(c.containsIndex(9));
        QVERIFY(!c.containsIndex(10));

        for (i = 0; i < 10; ++i)
            QCOMPARE(c.at(i), i);

        for (i = 10; i < 30; ++i)
            c.append(i);

        QCOMPARE(c.firstIndex(), 20);
        QCOMPARE(c.lastIndex(), 29);
        QVERIFY(c.containsIndex(20));
        QVERIFY(c.containsIndex(29));
        QVERIFY(!c.containsIndex(30));

        for (i = 20; i < 30; ++i)
            QCOMPARE(c.at(i), i);

        for (i = 19; i >= 10; --i)
            c.prepend(i);

        QCOMPARE(c.firstIndex(), 10);
        QCOMPARE(c.lastIndex(), 19);
        QVERIFY(c.containsIndex(10));
        QVERIFY(c.containsIndex(19));
        QVERIFY(!c.containsIndex(20));

        for (i = 10; i < 20; ++i)
            QCOMPARE(c.at(i), i);

        for (i = 200; i < 220; ++i)
            c.insert(i, i);

        QCOMPARE(c.firstIndex(), 210);
        QCOMPARE(c.lastIndex(), 219);
        QVERIFY(c.containsIndex(210));
        QVERIFY(c.containsIndex(219));
        QVERIFY(!c.containsIndex(300));
        QVERIFY(!c.containsIndex(209));

        for (i = 220; i < 220; ++i) {
            QVERIFY(c.containsIndex(i));
            QCOMPARE(c.at(i), i);
        }
        c.clear(); // needed to reset benchmark
    }

    // from a specific bug that was encountered.  100 to 299 cached, attempted to cache 250 - 205 via insert, failed.
    // bug was that item at 150 would instead be item that should have been inserted at 250
    c.setCapacity(200);
    for(i = 100; i < 300; ++i)
        c.insert(i, i);
    for (i = 250; i <= 306; ++i)
        c.insert(i, 1000+i);
    for (i = 107; i <= 306; ++i) {
        QVERIFY(c.containsIndex(i));
        QCOMPARE(c.at(i), i < 250 ? i : 1000+i);
    }
}

struct RefCountingClassData
{
    QBasicAtomicInt ref;
    static RefCountingClassData shared_null;
};

RefCountingClassData RefCountingClassData::shared_null = {
    Q_BASIC_ATOMIC_INITIALIZER(1)
};

class RefCountingClass
{
public:
    RefCountingClass() : d(&RefCountingClassData::shared_null) { d->ref.ref(); }

    RefCountingClass(const RefCountingClass &other)
    {
        d = other.d;
        d->ref.ref();
    }

    ~RefCountingClass()
    {
        if (!d->ref.deref())
            delete d;
    }

    RefCountingClass &operator=(const RefCountingClass &other)
    {
        if (!d->ref.deref())
            delete d;
        d = other.d;
        d->ref.ref();
        return *this;
    }

    int refCount() const { return d->ref; }
private:
    RefCountingClassData *d;
};

void tst_QOffsetVector::complexType()
{
    RefCountingClass original;

    QOffsetVector<RefCountingClass> offsetVector(10);
    offsetVector.append(original);
    QCOMPARE(original.refCount(), 3);
    offsetVector.removeFirst();
    QCOMPARE(original.refCount(), 2); // shared null, 'original'.
    offsetVector.append(original);
    QCOMPARE(original.refCount(), 3);
    offsetVector.clear();
    QCOMPARE(original.refCount(), 2);

    for(int i = 0; i < 100; ++i)
        offsetVector.insert(i, original);

    QCOMPARE(original.refCount(), 12); // shared null, 'original', + 10 in offsetVector.

    offsetVector.clear();
    QCOMPARE(original.refCount(), 2);
    for (int i = 0; i < 100; i++)
        offsetVector.append(original);

    QCOMPARE(original.refCount(), 12); // shared null, 'original', + 10 in offsetVector.
    offsetVector.clear();
    QCOMPARE(original.refCount(), 2);

    for (int i = 0; i < 100; i++)
        offsetVector.prepend(original);

    QCOMPARE(original.refCount(), 12); // shared null, 'original', + 10 in offsetVector.
    offsetVector.clear();
    QCOMPARE(original.refCount(), 2);

    for (int i = 0; i < 100; i++)
        offsetVector.append(original);

    offsetVector.takeLast();
    QCOMPARE(original.refCount(), 11);

    offsetVector.takeFirst();
    QCOMPARE(original.refCount(), 10);
}

void tst_QOffsetVector::operatorAt()
{
    RefCountingClass original;
    QOffsetVector<RefCountingClass> offsetVector(10);

    for (int i = 25; i < 35; ++i)
        offsetVector[i] = original;

    QCOMPARE(original.refCount(), 12); // shared null, orig, items in vector

    // verify const access does not copy items.
    const QOffsetVector<RefCountingClass> copy(offsetVector);
    for (int i = 25; i < 35; ++i)
        QCOMPARE(copy[i].refCount(), 12);

    // verify modifying the original increments ref count (e.g. does a detach)
    offsetVector[25] = original;
    QCOMPARE(original.refCount(), 22);
}

/*
    Benchmarks must be near identical in tasks to be fair.
    QCache uses pointers to ints as its a requirement of QCache,
    whereas QOffsetVector doesn't support pointers (won't free them).
    Given the ability to use simple data types is a benefit, its
    fair.  Although this obviously must take into account we are
    testing QOffsetVector use cases here, QCache has its own
    areas where it is the more sensible class to use.
*/
void tst_QOffsetVector::cacheBenchmark()
{
    QBENCHMARK {
        QCache<int, int> cache;
        cache.setMaxCost(100);

        for (int i = 0; i < 1000; i++)
            cache.insert(i, new int(i));
    }
}

void tst_QOffsetVector::offsetVectorBenchmark()
{
    QBENCHMARK {
        QOffsetVector<int> offsetVector(100);
        for (int i = 0; i < 1000; i++)
            offsetVector.insert(i, i);
    }
}

void tst_QOffsetVector::setCapacity()
{
    int i;
    QOffsetVector<int> offsetVector(100);
    for (i = 280; i < 310; ++i)
        offsetVector.insert(i, i);
    QCOMPARE(offsetVector.capacity(), 100);
    QCOMPARE(offsetVector.count(), 30);
    QCOMPARE(offsetVector.firstIndex(), 280);
    QCOMPARE(offsetVector.lastIndex(), 309);

    for (i = offsetVector.firstIndex(); i <= offsetVector.lastIndex(); ++i) {
        QVERIFY(offsetVector.containsIndex(i));
        QCOMPARE(offsetVector.at(i), i);
    }

    offsetVector.setCapacity(150);

    QCOMPARE(offsetVector.capacity(), 150);
    QCOMPARE(offsetVector.count(), 30);
    QCOMPARE(offsetVector.firstIndex(), 280);
    QCOMPARE(offsetVector.lastIndex(), 309);

    for (i = offsetVector.firstIndex(); i <= offsetVector.lastIndex(); ++i) {
        QVERIFY(offsetVector.containsIndex(i));
        QCOMPARE(offsetVector.at(i), i);
    }

    offsetVector.setCapacity(20);

    QCOMPARE(offsetVector.capacity(), 20);
    QCOMPARE(offsetVector.count(), 20);
    QCOMPARE(offsetVector.firstIndex(), 290);
    QCOMPARE(offsetVector.lastIndex(), 309);

    for (i = offsetVector.firstIndex(); i <= offsetVector.lastIndex(); ++i) {
        QVERIFY(offsetVector.containsIndex(i));
        QCOMPARE(offsetVector.at(i), i);
    }
}

#include "tst_qoffsetvector.moc"