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 | |
31 | // Don't do this at home. This is test code, not production. |
32 | #define protected public |
33 | #define private public |
34 | |
35 | #include <private/qobject_p.h> |
36 | #include <private/qv4compileddata_p.h> |
37 | #include <private/qv4string_p.h> |
38 | #include <private/qqmlrefcount_p.h> |
39 | #include <qobject.h> |
40 | |
41 | #if defined(Q_CC_GNU) || defined(Q_CC_MSVC) |
42 | #define RUN_MEMBER_OFFSET_TEST 1 |
43 | #else |
44 | #define RUN_MEMBER_OFFSET_TEST 0 |
45 | #endif |
46 | |
47 | #if RUN_MEMBER_OFFSET_TEST |
48 | template <typename T, typename K> |
49 | size_t pmm_to_offsetof(T K:: *pmm) |
50 | { |
51 | #ifdef Q_CC_MSVC |
52 | // Even on 64 bit MSVC uses 4 byte offsets. |
53 | quint32 ret; |
54 | #else |
55 | size_t ret; |
56 | #endif |
57 | Q_STATIC_ASSERT(sizeof(ret) == sizeof(pmm)); |
58 | memcpy(&ret, &pmm, sizeof(ret)); |
59 | return ret; |
60 | } |
61 | #endif |
62 | |
63 | class tst_toolsupport : public QObject |
64 | { |
65 | Q_OBJECT |
66 | |
67 | private slots: |
68 | void offsets(); |
69 | void offsets_data(); |
70 | }; |
71 | |
72 | void tst_toolsupport::offsets() |
73 | { |
74 | QFETCH(size_t, actual); |
75 | QFETCH(int, expected32); |
76 | QFETCH(int, expected64); |
77 | size_t expect = sizeof(void *) == 4 ? expected32 : expected64; |
78 | QCOMPARE(actual, expect); |
79 | } |
80 | |
81 | void tst_toolsupport::offsets_data() |
82 | { |
83 | QTest::addColumn<size_t>(name: "actual" ); |
84 | QTest::addColumn<int>(name: "expected32" ); |
85 | QTest::addColumn<int>(name: "expected64" ); |
86 | |
87 | { |
88 | QTestData &data = QTest::newRow(dataTag: "sizeof(QObjectData)" ) |
89 | << sizeof(QObjectData); |
90 | data << 28 << 48; // vptr + 3 ptr + 2 int + ptr |
91 | } |
92 | |
93 | { |
94 | QTestData &data = QTest::newRow(dataTag: "sizeof(QQmlRefCount)" ) |
95 | << sizeof(QQmlRefCount); |
96 | data << 8 << 16; |
97 | } |
98 | |
99 | #if RUN_MEMBER_OFFSET_TEST |
100 | { |
101 | QTestData &data |
102 | = QTest::newRow(dataTag: "CompiledData::CompilationUnit::data" ) |
103 | << pmm_to_offsetof(pmm: &QV4::CompiledData::CompilationUnit::data); |
104 | |
105 | data << 20 << 40; |
106 | } |
107 | |
108 | { |
109 | QTestData &data |
110 | = QTest::newRow(dataTag: "CompiledData::CompilationUnit::runtimeStrings" ) |
111 | << pmm_to_offsetof(pmm: &QV4::CompiledData::CompilationUnit::runtimeStrings); |
112 | |
113 | data << 0 << 0; |
114 | } |
115 | |
116 | { |
117 | QTestData &data |
118 | = QTest::newRow(dataTag: "Heap::String::text" ) |
119 | << pmm_to_offsetof(pmm: &QV4::Heap::String::text); |
120 | |
121 | data << 4 << 8; |
122 | } |
123 | |
124 | #endif // RUN_MEMBER_OFFSET_TEST |
125 | } |
126 | |
127 | |
128 | QTEST_APPLESS_MAIN(tst_toolsupport); |
129 | |
130 | #include "tst_toolsupport.moc" |
131 | |
132 | |