From 95cab90b1023a824017b6d22f62987ba9742b6b3 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Wed, 8 May 2013 18:02:02 +0200 Subject: Make QFlags enum flags (C++11 strict enums) friendly Change-Id: I9ccb3e4d281a545ca1845db4f6aa7ac6c04e8621 Reviewed-by: Olivier Goffart --- tests/auto/corelib/global/qflags/tst_qflags.cpp | 116 ++++++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'tests/auto/corelib/global/qflags') diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp index b9b817d688..e50a6b63fe 100644 --- a/tests/auto/corelib/global/qflags/tst_qflags.cpp +++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp @@ -49,6 +49,7 @@ private slots: void testFlagMultiBits() const; void constExpr(); void signedness(); + void classEnum(); }; void tst_QFlags::testFlag() const @@ -137,6 +138,121 @@ void tst_QFlags::signedness() QtPrivate::is_signed::value)); } +#if defined(Q_COMPILER_CLASS_ENUM) +enum class MyStrictEnum { StrictZero, StrictOne, StrictTwo, StrictFour=4 }; +Q_DECLARE_FLAGS( MyStrictFlags, MyStrictEnum ) +Q_DECLARE_OPERATORS_FOR_FLAGS( MyStrictFlags ) + +Q_STATIC_ASSERT( !QTypeInfo::isComplex ); +Q_STATIC_ASSERT( !QTypeInfo::isStatic ); +Q_STATIC_ASSERT( !QTypeInfo::isLarge ); +Q_STATIC_ASSERT( !QTypeInfo::isPointer ); +#endif + +void tst_QFlags::classEnum() +{ +#if defined(Q_COMPILER_CLASS_ENUM) + // The main aim of the test is making sure it compiles + // The QCOMPARE are there as an extra + MyStrictEnum e1 = MyStrictEnum::StrictOne; + MyStrictEnum e2 = MyStrictEnum::StrictTwo; + + MyStrictFlags f1(MyStrictEnum::StrictOne); + QCOMPARE(f1, 1); + + MyStrictFlags f2(e2); + QCOMPARE(f2, 2); + + MyStrictFlags f0; + QCOMPARE(f0, 0); + + MyStrictFlags f3(e2 | e1); + QCOMPARE(f3, 3); + + QVERIFY(f3.testFlag(MyStrictEnum::StrictOne)); + QVERIFY(!f1.testFlag(MyStrictEnum::StrictTwo)); + + QVERIFY(!f0); + + QCOMPARE(f3 & int(1), 1); + QCOMPARE(f3 & uint(1), 1); + QCOMPARE(f3 & MyStrictEnum::StrictOne, 1); + + MyStrictFlags aux; + aux = f3; + aux &= int(1); + QCOMPARE(aux, 1); + + aux = f3; + aux &= uint(1); + QCOMPARE(aux, 1); + + aux = f3; + aux &= MyStrictEnum::StrictOne; + QCOMPARE(aux, 1); + + aux = f3; + aux &= f1; + QCOMPARE(aux, 1); + + aux = f3 ^ f3; + QCOMPARE(aux, 0); + + aux = f3 ^ f1; + QCOMPARE(aux, 2); + + aux = f3 ^ f0; + QCOMPARE(aux, 3); + + aux = f3 ^ MyStrictEnum::StrictOne; + QCOMPARE(aux, 2); + + aux = f3 ^ MyStrictEnum::StrictZero; + QCOMPARE(aux, 3); + + aux = f3; + aux ^= f3; + QCOMPARE(aux, 0); + + aux = f3; + aux ^= f1; + QCOMPARE(aux, 2); + + aux = f3; + aux ^= f0; + QCOMPARE(aux, 3); + + aux = f3; + aux ^= MyStrictEnum::StrictOne; + QCOMPARE(aux, 2); + + aux = f3; + aux ^= MyStrictEnum::StrictZero; + QCOMPARE(aux, 3); + + aux = f1 | f2; + QCOMPARE(aux, 3); + + aux = MyStrictEnum::StrictOne | MyStrictEnum::StrictTwo; + QCOMPARE(aux, 3); + + aux = f1; + aux |= f2; + QCOMPARE(aux, 3); + + aux = MyStrictEnum::StrictOne; + aux |= MyStrictEnum::StrictTwo; + QCOMPARE(aux, 3); + + aux = ~f1; + QCOMPARE(aux, -2); + + // Just to make sure it compiles + if (false) + qDebug() << f3; +#endif +} + // (statically) check QTypeInfo for QFlags instantiations: enum MyEnum { Zero, One, Two, Four=4 }; Q_DECLARE_FLAGS( MyFlags, MyEnum ) -- cgit v1.2.3