From 0330b967f20bc265b9799418ce8e0b6faf929a62 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 29 Oct 2018 12:21:38 +0100 Subject: Simplify the Q_FOREACH macro when using C++17 This way there is only one for loop, which is more optimizer friendly Change-Id: Iaa02026627d5259c3eea1ff5664e8f22664eef73 Reviewed-by: Thiago Macieira --- tests/auto/corelib/global/qglobal/qglobal.pro | 1 + tests/auto/corelib/global/qglobal/tst_qglobal.cpp | 40 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/corelib/global/qglobal/qglobal.pro b/tests/auto/corelib/global/qglobal/qglobal.pro index a40cb9a288..b105769430 100644 --- a/tests/auto/corelib/global/qglobal/qglobal.pro +++ b/tests/auto/corelib/global/qglobal/qglobal.pro @@ -2,3 +2,4 @@ CONFIG += testcase TARGET = tst_qglobal QT = core testlib SOURCES = tst_qglobal.cpp qglobal.c +contains(QT_CONFIG, c++1z): CONFIG += c++1z diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 78b954f373..56da047147 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -126,6 +126,46 @@ void tst_QGlobal::for_each() QCOMPARE(i, counter++); } QCOMPARE(counter, list.count()); + + // Should also work with an existing variable + int local; + counter = 0; + foreach (local, list) { + QCOMPARE(local, counter++); + } + QCOMPARE(counter, list.count()); + QCOMPARE(local, counter - 1); + + // Test the macro does not mess if/else conditions + counter = 0; + if (true) + foreach (int i, list) + QCOMPARE(i, counter++); + else + QFAIL("If/Else mismatch"); + QCOMPARE(counter, list.count()); + + counter = 0; + if (false) + foreach (int i, list) + if (i) QFAIL("If/Else mismatch"); + else QFAIL("If/Else mismatch"); + else + foreach (int i, list) + if (false) { } + else QCOMPARE(i, counter++); + QCOMPARE(counter, list.count()); + + // break and continue + counter = 0; + foreach (int i, list) { + if (i == 0) + continue; + QCOMPARE(i, (counter++) + 1); + if (i == 3) + break; + } + QCOMPARE(counter, 3); } void tst_QGlobal::qassert() -- cgit v1.2.3