Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qtesttable.cpp
Go to the documentation of this file.
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
<
QtTest
/
private
/
qtesttable_p
.
h
>
5
#
include
<
QtTest
/
qtestdata
.
h
>
6
#
include
<
QtTest
/
qtestassert
.
h
>
7
8
#
include
<
QtCore
/
private
/
qduplicatetracker_p
.
h
>
9
#
include
<
QtCore
/
qmetaobject
.
h
>
10
11
#
include
<
string
.
h
>
12
#
include
<
vector
>
13
#
include
<
algorithm
>
14
15
QT_BEGIN_NAMESPACE
16
17
class
QTestTablePrivate
18
{
19
public
:
20
~QTestTablePrivate()
21
{
22
qDeleteAll(dataList.begin(), dataList.end());
23
}
24
25
struct
Element {
26
Element() =
default
;
27
Element(
const
char
*n,
int
t) : name(n), type(t) {}
28
29
const
char
*name =
nullptr
;
30
int
type = 0;
31
};
32
33
using
ElementList = std::vector<Element>;
34
ElementList elementList;
35
36
using
DataList = std::vector<QTestData *>;
37
DataList dataList;
38
39
using
TagSet = QDuplicateTracker<std::string>;
40
TagSet tagSet;
41
42
void
addColumn(
int
elemType,
const
char
*elemName) { elementList.push_back(Element(elemName, elemType)); }
43
void
addRow(QTestData *data) { dataList.push_back(data); }
44
45
static
QTestTable *currentTestTable;
46
static
QTestTable *gTable;
47
};
48
49
QTestTable *QTestTablePrivate::currentTestTable =
nullptr
;
50
QTestTable *QTestTablePrivate::gTable =
nullptr
;
51
52
void
QTestTable::addColumn(
int
type,
const
char
*name)
53
{
54
QTEST_ASSERT(type);
55
QTEST_ASSERT(name);
56
if
(indexOf(name) != -1)
57
qWarning() <<
"Duplicate data column"
<< name <<
"- please rename."
;
58
59
d->addColumn(type, name);
60
}
61
62
int
QTestTable::elementCount()
const
63
{
64
return
int
(d->elementList.size());
65
}
66
67
int
QTestTable::dataCount()
const
68
{
69
return
int
(d->dataList.size());
70
}
71
72
bool
QTestTable::isEmpty()
const
73
{
74
return
d->elementList.empty();
75
}
76
77
QTestData *QTestTable::newData(
const
char
*tag)
78
{
79
QTEST_ASSERT(tag);
80
if
(d->tagSet.hasSeen(tag))
81
qWarning(
"Duplicate data tag \"%s\" - please rename."
, tag);
82
83
QTestData *dt =
new
QTestData(tag,
this
);
84
d->addRow(dt);
85
return
dt;
86
}
87
88
QTestTable::QTestTable()
89
{
90
d =
new
QTestTablePrivate;
91
QTestTablePrivate::currentTestTable =
this
;
92
}
93
94
QTestTable::~QTestTable()
95
{
96
QTestTablePrivate::currentTestTable =
nullptr
;
97
delete
d;
98
}
99
100
int
QTestTable::elementTypeId(
int
index)
const
101
{
102
return
size_t(index) < d->elementList.size() ? d->elementList[index].type : -1;
103
}
104
105
const
char
*QTestTable::dataTag(
int
index)
const
106
{
107
return
size_t(index) < d->elementList.size() ? d->elementList[index].name :
nullptr
;
108
}
109
110
QTestData *QTestTable::testData(
int
index)
const
111
{
112
return
size_t(index) < d->dataList.size() ? d->dataList[index] :
nullptr
;
113
}
114
115
class
NamePredicate
116
{
117
public
:
118
explicit
NamePredicate
(
const
char
*needle) : m_needle(needle) {}
119
120
bool
operator
()(
const
QTestTablePrivate::Element &e)
const
121
{
return
!strcmp(e.name, m_needle); }
122
123
bool
operator
()(
const
QTestData *e)
const
124
{
return
!strcmp(e->dataTag(), m_needle); }
125
126
private
:
127
const
char
*m_needle;
128
};
129
130
int
QTestTable::indexOf(
const
char
*elementName)
const
131
{
132
QTEST_ASSERT(elementName);
133
134
const
QTestTablePrivate::ElementList &elementList = d->elementList;
135
136
const
auto
it = std::find_if(elementList.begin(), elementList.end(),
137
NamePredicate(elementName));
138
return
it != elementList.end() ?
139
int
(it - elementList.begin()) : -1;
140
}
141
142
QTestTable *QTestTable::globalTestTable()
143
{
144
if
(!QTestTablePrivate::gTable)
145
QTestTablePrivate::gTable =
new
QTestTable();
146
return
QTestTablePrivate::gTable;
147
}
148
149
void
QTestTable::clearGlobalTestTable()
150
{
151
delete
QTestTablePrivate::gTable;
152
QTestTablePrivate::gTable =
nullptr
;
153
}
154
155
QTestTable *QTestTable::currentTestTable()
156
{
157
return
QTestTablePrivate::currentTestTable;
158
}
159
160
QT_END_NAMESPACE
NamePredicate
Definition
qtesttable.cpp:116
NamePredicate::operator()
bool operator()(const QTestTablePrivate::Element &e) const
Definition
qtesttable.cpp:120
NamePredicate::operator()
bool operator()(const QTestData *e) const
Definition
qtesttable.cpp:123
NamePredicate::NamePredicate
NamePredicate(const char *needle)
Definition
qtesttable.cpp:118
qtbase
src
testlib
qtesttable.cpp
Generated on
for Qt by
1.18.0