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

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