| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
#include <QtTest> |
| 30 |
#include <QtCore> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <assert.h> |
| 33 |
|
| 34 |
#include <private/qv4executableallocator.h> |
| 35 |
|
| 36 |
using namespace QQmlJS::VM; |
| 37 |
|
| 38 |
class tst_ExecutableAllocator : public QObject |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
private slots: |
| 42 |
void singleAlloc(); |
| 43 |
void mergeNext(); |
| 44 |
void mergePrev(); |
| 45 |
void multipleChunks(); |
| 46 |
}; |
| 47 |
|
| 48 |
void tst_ExecutableAllocator::singleAlloc() |
| 49 |
{ |
| 50 |
ExecutableAllocator allocator; |
| 51 |
ExecutableAllocator::Allocation *p = allocator.allocate(256); |
| 52 |
QCOMPARE(allocator.freeAllocationCount(), 1); |
| 53 |
QCOMPARE(allocator.chunkCount(), 1); |
| 54 |
allocator.free(p); |
| 55 |
QCOMPARE(allocator.freeAllocationCount(), 0); |
| 56 |
QCOMPARE(allocator.chunkCount(), 0); |
| 57 |
} |
| 58 |
|
| 59 |
void tst_ExecutableAllocator::mergeNext() |
| 60 |
{ |
| 61 |
ExecutableAllocator allocator; |
| 62 |
|
| 63 |
ExecutableAllocator::Allocation *first = allocator.allocate(10); |
| 64 |
QCOMPARE(allocator.freeAllocationCount(), 1); |
| 65 |
QCOMPARE(allocator.chunkCount(), 1); |
| 66 |
|
| 67 |
ExecutableAllocator::Allocation *second = allocator.allocate(10); |
| 68 |
QCOMPARE(allocator.freeAllocationCount(), 1); |
| 69 |
QCOMPARE(allocator.chunkCount(), 1); |
| 70 |
|
| 71 |
allocator.free(second); |
| 72 |
QCOMPARE(allocator.freeAllocationCount(), 1); |
| 73 |
QCOMPARE(allocator.chunkCount(), 1); |
| 74 |
|
| 75 |
allocator.free(first); |
| 76 |
QCOMPARE(allocator.freeAllocationCount(), 0); |
| 77 |
QCOMPARE(allocator.chunkCount(), 0); |
| 78 |
} |
| 79 |
|
| 80 |
void tst_ExecutableAllocator::mergePrev() |
| 81 |
{ |
| 82 |
ExecutableAllocator allocator; |
| 83 |
|
| 84 |
ExecutableAllocator::Allocation *first = allocator.allocate(10); |
| 85 |
QCOMPARE(allocator.freeAllocationCount(), 1); |
| 86 |
|
| 87 |
ExecutableAllocator::Allocation *second = allocator.allocate(10); |
| 88 |
QCOMPARE(allocator.freeAllocationCount(), 1); |
| 89 |
|
| 90 |
ExecutableAllocator::Allocation *third = allocator.allocate(10); |
| 91 |
QCOMPARE(allocator.freeAllocationCount(), 1); |
| 92 |
|
| 93 |
allocator.free(first); |
| 94 |
QCOMPARE(allocator.freeAllocationCount(), 2); |
| 95 |
|
| 96 |
allocator.free(second); |
| 97 |
QCOMPARE(allocator.freeAllocationCount(), 2); |
| 98 |
|
| 99 |
allocator.free(third); |
| 100 |
QCOMPARE(allocator.freeAllocationCount(), 0); |
| 101 |
} |
| 102 |
|
| 103 |
void tst_ExecutableAllocator::multipleChunks() |
| 104 |
{ |
| 105 |
ExecutableAllocator allocator; |
| 106 |
|
| 107 |
ExecutableAllocator::Allocation *first = allocator.allocate(10); |
| 108 |
QCOMPARE(allocator.chunkCount(), 1); |
| 109 |
|
| 110 |
ExecutableAllocator::Allocation *second = allocator.allocate(8 * 1024 * 1024); |
| 111 |
QCOMPARE(allocator.chunkCount(), 2); |
| 112 |
|
| 113 |
ExecutableAllocator::Allocation *third = allocator.allocate(1024); |
| 114 |
QCOMPARE(allocator.chunkCount(), 2); |
| 115 |
|
| 116 |
allocator.free(first); |
| 117 |
allocator.free(second); |
| 118 |
allocator.free(third); |
| 119 |
QCOMPARE(allocator.chunkCount(), 0); |
| 120 |
QCOMPARE(allocator.freeAllocationCount(), 0); |
| 121 |
} |
| 122 |
|
| 123 |
QTEST_MAIN(tst_ExecutableAllocator) |
| 124 |
#include "tst_executableallocator.moc" |