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>
bb10
Marc Mutz 2020-06-08 09:34:48 +02:00
parent fd52a6fcb9
commit 82743fb8a2
1 changed files with 51 additions and 0 deletions

View File

@ -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);