From 82a6f38873b75c75b345c4eea1a378fd867f5e69 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 29 Nov 2016 16:56:42 +0100 Subject: [PATCH] tst_qsqlquery.cpp: Refactor runIntegralTypesMysqlTest() Change 3370ab9119df09ca14f7d4641c555e60c1b3f478 introduced warnings from MSVC: tst_qsqlquery.cpp(4005): warning C4805: '==': unsafe mix of type 'const bool' and type 'int' in operation tst_qsqlquery.cpp(4059): note: see reference to function template instantiation 'void runIntegralTypesMysqlTest(QSqlDatabase &,const QString &,const QString &,const bool,const T,const T)' being compiled with [ T=bool ] tst_qsqlquery.cpp(4006): warning C4805: '==': unsafe mix of type 'const bool' and type 'int' in operation tst_qsqlquery.cpp(4006): warning C4804: '/': unsafe use of type 'bool' in operation tst_qsqlquery.cpp(4026): warning C4804: '+=': unsafe use of type 'bool' in operation Extract an overload taking a QVector of values and use that for the bool case instead of looping over min/max to generate a sequence of values for bool. Change-Id: I72583774e788b8df899f22ed1a64278217e664f6 Reviewed-by: Milian Wolff --- .../sql/kernel/qsqlquery/tst_qsqlquery.cpp | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp index 84e9643e773..8deb5ddf8ff 100644 --- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp @@ -3995,35 +3995,29 @@ void tst_QSqlQuery::aggregateFunctionTypes() } template -void runIntegralTypesMysqlTest(QSqlDatabase &db, const QString &tableName, const QString &type, const bool withPreparedStatement, - const T min = std::numeric_limits::min(), const T max = std::numeric_limits::max()) +void runIntegralTypesMysqlTest(QSqlDatabase &db, const QString &tableName, + const QString &type, bool withPreparedStatement, + const QVector &values) { + QVector variantValues; + variantValues.reserve(values.size()); + QSqlQuery q(db); QVERIFY_SQL(q, exec("DROP TABLE IF EXISTS " + tableName)); QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + " (id " + type + ')')); - const int steps = (max == min + 1) ? 2 : 20; - const T increment = (max == min + 1) ? 1 : (max / steps - min / steps); - - // insert some values - QVector values; - QVector variantValues; - values.resize(steps); - variantValues.resize(steps); - T v = min; if (withPreparedStatement) { QVERIFY_SQL(q, prepare("INSERT INTO " + tableName + " (id) VALUES (?)")); } for (int i = 0; i < values.size(); ++i) { + const T v = values.at(i); if (withPreparedStatement) { q.bindValue(0, v); QVERIFY_SQL(q, exec()); } else { QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (" + QString::number(v) + QLatin1Char(')'))); } - values[i] = v; - variantValues[i] = QVariant::fromValue(v); - v += increment; + variantValues.append(QVariant::fromValue(v)); } // ensure we can read them back properly @@ -4048,16 +4042,34 @@ void runIntegralTypesMysqlTest(QSqlDatabase &db, const QString &tableName, const QCOMPARE(actualVariantValues, variantValues); } +template +void runIntegralTypesMysqlTest(QSqlDatabase &db, const QString &tableName, + const QString &type, const bool withPreparedStatement, + const T min = std::numeric_limits::min(), + const T max = std::numeric_limits::max()) +{ + // insert some values + const int steps = 20; + const T increment = (max / steps - min / steps); + QVector values; + values.reserve(steps); + T v = min; + for (int i = 0; i < steps; ++i, v += increment) + values.append(v); + runIntegralTypesMysqlTest(db, tableName, type, withPreparedStatement, values); +} + void tst_QSqlQuery::integralTypesMysql() { QFETCH(QString, dbName); QSqlDatabase db = QSqlDatabase::database(dbName); CHECK_DATABASE(db); + const QVector boolValues = QVector() << false << true; for (int i = 0; i < 2; ++i) { const bool withPreparedStatement = (i == 1); - runIntegralTypesMysqlTest(db, "tinyInt1Test", "TINYINT(1)", withPreparedStatement); - runIntegralTypesMysqlTest(db, "unsignedTinyInt1Test", "TINYINT(1) UNSIGNED", withPreparedStatement); + runIntegralTypesMysqlTest(db, "tinyInt1Test", "TINYINT(1)", withPreparedStatement, boolValues); + runIntegralTypesMysqlTest(db, "unsignedTinyInt1Test", "TINYINT(1) UNSIGNED", withPreparedStatement, boolValues); runIntegralTypesMysqlTest(db, "tinyIntTest", "TINYINT", withPreparedStatement); runIntegralTypesMysqlTest(db, "unsignedTinyIntTest", "TINYINT UNSIGNED", withPreparedStatement); runIntegralTypesMysqlTest(db, "smallIntTest", "SMALLINT", withPreparedStatement);