1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
41 #include <QtTest/QtTest>
43 class tst_QFlags: public QObject
47 void testFlag() const;
48 void testFlagZeroFlag() const;
49 void testFlagMultiBits() const;
53 void tst_QFlags::testFlag() const
55 Qt::MouseButtons btn = Qt::LeftButton | Qt::RightButton;
57 QVERIFY(btn.testFlag(Qt::LeftButton));
58 QVERIFY(!btn.testFlag(Qt::MidButton));
61 QVERIFY(!btn.testFlag(Qt::LeftButton));
64 void tst_QFlags::testFlagZeroFlag() const
67 Qt::MouseButtons btn = Qt::LeftButton | Qt::RightButton;
68 /* Qt::NoButton has the value 0. */
70 QVERIFY(!btn.testFlag(Qt::NoButton));
74 /* A zero enum set should test true with zero. */
75 QVERIFY(Qt::MouseButtons().testFlag(Qt::NoButton));
79 Qt::MouseButtons btn = Qt::NoButton;
80 QVERIFY(btn.testFlag(Qt::NoButton));
84 void tst_QFlags::testFlagMultiBits() const
86 /* Qt::Window is 0x00000001
87 * Qt::Dialog is 0x00000002 | Window
90 const Qt::WindowFlags onlyWindow(Qt::Window);
91 QVERIFY(!onlyWindow.testFlag(Qt::Dialog));
95 const Qt::WindowFlags hasDialog(Qt::Dialog);
96 QVERIFY(hasDialog.testFlag(Qt::Dialog));
100 template <int N, typename T> bool verifyConstExpr(T n) { return n == N; }
102 void tst_QFlags::constExpr()
104 #ifdef Q_COMPILER_CONSTEXPR
105 Qt::MouseButtons btn = Qt::LeftButton | Qt::RightButton;
107 case Qt::LeftButton: QVERIFY(false); break;
108 case Qt::RightButton: QVERIFY(false); break;
109 case Qt::LeftButton | Qt::RightButton: QVERIFY(true); break;
110 default: QVERIFY(false);
113 QVERIFY(verifyConstExpr<(Qt::LeftButton | Qt::RightButton) & Qt::LeftButton>(Qt::LeftButton));
114 QVERIFY(verifyConstExpr<(Qt::LeftButton | Qt::RightButton) & Qt::MiddleButton>(0));
115 QVERIFY(verifyConstExpr<(Qt::LeftButton | Qt::RightButton) | Qt::MiddleButton>(Qt::LeftButton | Qt::RightButton | Qt::MiddleButton));
116 QVERIFY(verifyConstExpr<~(Qt::LeftButton | Qt::RightButton)>(~(Qt::LeftButton | Qt::RightButton)));
117 QVERIFY(verifyConstExpr<Qt::MouseButtons(Qt::LeftButton) ^ Qt::RightButton>(Qt::LeftButton ^ Qt::RightButton));
118 QVERIFY(verifyConstExpr<Qt::MouseButtons(0)>(0));
119 QVERIFY(verifyConstExpr<Qt::MouseButtons(Qt::RightButton) & 0xff>(Qt::RightButton));
120 QVERIFY(verifyConstExpr<Qt::MouseButtons(Qt::RightButton) | 0xff>(0xff));
122 QVERIFY(!verifyConstExpr<Qt::RightButton>(!Qt::MouseButtons(Qt::LeftButton)));
127 QTEST_MAIN(tst_QFlags)
128 #include "tst_qflags.moc"