summaryrefslogtreecommitdiffstats
path: root/tests/auto/threed/qareaallocator/tst_qareaallocator.cpp
blob: 40b76fafd52224b6f55a9c2e0706ded8363ad374 (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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtQuick3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include "qareaallocator.h"

class tst_QAreaAllocator : public QObject
{
    Q_OBJECT
public:
    tst_QAreaAllocator() {}
    ~tst_QAreaAllocator() {}

private slots:
    void create();
    void allocateSimple();
    void allocateGeneral();
    void allocateUniform();
};

void tst_QAreaAllocator::create()
{
    QSimpleAreaAllocator alloc1(QSize(400, 200));
    QCOMPARE(alloc1.size(), QSize(400, 200));
    QCOMPARE(alloc1.minimumAllocation(), QSize(1, 1));
    QCOMPARE(alloc1.margin(), QSize(0, 0));

    alloc1.setMinimumAllocation(QSize(5, 7));
    QCOMPARE(alloc1.minimumAllocation(), QSize(5, 7));

    alloc1.setMargin(QSize(8, 9));
    QCOMPARE(alloc1.margin(), QSize(8, 9));

    alloc1.expand(QSize(300, 21));
    QCOMPARE(alloc1.size(), QSize(400, 200));

    alloc1.expand(QSize(300, 210));
    QCOMPARE(alloc1.size(), QSize(400, 210));

    alloc1.expand(QSize(3000, 110));
    QCOMPARE(alloc1.size(), QSize(3000, 210));

    alloc1.expandBy(QSize(-1, -1000));
    QCOMPARE(alloc1.size(), QSize(3000, 210));

    alloc1.expandBy(QSize(1, 10));
    QCOMPARE(alloc1.size(), QSize(3001, 220));

    QGeneralAreaAllocator alloc2(QSize(400, 200));
    QCOMPARE(alloc2.size(), QSize(512, 256));
    QCOMPARE(alloc2.minimumAllocation(), QSize(8, 8));
    QCOMPARE(alloc2.margin(), QSize(0, 0));

    alloc2.expand(QSize(256, 96));
    QCOMPARE(alloc2.size(), QSize(512, 256));

    alloc2.expand(QSize(256, 257));
    QCOMPARE(alloc2.size(), QSize(512, 512));

    alloc2.expand(QSize(513, 257));
    QCOMPARE(alloc2.size(), QSize(1024, 512));

    alloc2.expandBy(QSize(-100, -100));
    QCOMPARE(alloc2.size(), QSize(1024, 512));

    alloc2.expandBy(QSize(1, 2));
    QCOMPARE(alloc2.size(), QSize(2048, 1024));

    QUniformAreaAllocator alloc3(QSize(400, 200), QSize(16, 16));
    QCOMPARE(alloc3.size(), QSize(400, 200));
    QCOMPARE(alloc3.minimumAllocation(), QSize(1, 1));
    QCOMPARE(alloc3.margin(), QSize(0, 0));

    alloc3.expand(QSize(256, 96));
    QCOMPARE(alloc3.size(), QSize(400, 200));

    alloc3.expand(QSize(256, 257));
    QCOMPARE(alloc3.size(), QSize(400, 257));

    alloc3.expand(QSize(513, 257));
    QCOMPARE(alloc3.size(), QSize(513, 257));

    alloc3.expandBy(QSize(-100, -100));
    QCOMPARE(alloc3.size(), QSize(513, 257));

    alloc3.expandBy(QSize(1, 2));
    QCOMPARE(alloc3.size(), QSize(514, 259));
}

void tst_QAreaAllocator::allocateSimple()
{
    // The simple allocator returns regions in a left-to-right,
    // top-to-bottom order across the image extents.
    QSimpleAreaAllocator alloc1(QSize(100, 100));
    QRect rect;
    for (int y = 0; y < 10; ++y) {
        for (int x = 0; x < 10; ++x) {
            rect = alloc1.allocate(QSize(10, 10));
            QCOMPARE(rect, QRect(x * 10, y * 10, 10, 10));
        }
    }
    rect = alloc1.allocate(QSize(10, 10));
    QVERIFY(rect.isNull());

    // Release doesn't do anything on the simple allocator.
    alloc1.release(QRect(0, 0, 10, 10));
    rect = alloc1.allocate(QSize(10, 10));
    QVERIFY(rect.isNull());

    // Test minimum allocation sizes.
    QSimpleAreaAllocator alloc2(QSize(100, 100));
    alloc2.setMinimumAllocation(QSize(10, 5));
    for (int y = 0; y < 10; ++y) {
        for (int x = 0; x < 10; ++x) {
            rect = alloc2.allocate(QSize(x + 1, y + 1));
            if (y < 5)
                QCOMPARE(rect, QRect(x * 10, y * 5, x + 1, y + 1));
            else
                QCOMPARE(rect, QRect(x * 10, (y - 5) * 10 + 25, x + 1, y + 1));
        }
    }
    rect = alloc2.allocate(QSize(10, 10));
    QCOMPARE(rect, QRect(0, 75, 10, 10));   // Still some space left over.
    rect = alloc2.allocate(QSize(10, 26));  // Won't fit.
    QVERIFY(rect.isNull());
    rect = alloc2.allocate(QSize(10, 10));  // Should still fit.
    QCOMPARE(rect, QRect(10, 75, 10, 10));
}

void tst_QAreaAllocator::allocateGeneral()
{
    QGeneralAreaAllocator alloc1(QSize(128, 128));
    QRect rect;
    QList<QRect> allocations;
    for (int y = 0; y < 16; ++y) {
        for (int x = 0; x < 16; ++x) {
            // The order in which allocations are returned is not
            // as predictable as QSimpleAreaAllocator.  But we can
            // ensure that all allocations are unique and 8x8-aligned.
            rect = alloc1.allocate(QSize(8, 8));
            QVERIFY((rect.x() % 8) == 0);
            QVERIFY((rect.y() % 8) == 0);
            QVERIFY((rect.height() % 8) == 0);
            QVERIFY((rect.height() % 8) == 0);
            QVERIFY(!allocations.contains(rect));
            allocations += rect;
        }
    }
    QCOMPARE(allocations.size(), 256);
    rect = alloc1.allocate(QSize(8, 8));
    QVERIFY(rect.isNull());

    // Double the width and height and then do some more allocations at 16x16.
    alloc1.expandBy(QSize(1, 1));
    for (int z = 0; z < 3; ++z) {
        for (int y = 0; y < 8; ++y) {
            for (int x = 0; x < 8; ++x) {
                rect = alloc1.allocate(QSize(16, 16));
                QVERIFY((rect.x() % 8) == 0);
                QVERIFY((rect.y() % 8) == 0);
                QVERIFY((rect.height() % 16) == 0);
                QVERIFY((rect.height() % 16) == 0);
                QVERIFY(!allocations.contains(rect));
                allocations += rect;
            }
        }
    }
    QCOMPARE(allocations.size(), 256 + 3 * 8 * 8);
    rect = alloc1.allocate(QSize(8, 8));
    QVERIFY(rect.isNull());

    // Release the 8x8 allocations.
    for (int y = 0; y < 16; ++y)
        for (int x = 0; x < 16; ++x)
            alloc1.release(QRect(x * 8, y * 8, 8, 8));
    rect = alloc1.allocate(QSize(8, 8));
    QVERIFY(!rect.isNull());
    alloc1.release(rect);

    // Allocate some other sizes into the gap we've just made.
    rect = alloc1.allocate(QSize(256, 256));    // Too big.
    QVERIFY(rect.isNull());
    rect = alloc1.allocate(QSize(64, 128));
    QVERIFY(!rect.isNull());
    rect = alloc1.allocate(QSize(64, 64));
    QVERIFY(!rect.isNull());
    rect = alloc1.allocate(QSize(64, 48));      // Over-allocates to 64x64.
    QVERIFY(!rect.isNull());
    rect = alloc1.allocate(QSize(8, 8));        // Will fail.
    QVERIFY(rect.isNull());
}

void tst_QAreaAllocator::allocateUniform()
{
    // The uniform allocator initially returns regions in a
    // left-to-right, top-to-bottom order across the image extents.
    QUniformAreaAllocator alloc1(QSize(100, 100), QSize(10, 10));
    QRect rect;
    for (int y = 0; y < 10; ++y) {
        for (int x = 0; x < 10; ++x) {
            rect = alloc1.allocate(QSize(10, 10));
            QCOMPARE(rect, QRect(x * 10, y * 10, 10, 10));
        }
    }
    rect = alloc1.allocate(QSize(10, 10));
    QVERIFY(rect.isNull());

    // Release some areas and then allocate again.  Should act like a stack.
    rect = QRect(60, 50, 10, 10);
    QRect rect2 = QRect(50, 40, 10, 10);
    alloc1.release(rect);
    alloc1.release(rect2);
    QCOMPARE(alloc1.allocate(QSize(10, 10)), rect2);
    QCOMPARE(alloc1.allocate(QSize(10, 10)), rect);
    QVERIFY(alloc1.allocate(QSize(10, 10)).isNull());

    // Expand the allocation area.
    alloc1.release(rect);   // Copy across at least 1 free list entry.
    alloc1.expandBy(QSize(10, 10));
    for (int count = 0; count < 22; ++count) {
        rect = alloc1.allocate(QSize(10, 10));
        QVERIFY(!rect.isNull());
    }
    rect = alloc1.allocate(QSize(10, 10));
    QVERIFY(rect.isNull());

    // Release everything and then reallocate.
    for (int y = 0; y < 11; ++y) {
        for (int x = 0; x < 11; ++x)
            alloc1.release(QRect(x * 10, y * 10, 10, 10));
    }
    for (int count = 0; count < (11 * 11); ++count) {
        rect = alloc1.allocate(QSize(10, 10));
        QVERIFY(!rect.isNull());
    }
    rect = alloc1.allocate(QSize(10, 10));
    QVERIFY(rect.isNull());
}

QTEST_APPLESS_MAIN(tst_QAreaAllocator)

#include "tst_qareaallocator.moc"