diff options
Diffstat (limited to 'tests/auto/corelib/global/qflags/tst_qflags.cpp')
-rw-r--r-- | tests/auto/corelib/global/qflags/tst_qflags.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp index 73a76ebcde..e64ef3e128 100644 --- a/tests/auto/corelib/global/qflags/tst_qflags.cpp +++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp @@ -32,6 +32,7 @@ class tst_QFlags: public QObject { Q_OBJECT private slots: + void boolCasts() const; void operators() const; void testFlag() const; void testFlagZeroFlag() const; @@ -46,6 +47,49 @@ private slots: void adl(); }; +void tst_QFlags::boolCasts() const +{ + // This tests that the operator overloading is sufficient so that common + // idioms involving flags -> bool casts work as expected: + + const Qt::Alignment nonNull = Qt::AlignCenter; + const Qt::Alignment null = {}; + + // basic premiss: + QVERIFY(bool(nonNull)); + QVERIFY(!bool(null)); + + // The rest is just checking that stuff compiles: + + // QVERIFY should compile: + QVERIFY(nonNull); + QVERIFY(!null); + + // ifs should compile: + if (null) QFAIL("Can't contextually convert QFlags to bool!"); + if (!nonNull) QFAIL("Missing operator! on QFlags (shouldn't be necessary)."); + + // ternary should compile: + QVERIFY(nonNull ? true : false); + QVERIFY(!null ? true : false); + + // logical operators should compile: + QVERIFY(nonNull && true); + QVERIFY(nonNull || false); + QVERIFY(!null && true); + QVERIFY(!null || false); + + // ... in both directions: + QVERIFY(true && nonNull); + QVERIFY(false || nonNull); + QVERIFY(true && !null); + QVERIFY(false || !null); + + // ... and mixed: + QVERIFY(null || nonNull); + QVERIFY(!(null && nonNull)); +} + void tst_QFlags::operators() const { #define CHECK(op, LHS, RHS, RES) \ |