diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2014-02-25 23:07:46 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-02-28 21:27:41 +0100 |
commit | db8167c224348fe828aaa0746701b7fa3040c2df (patch) | |
tree | c733df7fc7c7509db817e60e27f4aea7757231cb /tests/auto/gui/painting | |
parent | 98d7d4c1e51010a0ca48ff59f30342e81fd0d066 (diff) |
QPen: add move constructor
After this change, the relation between copy/move constructor calls in QtGui is
something like 40/16.
A moved-from QPen can only be copied, assigned-to or else destroyed. This required
adding a nullptr check to the copy ctor and the dtor and rewriting copy assignment
(which used non-nullptr-safe qAtomicAssign) in terms of copy construction and
swapping.
Extensive tests included. They are implemented such that they work in C++98 as well
as C++11 mode, but they naturally test move semantics only in a C++11 build.
Change-Id: If68f37c10b8eeefb2478cbae386cd2e38b4f6e19
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/gui/painting')
-rw-r--r-- | tests/auto/gui/painting/qpen/tst_qpen.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/auto/gui/painting/qpen/tst_qpen.cpp b/tests/auto/gui/painting/qpen/tst_qpen.cpp index 07c996d026..a373c51710 100644 --- a/tests/auto/gui/painting/qpen/tst_qpen.cpp +++ b/tests/auto/gui/painting/qpen/tst_qpen.cpp @@ -57,6 +57,8 @@ public: private slots: void getSetCheck(); void swap(); + void move(); + void move_assign(); void operator_eq_eq(); void operator_eq_eq_data(); @@ -101,6 +103,57 @@ void tst_QPen::swap() QCOMPARE(p2.color(), QColor(Qt::black)); } +void tst_QPen::move() +{ + QPen p1(Qt::black); + + // check that moving does the right thing: + QPen p2 = qMove(p1); // could be move or copy construction, so don't check p1's state + QCOMPARE(p2.color(), QColor(Qt::black)); + + // this, executed ehre, would crash: + // QVERIFY(p1.style() != Qt::NoPen); + + // check that moved-from QPen p1 can still be safely copied: + const QPen p3 = p1; + + // check that moved-from QPen p1 can still be safely assigned to: + const QPen p4(Qt::yellow); + p1 = p4; + QCOMPARE(p1.color(), QColor(Qt::yellow)); + + // check that moved-from QPens p2, p3 can still be safely destroyed: + QPen p5 = qMove(p2); + + // intentionally no more statements beyond this point +} + +void tst_QPen::move_assign() +{ + QPen p1(Qt::black), p2(Qt::white); + + // check that moving does the right thing: + p2 = qMove(p1); // could be move or copy assignment, so don't check p1's state + QCOMPARE(p2.color(), QColor(Qt::black)); + + // check that move-assigned-from QPen p1 can still be used, albeit + // with undocumented state (it's p2's original state): + QVERIFY(p1.style() != Qt::NoPen); + + // check that moved-from QPen p1 can still be safely copied: + const QPen p3 = p1; + + // check that moved-from QPen p1 can still be safely assigned to: + const QPen p4(Qt::yellow); + p1 = p4; + QCOMPARE(p1.color(), QColor(Qt::yellow)); + + // check that moved-from QPens p2, p3 can still be safely destroyed: + QPen p5; + p5 = qMove(p2); + + // intentionally no more statements beyond this point +} tst_QPen::tst_QPen() |