1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://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//
32// Note:
33//
34// When this test here fails and the change leading to the failure
35// intentionally changed a private class, adjust the test here and bump
36// the TypeInformationVersion field in src/corelib/global/qhooks.cpp
37// in the same commit as the modification to the private class.
38//
39
40
41// Don't do this at home. This is test code, not production.
42#define protected public
43#define private public
44
45#include <private/qdatetime_p.h>
46#include <private/qfile_p.h>
47#include <private/qfileinfo_p.h>
48#include <private/qobject_p.h>
49#include <qobject.h>
50
51#if defined(Q_CC_GNU) || defined(Q_CC_MSVC)
52#define RUN_MEMBER_OFFSET_TEST 1
53#else
54#define RUN_MEMBER_OFFSET_TEST 0
55#endif
56
57#if RUN_MEMBER_OFFSET_TEST
58template <typename T, typename K>
59size_t pmm_to_offsetof(T K:: *pmm)
60{
61#ifdef Q_CC_MSVC
62 // Even on 64 bit MSVC uses 4 byte offsets.
63 quint32 ret;
64#else
65 size_t ret;
66#endif
67 Q_STATIC_ASSERT(sizeof(ret) == sizeof(pmm));
68 memcpy(&ret, &pmm, sizeof(ret));
69 return ret;
70}
71#endif
72
73class tst_toolsupport : public QObject
74{
75 Q_OBJECT
76
77private slots:
78 void offsets();
79 void offsets_data();
80};
81
82void tst_toolsupport::offsets()
83{
84 QFETCH(size_t, actual);
85 QFETCH(int, expected32);
86 QFETCH(int, expected64);
87 size_t expect = sizeof(void *) == 4 ? expected32 : expected64;
88 QCOMPARE(actual, expect);
89}
90
91void tst_toolsupport::offsets_data()
92{
93 QTest::addColumn<size_t>(name: "actual");
94 QTest::addColumn<int>(name: "expected32");
95 QTest::addColumn<int>(name: "expected64");
96
97 {
98 QTestData &data = QTest::newRow(dataTag: "sizeof(QObjectData)")
99 << sizeof(QObjectData);
100 data << 28 << 48; // vptr + 3 ptr + 2 int + ptr
101 }
102
103#if RUN_MEMBER_OFFSET_TEST
104 {
105 QTestData &data = QTest::newRow(dataTag: "QObjectPrivate::extraData")
106 << pmm_to_offsetof(pmm: &QObjectPrivate::extraData);
107 data << 28 << 48; // sizeof(QObjectData)
108 }
109
110 {
111 QTestData &data = QTest::newRow(dataTag: "QFileInfoPrivate::fileEntry")
112 << pmm_to_offsetof(pmm: &QFileInfoPrivate::fileEntry);
113 data << 4 << 8;
114 }
115
116 {
117 QTestData &data = QTest::newRow(dataTag: "QFileSystemEntry::filePath")
118 << pmm_to_offsetof(pmm: &QFileSystemEntry::m_filePath);
119 data << 0 << 0;
120 }
121
122#ifdef Q_OS_LINUX
123 {
124 QTestData &data = QTest::newRow(dataTag: "QFilePrivate::fileName")
125 << pmm_to_offsetof(pmm: &QFilePrivate::fileName);
126#ifdef Q_PROCESSOR_X86
127 // x86 32-bit has weird alignment rules. Refer to QtPrivate::AlignOf in
128 // qglobal.h for more details.
129 data << 152 << 224;
130#else
131 data << 156 << 224;
132#endif
133 }
134#endif
135
136 {
137 QTest::newRow(dataTag: "QDateTimePrivate::m_msecs")
138 << pmm_to_offsetof(pmm: &QDateTimePrivate::m_msecs) << 8 << 8;
139 QTest::newRow(dataTag: "QDateTimePrivate::m_status")
140 << pmm_to_offsetof(pmm: &QDateTimePrivate::m_status) << 4 << 4;
141 QTest::newRow(dataTag: "QDateTimePrivate::m_offsetFromUtc")
142 << pmm_to_offsetof(pmm: &QDateTimePrivate::m_offsetFromUtc) << 16 << 16;
143#if QT_CONFIG(timezone)
144 QTest::newRow(dataTag: "QDateTimePrivate::m_timeZone")
145 << pmm_to_offsetof(pmm: &QDateTimePrivate::m_timeZone) << 20 << 24;
146#endif
147 }
148#endif // RUN_MEMBER_OFFSET_TEST
149}
150
151
152QTEST_APPLESS_MAIN(tst_toolsupport);
153
154#include "tst_toolsupport.moc"
155
156

source code of qtbase/tests/auto/other/toolsupport/tst_toolsupport.cpp