tst_QSqlRecord: replace manual memory management with unique_ptr

Also replace the C array with a std::array and (some) indexed loops
with ranged-for loops. Most loops need the index in one way of
another, so can't easily be converted to ranged.

Pick-to: 6.5 6.4 6.2
Change-Id: I7fa05f22de9df6c68ec5797c9583476a3881532c
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Marc Mutz 2023-01-09 08:07:44 +01:00
parent 1c5e6615c7
commit 70e070ecbd

View File

@ -14,12 +14,7 @@
class tst_QSqlRecord : public QObject
{
Q_OBJECT
public:
tst_QSqlRecord();
virtual ~tst_QSqlRecord();
Q_OBJECT
public slots:
void init();
@ -47,26 +42,11 @@ private slots:
void append();
private:
QSqlRecord* rec;
QSqlField* fields[ NUM_FIELDS ];
std::unique_ptr<QSqlRecord> rec;
std::array<std::unique_ptr<QSqlField>, NUM_FIELDS> fields;
void createTestRecord();
};
tst_QSqlRecord::tst_QSqlRecord()
{
rec = 0;
for ( int i = 0; i < NUM_FIELDS; ++i )
fields[ i ] = 0;
}
tst_QSqlRecord::~tst_QSqlRecord()
{
delete rec;
for ( int i = 0; i < NUM_FIELDS; ++i )
delete fields[ i ];
rec = 0;
}
void tst_QSqlRecord::init()
{
cleanup();
@ -74,31 +54,26 @@ void tst_QSqlRecord::init()
void tst_QSqlRecord::cleanup()
{
delete rec;
for ( int i = 0; i < NUM_FIELDS; ++i ) {
delete fields[ i ];
fields[ i ] = 0;
}
rec = 0;
rec = nullptr;
for (auto &field : fields)
field = nullptr;
}
void tst_QSqlRecord::createTestRecord()
{
delete rec;
rec = new QSqlRecord();
fields[0] = new QSqlField(QStringLiteral("string"), QMetaType(QMetaType::QString), QStringLiteral("stringtable"));
fields[1] = new QSqlField(QStringLiteral("int"), QMetaType(QMetaType::Int), QStringLiteral("inttable"));
fields[2] = new QSqlField(QStringLiteral("double"), QMetaType(QMetaType::Double), QStringLiteral("doubletable"));
fields[3] = new QSqlField(QStringLiteral("bool"), QMetaType(QMetaType::Bool));
for ( int i = 0; i < NUM_FIELDS; ++i )
rec->append( *(fields[ i ] ) );
rec = std::make_unique<QSqlRecord>();
fields[0] = std::make_unique<QSqlField>(QStringLiteral("string"), QMetaType(QMetaType::QString), QStringLiteral("stringtable"));
fields[1] = std::make_unique<QSqlField>(QStringLiteral("int"), QMetaType(QMetaType::Int), QStringLiteral("inttable"));
fields[2] = std::make_unique<QSqlField>(QStringLiteral("double"), QMetaType(QMetaType::Double), QStringLiteral("doubletable"));
fields[3] = std::make_unique<QSqlField>(QStringLiteral("bool"), QMetaType(QMetaType::Bool));
for (const auto &field : fields)
rec->append(*field);
}
void tst_QSqlRecord::append()
{
delete rec;
rec = new QSqlRecord();
rec = std::make_unique<QSqlRecord>();
rec->append(QSqlField("string", QMetaType(QMetaType::QString), QStringLiteral("stringtable")));
QCOMPARE( rec->field( 0 ).name(), (QString) "string" );
QCOMPARE(rec->field(0).tableName(), QStringLiteral("stringtable"));
@ -157,10 +132,7 @@ void tst_QSqlRecord::clearValues()
QFETCH( double, dval );
QFETCH( int, bval );
if(rec)
delete rec;
rec = new QSqlRecord();
rec = std::make_unique<QSqlRecord>();
rec->append( QSqlField( "string", QMetaType(QMetaType::QString) ) );
QCOMPARE( rec->field(0).name(), (QString) "string" );
QVERIFY( !rec->isEmpty() );
@ -200,8 +172,8 @@ void tst_QSqlRecord::clearValues()
void tst_QSqlRecord::contains()
{
createTestRecord();
for ( int i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->contains( fields[ i ]->name() ) );
for (const auto &field : fields)
QVERIFY(rec->contains(field->name()));
QVERIFY( !rec->contains( "__Harry__" ) );
}
@ -233,8 +205,8 @@ void tst_QSqlRecord::fieldName()
{
createTestRecord();
for ( int i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->field( (fields[ i ] )->name() ) == *( fields[ i ] ) );
for (const auto &field : fields)
QVERIFY(rec->field(field->name()) == *field);
QVERIFY( rec->fieldName( NUM_FIELDS ).isNull() );
}
@ -412,8 +384,7 @@ void tst_QSqlRecord::setValue()
{
int i;
delete rec;
rec = new QSqlRecord();
rec = std::make_unique<QSqlRecord>();
rec->append( QSqlField( "string", QMetaType(QMetaType::QString) ) );
QCOMPARE( rec->field( 0 ).name(), (QString) "string" );
QVERIFY( !rec->isEmpty() );