summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtesttable.cpp
blob: 2276365505e61a286ed3c3cda4e2df5443d6e353 (plain)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
// Copyright (C) 2016 The Qt Company Ltd.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#include <QtTest/private/qtesttable_p.h>#include <QtTest/qtestdata.h>#include <QtTest/qtestassert.h>#include <QtCore/private/qduplicatetracker_p.h>#include <QtCore/qmetaobject.h>#include <string.h>#include <vector>#include <algorithm> QT_BEGIN_NAMESPACE class QTestTablePrivate {public:~QTestTablePrivate(){qDeleteAll(dataList.begin(), dataList.end());}struct Element {Element() =default;Element(const char*n,int t) :name(n),type(t) {}const char*name =nullptr;int type =0;};using ElementList =std::vector<Element>; ElementList elementList;using DataList =std::vector<QTestData *>; DataList dataList;using TagSet = QDuplicateTracker<std::string>; TagSet tagSet;voidaddColumn(int elemType,const char*elemName) { elementList.push_back(Element(elemName, elemType)); }voidaddRow(QTestData *data) { dataList.push_back(data); }static QTestTable *currentTestTable;static QTestTable *gTable;}; QTestTable *QTestTablePrivate::currentTestTable =nullptr; QTestTable *QTestTablePrivate::gTable =nullptr;voidQTestTable::addColumn(int type,const char*name){QTEST_ASSERT(type);QTEST_ASSERT(name);if(indexOf(name) != -1)qWarning() <<"Duplicate data column"<< name <<"- please rename."; d->addColumn(type, name);}intQTestTable::elementCount()const{returnint(d->elementList.size());}intQTestTable::dataCount()const{returnint(d->dataList.size());}boolQTestTable::isEmpty()const{return d->elementList.empty();} QTestData *QTestTable::newData(const char*tag){QTEST_ASSERT(tag);if(d->tagSet.hasSeen(tag))qWarning("Duplicate data tag\"%s\"- please rename.", tag); QTestData *dt =newQTestData(tag,this); d->addRow(dt);return dt;}QTestTable::QTestTable(){ d =new QTestTablePrivate;QTestTablePrivate::currentTestTable =this;}QTestTable::~QTestTable(){QTestTablePrivate::currentTestTable =nullptr;delete d;}intQTestTable::elementTypeId(int index)const{returnsize_t(index) < d->elementList.size() ? d->elementList[index].type : -1;}const char*QTestTable::dataTag(int index)const{returnsize_t(index) < d->elementList.size() ? d->elementList[index].name :nullptr;} QTestData *QTestTable::testData(int index)const{returnsize_t(index) < d->dataList.size() ? d->dataList[index] :nullptr;}class NamePredicate {public:explicitNamePredicate(const char*needle) :m_needle(needle) {}booloperator()(constQTestTablePrivate::Element &e)const{return!strcmp(e.name, m_needle); }booloperator()(const QTestData *e)const{return!strcmp(e->dataTag(), m_needle); }private:const char*m_needle;};intQTestTable::indexOf(const char*elementName)const{QTEST_ASSERT(elementName);constQTestTablePrivate::ElementList &elementList = d->elementList;constauto it =std::find_if(elementList.begin(), elementList.end(),NamePredicate(elementName));return it != elementList.end() ?int(it - elementList.begin()) : -1;} QTestTable *QTestTable::globalTestTable(){if(!QTestTablePrivate::gTable)QTestTablePrivate::gTable =newQTestTable();returnQTestTablePrivate::gTable;}voidQTestTable::clearGlobalTestTable(){deleteQTestTablePrivate::gTable;QTestTablePrivate::gTable =nullptr;} QTestTable *QTestTable::currentTestTable(){returnQTestTablePrivate::currentTestTable;} QT_END_NAMESPACE 
close