1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtCore/qmetaobject.h>
5
6#include <QtTest/qtestassert.h>
7#include <QtTest/qtestdata.h>
8#include <QtTest/private/qtesttable_p.h>
9
10#include <string.h>
11#include <stdlib.h>
12
13QT_BEGIN_NAMESPACE
14
15class QTestDataPrivate
16{
17public:
18 char *tag = nullptr;
19 QTestTable *parent = nullptr;
20 void **data = nullptr;
21 int dataCount = 0;
22};
23
24QTestData::QTestData(const char *tag, QTestTable *parent)
25{
26 QTEST_ASSERT(tag);
27 QTEST_ASSERT(parent);
28 d = new QTestDataPrivate;
29 d->tag = qstrdup(tag);
30 d->parent = parent;
31 d->data = new void *[parent->elementCount()];
32 memset(s: d->data, c: 0, n: parent->elementCount() * sizeof(void*));
33}
34
35QTestData::~QTestData()
36{
37 for (int i = 0; i < d->dataCount; ++i) {
38 if (d->data[i])
39 QMetaType(d->parent->elementTypeId(index: i)).destroy(data: d->data[i]);
40 }
41 delete [] d->data;
42 delete [] d->tag;
43 delete d;
44}
45
46void QTestData::append(int type, const void *data)
47{
48 QTEST_ASSERT(d->dataCount < d->parent->elementCount());
49 int expectedType = d->parent->elementTypeId(index: d->dataCount);
50 int dd = 0;
51 if constexpr (sizeof(qsizetype) == 8) {
52 // Compatibility with Qt 5. Passing a qsizetype to a test function expecting
53 // an int will work. This is required, as methods returning a qsizetype in Qt 6
54 // used to return an int in Qt 5.
55 if (type == QMetaType::LongLong && expectedType == QMetaType::Int) {
56 qlonglong d = *static_cast<const qlonglong *>(data);
57 if (d >= std::numeric_limits<int>::min() && d <= std::numeric_limits<int>::max()) {
58 dd = d;
59 data = &dd;
60 type = QMetaType::Int;
61 }
62 }
63 }
64 if (expectedType != type) {
65 qDebug(msg: "expected data of type '%s', got '%s' for element %d of data with tag '%s'",
66 QMetaType(expectedType).name(),
67 QMetaType(type).name(),
68 d->dataCount, d->tag);
69 QTEST_ASSERT(false);
70 }
71 d->data[d->dataCount] = QMetaType(type).create(copy: data);
72 ++d->dataCount;
73}
74
75void *QTestData::data(int index) const
76{
77 QTEST_ASSERT(index >= 0);
78 QTEST_ASSERT(index < d->parent->elementCount());
79 return d->data[index];
80}
81
82QTestTable *QTestData::parent() const
83{
84 return d->parent;
85}
86
87const char *QTestData::dataTag() const
88{
89 return d->tag;
90}
91
92int QTestData::dataCount() const
93{
94 return d->dataCount;
95}
96
97QT_END_NAMESPACE
98

source code of qtbase/src/testlib/qtestdata.cpp