diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-11-10 12:41:37 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-11-20 20:18:07 +0000 |
commit | e002a55355207012da672e1abb3434009eba82c0 (patch) | |
tree | c21bbac46a9770f5c37f149900b048624916f897 /tests/auto/other | |
parent | 10bea194f873eedda312dd958fb8696e5ad2bb1b (diff) |
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 <thiago.macieira@intel.com>
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto/other')
-rw-r--r-- | tests/auto/other/compiler/tst_compiler.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
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<int> v; +public: + AdlOnly() : v(5) { std::fill_n(v.begin(), v.size(), 42); } + +private: + friend QVector<int>::const_iterator begin(const AdlOnly &x) { return x.v.begin(); } + friend QVector<int>::const_iterator end(const AdlOnly &x) { return x.v.end(); } + friend QVector<int>::iterator begin(AdlOnly &x) { return x.v.begin(); } + friend QVector<int>::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 } |