From 9b6eacab99673d7d9848b341c4cf36a7c35f07c0 Mon Sep 17 00:00:00 2001 From: Frans Englich Date: Mon, 13 Jul 2009 13:02:20 +0200 Subject: QFlags::testFlag(): handle the zero case appropriately. Brought up by Andy. See perforce change 314809, 17b07e3ab6192b31f77fd2f126705b9ab53b3937. Related to task 221708. Reviewed-By: Andy Shaw (cherry picked from commit cc24c46c117248ecb98200416e7f25375e6bb476) --- src/corelib/global/qglobal.h | 2 +- tests/auto/qflags/tst_qflags.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 63f6c31..f650bd2 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -2017,7 +2017,7 @@ public: inline bool operator!() const { return !i; } - inline bool testFlag(Enum f) const { return (i & f) == f; } + inline bool testFlag(Enum f) const { return (i & f) == f && (f != 0 || i == f ); } }; #define Q_DECLARE_FLAGS(Flags, Enum)\ diff --git a/tests/auto/qflags/tst_qflags.cpp b/tests/auto/qflags/tst_qflags.cpp index a5f68dc..87d8258 100644 --- a/tests/auto/qflags/tst_qflags.cpp +++ b/tests/auto/qflags/tst_qflags.cpp @@ -45,6 +45,7 @@ class tst_QFlags: public QObject Q_OBJECT private slots: void testFlag() const; + void testFlagZeroFlag() const; void testFlagMultiBits() const; }; @@ -59,8 +60,31 @@ void tst_QFlags::testFlag() const QVERIFY(!btn.testFlag(Qt::LeftButton)); } +void tst_QFlags::testFlagZeroFlag() const +{ + { + Qt::MouseButtons btn = Qt::LeftButton | Qt::RightButton; + /* Qt::NoButton has the value 0. */ + + QVERIFY(!btn.testFlag(Qt::NoButton)); + } + + { + /* A zero enum set should test true with zero. */ + QVERIFY(Qt::MouseButtons().testFlag(Qt::NoButton)); + } + + { + Qt::MouseButtons btn = Qt::NoButton; + QVERIFY(btn.testFlag(Qt::NoButton)); + } +} + void tst_QFlags::testFlagMultiBits() const { + /* Qt::Window is 0x00000001 + * Qt::Dialog is 0x00000002 | Window + */ { const Qt::WindowFlags onlyWindow(Qt::Window); QVERIFY(!onlyWindow.testFlag(Qt::Dialog)); -- cgit v0.12