From e002a55355207012da672e1abb3434009eba82c0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 10 Nov 2015 12:41:37 +0100 Subject: [PATCH] tst_compiler: check more cases for RANGE_FOR Check that it works on C arrays, with auto type deduction and with types that only provide free begin()/end() functions that can only be found through ADL. Change-Id: I760722a0f56c9ebe967070ff68af90b96ed77e66 Reviewed-by: Thiago Macieira Reviewed-by: Olivier Goffart (Woboq GmbH) --- tests/auto/other/compiler/tst_compiler.cpp | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/tests/auto/other/compiler/tst_compiler.cpp b/tests/auto/other/compiler/tst_compiler.cpp index e1db8d8aaf..981206cc9e 100644 --- a/tests/auto/other/compiler/tst_compiler.cpp +++ b/tests/auto/other/compiler/tst_compiler.cpp @@ -979,6 +979,20 @@ void tst_Compiler::cxx11_nullptr() #endif } +namespace SomeNamespace { +class AdlOnly { + QVector v; +public: + AdlOnly() : v(5) { std::fill_n(v.begin(), v.size(), 42); } + +private: + friend QVector::const_iterator begin(const AdlOnly &x) { return x.v.begin(); } + friend QVector::const_iterator end(const AdlOnly &x) { return x.v.end(); } + friend QVector::iterator begin(AdlOnly &x) { return x.v.begin(); } + friend QVector::iterator end(AdlOnly &x) { return x.v.end(); } +}; +} + void tst_Compiler::cxx11_range_for() { #ifndef Q_COMPILER_RANGE_FOR @@ -998,6 +1012,85 @@ void tst_Compiler::cxx11_range_for() l << 2; for (int i : ll) QCOMPARE(i, 2); + + { + const int array[] = { 0, 1, 2, 3, 4 }; + int i = 0; + for (const int &e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (int e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (const int e : array) + QCOMPARE(e, array[i++]); +#ifdef Q_COMPILER_AUTO_TYPE + i = 0; + for (const auto &e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (auto &e : array) // auto deducing const + QCOMPARE(e, array[i++]); + i = 0; + for (auto e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (const auto e : array) + QCOMPARE(e, array[i++]); +#endif + } + + { + int array[] = { 0, 1, 2, 3, 4 }; + const int array2[] = { 10, 11, 12, 13, 14 }; + int i = 0; + for (const int &e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (int &e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (int e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (const int e : array) + QCOMPARE(e, array[i++]); +#ifdef Q_COMPILER_AUTO_TYPE + i = 0; + for (const auto &e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (auto &e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (auto e : array) + QCOMPARE(e, array[i++]); + i = 0; + for (const auto e : array) + QCOMPARE(e, array[i++]); +#endif + for (int &e : array) + e += 10; + i = 0; + for (const int &e : array) + QCOMPARE(e, array2[i++]); + } + + { + const SomeNamespace::AdlOnly x; + for (const int &e : x) + QCOMPARE(e, 42); + } + + { + SomeNamespace::AdlOnly x; + for (const int &e : x) + QCOMPARE(e, 42); + for (int &e : x) + e += 10; + for (const int &e : x) + QCOMPARE(e, 52); + } #endif }