summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2020-06-08 09:34:48 +0200
committerMarc Mutz <marc.mutz@kdab.com>2020-06-24 17:49:35 +0200
commit82743fb8a237c4b2aad9f3c029de4da73379f352 (patch)
treeaaf74dcdf3a898678a15a23465955691be9e635b
parentfd52a6fcb952ec5790e776fe1b97b47d6c5b6f89 (diff)
QPair: add a check that SB works as expected
We have no doubt it does, because it's compiler-synthesized, but we might want to implement the tuple protocol for QPair in the future and then this will act as a safety net, emulating what users are currently already doing with QPair. Change-Id: Ie37f0214bb1aa64210d25be8a256606f4572febe Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--tests/auto/corelib/tools/qpair/tst_qpair.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qpair/tst_qpair.cpp b/tests/auto/corelib/tools/qpair/tst_qpair.cpp
index 8eeddf5320..0708c15530 100644
--- a/tests/auto/corelib/tools/qpair/tst_qpair.cpp
+++ b/tests/auto/corelib/tools/qpair/tst_qpair.cpp
@@ -36,6 +36,7 @@ class tst_QPair : public QObject
Q_OBJECT
private Q_SLOTS:
void pairOfReferences();
+ void structuredBindings();
void testConstexpr();
void testConversions();
void taskQTBUG_48780_pairContainingCArray();
@@ -121,6 +122,56 @@ void tst_QPair::pairOfReferences()
QCOMPARE(p.second, QLatin1String("World"));
}
+void tst_QPair::structuredBindings()
+{
+ using PV = QPair<int, QString>;
+ using PR = QPair<int&, const QString&>;
+
+ {
+ PV pv = {42, "Hello"};
+ PR pr = {pv.first, pv.second};
+
+ auto [fv, sv] = pv;
+
+ fv = 24;
+ sv = "World";
+ QCOMPARE(fv, 24);
+ QCOMPARE(sv, "World");
+ QCOMPARE(pv.first, 42);
+ QCOMPARE(pv.second, "Hello");
+
+ auto [fr, sr] = pr;
+
+ fr = 2424;
+ // sr = "World"; // const
+ QCOMPARE(fr, 2424);
+ QCOMPARE(pv.first, 2424);
+ }
+
+ {
+ PV pv = {42, "Hello"};
+ PR pr = {pv.first, pv.second};
+
+ auto& [fv, sv] = pv;
+
+ fv = 24;
+ sv = "World";
+ QCOMPARE(fv, 24);
+ QCOMPARE(sv, "World");
+ QCOMPARE(pv.first, 24);
+ QCOMPARE(pv.second, "World");
+
+ auto& [fr, sr] = pr;
+
+ fr = 4242;
+ //sr = "2World"; // const
+
+ QCOMPARE(fr, 4242);
+ QCOMPARE(pr.first, 4242);
+ QCOMPARE(pv.first, 4242);
+ }
+}
+
void tst_QPair::testConstexpr()
{
Q_CONSTEXPR QPair<int, double> pID = qMakePair(0, 0.0);