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 QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtTest/private/qtesttable_p.h>
41#include <QtTest/qtestdata.h>
42#include <QtTest/qtestassert.h>
43
44#include <QtCore/qmetaobject.h>
45
46#include <string.h>
47#include <vector>
48#include <algorithm>
49
50QT_BEGIN_NAMESPACE
51
52class QTestTablePrivate
53{
54public:
55 ~QTestTablePrivate()
56 {
57 qDeleteAll(dataList.begin(), dataList.end());
58 }
59
60 struct Element {
61 Element() = default;
62 Element(const char *n, int t) : name(n), type(t) {}
63
64 const char *name = nullptr;
65 int type = 0;
66 };
67
68 using ElementList = std::vector<Element>;
69 ElementList elementList;
70
71 using DataList = std::vector<QTestData *>;
72 DataList dataList;
73
74 void addColumn(int elemType, const char *elemName) { elementList.push_back(Element(elemName, elemType)); }
75 void addRow(QTestData *data) { dataList.push_back(data); }
76
77 static QTestTable *currentTestTable;
78 static QTestTable *gTable;
79};
80
81QTestTable *QTestTablePrivate::currentTestTable = nullptr;
82QTestTable *QTestTablePrivate::gTable = nullptr;
83
84void QTestTable::addColumn(int type, const char *name)
85{
86 QTEST_ASSERT(type);
87 QTEST_ASSERT(name);
88
89 d->addColumn(type, name);
90}
91
92int QTestTable::elementCount() const
93{
94 return int(d->elementList.size());
95}
96
97int QTestTable::dataCount() const
98{
99 return int(d->dataList.size());
100}
101
102bool QTestTable::isEmpty() const
103{
104 return d->elementList.empty();
105}
106
107QTestData *QTestTable::newData(const char *tag)
108{
109 QTestData *dt = new QTestData(tag, this);
110 d->addRow(dt);
111 return dt;
112}
113
114QTestTable::QTestTable()
115{
116 d = new QTestTablePrivate;
117 QTestTablePrivate::currentTestTable = this;
118}
119
120QTestTable::~QTestTable()
121{
122 QTestTablePrivate::currentTestTable = nullptr;
123 delete d;
124}
125
126int QTestTable::elementTypeId(int index) const
127{
128 return size_t(index) < d->elementList.size() ? d->elementList[index].type : -1;
129}
130
131const char *QTestTable::dataTag(int index) const
132{
133 return size_t(index) < d->elementList.size() ? d->elementList[index].name : nullptr;
134}
135
136QTestData *QTestTable::testData(int index) const
137{
138 return size_t(index) < d->dataList.size() ? d->dataList[index] : nullptr;
139}
140
141class NamePredicate
142{
143public:
144 explicit NamePredicate(const char *needle) : m_needle(needle) {}
145
146 bool operator()(const QTestTablePrivate::Element &e) const
147 { return !strcmp(e.name, m_needle); }
148
149private:
150 const char *m_needle;
151};
152
153int QTestTable::indexOf(const char *elementName) const
154{
155 QTEST_ASSERT(elementName);
156
157 const QTestTablePrivate::ElementList &elementList = d->elementList;
158
159 const auto it = std::find_if(elementList.begin(), elementList.end(),
160 NamePredicate(elementName));
161 return it != elementList.end() ?
162 int(it - elementList.begin()) : -1;
163}
164
165QTestTable *QTestTable::globalTestTable()
166{
167 if (!QTestTablePrivate::gTable)
168 QTestTablePrivate::gTable = new QTestTable();
169 return QTestTablePrivate::gTable;
170}
171
172void QTestTable::clearGlobalTestTable()
173{
174 delete QTestTablePrivate::gTable;
175 QTestTablePrivate::gTable = nullptr;
176}
177
178QTestTable *QTestTable::currentTestTable()
179{
180 return QTestTablePrivate::currentTestTable;
181}
182
183QT_END_NAMESPACE
184

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