From 405244fe301ac18d20aae245ba2faafaec74e453 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 22 Nov 2020 18:25:44 +0100 Subject: Fix QVariant/QMetaType::compare APIs std::optional is the wrong datatype to use for compare. First and foremost, it can't be used in the idiomatic form of auto r = a.compare(b); if (r < 0) ~~~ // a is less than b if (r > 0) ~~~ // a is greater than b which we *already* feature in Qt (QString, QByteArray). Also, std::optional (explicitly) converts to bool, which is a trap, because the result of the comparison can be accidentally tested as a bool: if (a.compare(b)) ~~~ // oops! does NOT mean a API, and use that. This commits just adds the necessary parts for compare() (i.e. partial ordering), the rest of (classes, functions, conversions) can be added to 6.1. Change-Id: I2b5522da47854da39f79993e1207fad033786f00 Reviewed-by: Volker Hilsheimer (cherry picked from commit 3e59c97c3453926fc66479d9ceca03901df55f90) Reviewed-by: Qt Cherry-pick Bot --- tests/auto/corelib/global/CMakeLists.txt | 1 + tests/auto/corelib/global/global.pro | 1 + tests/auto/corelib/global/qcompare/CMakeLists.txt | 13 ++ tests/auto/corelib/global/qcompare/qcompare.pro | 8 ++ .../auto/corelib/global/qcompare/tst_qcompare.cpp | 134 +++++++++++++++++++++ .../corelib/kernel/qmetatype/tst_qmetatype.cpp | 6 +- 6 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 tests/auto/corelib/global/qcompare/CMakeLists.txt create mode 100644 tests/auto/corelib/global/qcompare/qcompare.pro create mode 100644 tests/auto/corelib/global/qcompare/tst_qcompare.cpp (limited to 'tests/auto') diff --git a/tests/auto/corelib/global/CMakeLists.txt b/tests/auto/corelib/global/CMakeLists.txt index 91890462fe..8ba71543c2 100644 --- a/tests/auto/corelib/global/CMakeLists.txt +++ b/tests/auto/corelib/global/CMakeLists.txt @@ -1,5 +1,6 @@ # Generated from global.pro. +add_subdirectory(qcompare) add_subdirectory(qflags) add_subdirectory(q_func_info) add_subdirectory(qgetputenv) diff --git a/tests/auto/corelib/global/global.pro b/tests/auto/corelib/global/global.pro index 80ac1a3f83..b3f3be08a5 100644 --- a/tests/auto/corelib/global/global.pro +++ b/tests/auto/corelib/global/global.pro @@ -1,5 +1,6 @@ TEMPLATE=subdirs SUBDIRS=\ + qcompare \ qflags \ q_func_info \ qgetputenv \ diff --git a/tests/auto/corelib/global/qcompare/CMakeLists.txt b/tests/auto/corelib/global/qcompare/CMakeLists.txt new file mode 100644 index 0000000000..0fbf696d0e --- /dev/null +++ b/tests/auto/corelib/global/qcompare/CMakeLists.txt @@ -0,0 +1,13 @@ +# Generated from qcompare.pro. + +##################################################################### +## tst_qcompare Test: +##################################################################### + +qt_internal_add_test(tst_qcompare + SOURCES + tst_qcompare.cpp +) + +## Scopes: +##################################################################### diff --git a/tests/auto/corelib/global/qcompare/qcompare.pro b/tests/auto/corelib/global/qcompare/qcompare.pro new file mode 100644 index 0000000000..0e2cd2771c --- /dev/null +++ b/tests/auto/corelib/global/qcompare/qcompare.pro @@ -0,0 +1,8 @@ +CONFIG += testcase +TARGET = tst_qcompare +QT = core testlib +SOURCES = tst_qcompare.cpp +qtConfig(c++11): CONFIG += c++11 +qtConfig(c++14): CONFIG += c++14 +qtConfig(c++17): CONFIG += c++17 +qtConfig(c++2a): CONFIG += c++2a diff --git a/tests/auto/corelib/global/qcompare/tst_qcompare.cpp b/tests/auto/corelib/global/qcompare/tst_qcompare.cpp new file mode 100644 index 0000000000..61ea0eddc4 --- /dev/null +++ b/tests/auto/corelib/global/qcompare/tst_qcompare.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +class tst_QCompare: public QObject +{ + Q_OBJECT +private slots: + void partialOrdering(); +}; + +void tst_QCompare::partialOrdering() +{ + static_assert(QPartialOrdering::Unordered == QPartialOrdering::Unordered); + static_assert(QPartialOrdering::Unordered != QPartialOrdering::Less); + static_assert(QPartialOrdering::Unordered != QPartialOrdering::Equivalent); + static_assert(QPartialOrdering::Unordered != QPartialOrdering::Greater); + + static_assert(QPartialOrdering::Less != QPartialOrdering::Unordered); + static_assert(QPartialOrdering::Less == QPartialOrdering::Less); + static_assert(QPartialOrdering::Less != QPartialOrdering::Equivalent); + static_assert(QPartialOrdering::Less != QPartialOrdering::Greater); + + static_assert(QPartialOrdering::Equivalent != QPartialOrdering::Unordered); + static_assert(QPartialOrdering::Equivalent != QPartialOrdering::Less); + static_assert(QPartialOrdering::Equivalent == QPartialOrdering::Equivalent); + static_assert(QPartialOrdering::Equivalent != QPartialOrdering::Greater); + + static_assert(QPartialOrdering::Greater != QPartialOrdering::Unordered); + static_assert(QPartialOrdering::Greater != QPartialOrdering::Less); + static_assert(QPartialOrdering::Greater != QPartialOrdering::Equivalent); + static_assert(QPartialOrdering::Greater == QPartialOrdering::Greater); + + + static_assert(!(QPartialOrdering::Unordered == 0)); + static_assert(!(QPartialOrdering::Unordered != 0)); + static_assert(!(QPartialOrdering::Unordered < 0)); + static_assert(!(QPartialOrdering::Unordered <= 0)); + static_assert(!(QPartialOrdering::Unordered > 0)); + static_assert(!(QPartialOrdering::Unordered >= 0)); + + static_assert(!(0 == QPartialOrdering::Unordered)); + static_assert(!(0 != QPartialOrdering::Unordered)); + static_assert(!(0 < QPartialOrdering::Unordered)); + static_assert(!(0 <= QPartialOrdering::Unordered)); + static_assert(!(0 > QPartialOrdering::Unordered)); + static_assert(!(0 >= QPartialOrdering::Unordered)); + + + static_assert(!(QPartialOrdering::Less == 0)); + static_assert( (QPartialOrdering::Less != 0)); + static_assert( (QPartialOrdering::Less < 0)); + static_assert( (QPartialOrdering::Less <= 0)); + static_assert(!(QPartialOrdering::Less > 0)); + static_assert(!(QPartialOrdering::Less >= 0)); + + static_assert(!(0 == QPartialOrdering::Less)); + static_assert( (0 != QPartialOrdering::Less)); + static_assert(!(0 < QPartialOrdering::Less)); + static_assert(!(0 <= QPartialOrdering::Less)); + static_assert( (0 > QPartialOrdering::Less)); + static_assert( (0 >= QPartialOrdering::Less)); + + + static_assert( (QPartialOrdering::Equivalent == 0)); + static_assert(!(QPartialOrdering::Equivalent != 0)); + static_assert(!(QPartialOrdering::Equivalent < 0)); + static_assert( (QPartialOrdering::Equivalent <= 0)); + static_assert(!(QPartialOrdering::Equivalent > 0)); + static_assert( (QPartialOrdering::Equivalent >= 0)); + + static_assert( (0 == QPartialOrdering::Equivalent)); + static_assert(!(0 != QPartialOrdering::Equivalent)); + static_assert(!(0 < QPartialOrdering::Equivalent)); + static_assert( (0 <= QPartialOrdering::Equivalent)); + static_assert(!(0 > QPartialOrdering::Equivalent)); + static_assert( (0 >= QPartialOrdering::Equivalent)); + + + static_assert(!(QPartialOrdering::Greater == 0)); + static_assert( (QPartialOrdering::Greater != 0)); + static_assert(!(QPartialOrdering::Greater < 0)); + static_assert(!(QPartialOrdering::Greater <= 0)); + static_assert( (QPartialOrdering::Greater > 0)); + static_assert( (QPartialOrdering::Greater >= 0)); + + static_assert(!(0 == QPartialOrdering::Greater)); + static_assert( (0 != QPartialOrdering::Greater)); + static_assert( (0 < QPartialOrdering::Greater)); + static_assert( (0 <= QPartialOrdering::Greater)); + static_assert(!(0 > QPartialOrdering::Greater)); + static_assert(!(0 >= QPartialOrdering::Greater)); +} + +QTEST_MAIN(tst_QCompare) +#include "tst_qcompare.moc" diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 5a4ade5e47..b49f770919 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -2576,17 +2576,17 @@ void tst_QMetaType::compareCustomEqualOnlyType() // check QMetaType::compare works/doesn't crash for equals only comparators auto cmp = type.compare(variant50.constData(), variant50.constData()); - QVERIFY(!cmp); + QCOMPARE(cmp, QPartialOrdering::Unordered); bool equals = type.equals(variant50.constData(), variant50.constData()); QVERIFY(equals); cmp = type.compare(variant100.constData(), variant100x.constData()); - QVERIFY(!cmp); + QCOMPARE(cmp, QPartialOrdering::Unordered); equals = type.equals(variant100.constData(), variant100x.constData()); QVERIFY(equals); cmp = type.compare(variant50.constData(), variant100.constData()); - QVERIFY(!cmp); + QCOMPARE(cmp, QPartialOrdering::Unordered); equals = type.equals(variant50.constData(), variant100.constData()); QVERIFY(!equals); -- cgit v1.2.3