Moving relevant tests to corelib/global
[profile/ivi/qtbase.git] / tests / auto / corelib / global / qflags / tst_qflags.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42
43 class tst_QFlags: public QObject
44 {
45     Q_OBJECT
46 private slots:
47     void testFlag() const;
48     void testFlagZeroFlag() const;
49     void testFlagMultiBits() const;
50     void constExpr();
51 };
52
53 void tst_QFlags::testFlag() const
54 {
55     Qt::MouseButtons btn = Qt::LeftButton | Qt::RightButton;
56
57     QVERIFY(btn.testFlag(Qt::LeftButton));
58     QVERIFY(!btn.testFlag(Qt::MidButton));
59
60     btn = 0;
61     QVERIFY(!btn.testFlag(Qt::LeftButton));
62 }
63
64 void tst_QFlags::testFlagZeroFlag() const
65 {
66     {
67         Qt::MouseButtons btn = Qt::LeftButton | Qt::RightButton;
68         /* Qt::NoButton has the value 0. */
69
70         QVERIFY(!btn.testFlag(Qt::NoButton));
71     }
72
73     {
74         /* A zero enum set should test true with zero. */
75         QVERIFY(Qt::MouseButtons().testFlag(Qt::NoButton));
76     }
77
78     {
79         Qt::MouseButtons btn = Qt::NoButton;
80         QVERIFY(btn.testFlag(Qt::NoButton));
81     }
82 }
83
84 void tst_QFlags::testFlagMultiBits() const
85 {
86     /* Qt::Window is 0x00000001
87      * Qt::Dialog is 0x00000002 | Window
88      */
89     {
90         const Qt::WindowFlags onlyWindow(Qt::Window);
91         QVERIFY(!onlyWindow.testFlag(Qt::Dialog));
92     }
93
94     {
95         const Qt::WindowFlags hasDialog(Qt::Dialog);
96         QVERIFY(hasDialog.testFlag(Qt::Dialog));
97     }
98 }
99
100 template <int N, typename T> bool verifyConstExpr(T n) { return n == N; }
101
102 void tst_QFlags::constExpr()
103 {
104 #ifdef Q_COMPILER_CONSTEXPR
105     Qt::MouseButtons btn = Qt::LeftButton | Qt::RightButton;
106     switch (btn) {
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);
111     }
112
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));
121
122     QVERIFY(!verifyConstExpr<Qt::RightButton>(!Qt::MouseButtons(Qt::LeftButton)));
123 #endif
124 }
125
126
127 QTEST_MAIN(tst_QFlags)
128 #include "tst_qflags.moc"