Qt 6.x
The Qt SDK
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/qmetaobject.h>
9
10#include <string.h>
11#include <vector>
12#include <algorithm>
13
15
17{
18public:
20 {
21 qDeleteAll(dataList.begin(), 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>;
34
35 using DataList = std::vector<QTestData *>;
37
38 void addColumn(int elemType, const char *elemName) { elementList.push_back(Element(elemName, elemType)); }
39 void addRow(QTestData *data) { dataList.push_back(data); }
40 bool hasRow(const char *name) const;
41
44};
45
48
49void QTestTable::addColumn(int type, const char *name)
50{
53 if (indexOf(name) != -1)
54 qWarning() << "Duplicate data column" << name << "- please rename.";
55
56 d->addColumn(type, name);
57}
58
60{
61 return int(d->elementList.size());
62}
63
65{
66 return int(d->dataList.size());
67}
68
70{
71 return d->elementList.empty();
72}
73
75{
76 if (d->hasRow(tag))
77 qWarning("Duplicate data tag \"%s\" - please rename.", tag);
78
79 QTestData *dt = new QTestData(tag, this);
80 d->addRow(dt);
81 return dt;
82}
83
85{
86 d = new QTestTablePrivate;
88}
89
91{
93 delete d;
94}
95
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
107{
108 return size_t(index) < d->dataList.size() ? d->dataList[index] : nullptr;
109}
110
112{
113public:
114 explicit NamePredicate(const char *needle) : m_needle(needle) {}
115
117 { return !strcmp(e.name, m_needle); }
118
119 bool operator()(const QTestData *e) const
120 { return !strcmp(e->dataTag(), 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(dataList.begin(), dataList.end(), 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(elementList.begin(), elementList.end(),
139 NamePredicate(elementName));
140 return it != elementList.end() ?
141 int(it - elementList.begin()) : -1;
142}
143
145{
149}
150
152{
155}
156
158{
160}
161
bool operator()(const QTestTablePrivate::Element &e) const
bool operator()(const QTestData *e) const
NamePredicate(const char *needle)
iterator begin()
Definition qset.h:136
iterator end()
Definition qset.h:140
static QTestTable * currentTestTable
void addColumn(int elemType, const char *elemName)
std::vector< Element > ElementList
ElementList elementList
bool hasRow(const char *name) const
static QTestTable * gTable
std::vector< QTestData * > DataList
void addRow(QTestData *data)
int dataCount() const
int elementCount() const
const char * dataTag(int index) const
bool isEmpty() const
static QTestTable * currentTestTable()
int elementTypeId(int index) const
QTestData * testData(int index) const
QTestData * newData(const char *tag)
static void clearGlobalTestTable()
void addColumn(int elementType, const char *elementName)
static QTestTable * globalTestTable()
int indexOf(const char *elementName) const
qDeleteAll(list.begin(), list.end())
double e
QSet< QString >::iterator it
Combined button and popup list for selecting options.
AudioChannelLayoutTag tag
#define qWarning
Definition qlogging.h:162
GLuint index
[2]
GLenum type
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLfloat n
GLdouble GLdouble t
Definition qopenglext.h:243
#define QTEST_ASSERT(cond)
Definition qtestassert.h:12
Element(const char *n, int t)