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/qmetaobject.h>
9
10#include <string.h>
11#include <vector>
12#include <algorithm>
13
14QT_BEGIN_NAMESPACE
15
16class QTestTablePrivate
17{
18public:
19 ~QTestTablePrivate()
20 {
21 qDeleteAll(begin: dataList.begin(), end: dataList.end());
22 }
23
24 struct Element {
25 Element() = default;
26 Element(const char *n, int t) : name(n), type(t) {}
27
28 const char *name = nullptr;
29 int type = 0;
30 };
31
32 using ElementList = std::vector<Element>;
33 ElementList elementList;
34
35 using DataList = std::vector<QTestData *>;
36 DataList dataList;
37
38 void addColumn(int elemType, const char *elemName) { elementList.push_back(x: Element(elemName, elemType)); }
39 void addRow(QTestData *data) { dataList.push_back(x: data); }
40 bool hasRow(const char *name) const;
41
42 static QTestTable *currentTestTable;
43 static QTestTable *gTable;
44};
45
46QTestTable *QTestTablePrivate::currentTestTable = nullptr;
47QTestTable *QTestTablePrivate::gTable = nullptr;
48
49void QTestTable::addColumn(int type, const char *name)
50{
51 QTEST_ASSERT(type);
52 QTEST_ASSERT(name);
53 if (indexOf(elementName: name) != -1)
54 qWarning() << "Duplicate data column" << name << "- please rename.";
55
56 d->addColumn(elemType: type, elemName: name);
57}
58
59int QTestTable::elementCount() const
60{
61 return int(d->elementList.size());
62}
63
64int QTestTable::dataCount() const
65{
66 return int(d->dataList.size());
67}
68
69bool QTestTable::isEmpty() const
70{
71 return d->elementList.empty();
72}
73
74QTestData *QTestTable::newData(const char *tag)
75{
76 if (d->hasRow(name: tag))
77 qWarning(msg: "Duplicate data tag \"%s\" - please rename.", tag);
78
79 QTestData *dt = new QTestData(tag, this);
80 d->addRow(data: dt);
81 return dt;
82}
83
84QTestTable::QTestTable()
85{
86 d = new QTestTablePrivate;
87 QTestTablePrivate::currentTestTable = this;
88}
89
90QTestTable::~QTestTable()
91{
92 QTestTablePrivate::currentTestTable = nullptr;
93 delete d;
94}
95
96int QTestTable::elementTypeId(int index) const
97{
98 return size_t(index) < d->elementList.size() ? d->elementList[index].type : -1;
99}
100
101const char *QTestTable::dataTag(int index) const
102{
103 return size_t(index) < d->elementList.size() ? d->elementList[index].name : nullptr;
104}
105
106QTestData *QTestTable::testData(int index) const
107{
108 return size_t(index) < d->dataList.size() ? d->dataList[index] : nullptr;
109}
110
111class NamePredicate
112{
113public:
114 explicit NamePredicate(const char *needle) : m_needle(needle) {}
115
116 bool operator()(const QTestTablePrivate::Element &e) const
117 { return !strcmp(s1: e.name, s2: m_needle); }
118
119 bool operator()(const QTestData *e) const
120 { return !strcmp(s1: e->dataTag(), s2: m_needle); }
121
122private:
123 const char *m_needle;
124};
125
126bool QTestTablePrivate::hasRow(const char *rowName) const
127{
128 QTEST_ASSERT(rowName);
129 return std::find_if(first: dataList.begin(), last: dataList.end(), pred: NamePredicate(rowName)) != dataList.end();
130}
131
132int QTestTable::indexOf(const char *elementName) const
133{
134 QTEST_ASSERT(elementName);
135
136 const QTestTablePrivate::ElementList &elementList = d->elementList;
137
138 const auto it = std::find_if(first: elementList.begin(), last: elementList.end(),
139 pred: NamePredicate(elementName));
140 return it != elementList.end() ?
141 int(it - elementList.begin()) : -1;
142}
143
144QTestTable *QTestTable::globalTestTable()
145{
146 if (!QTestTablePrivate::gTable)
147 QTestTablePrivate::gTable = new QTestTable();
148 return QTestTablePrivate::gTable;
149}
150
151void QTestTable::clearGlobalTestTable()
152{
153 delete QTestTablePrivate::gTable;
154 QTestTablePrivate::gTable = nullptr;
155}
156
157QTestTable *QTestTable::currentTestTable()
158{
159 return QTestTablePrivate::currentTestTable;
160}
161
162QT_END_NAMESPACE
163

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