From 0e12be93ccbbe967cb13c7efb88cab56105213b3 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Wed, 31 Jul 2019 23:57:35 +0200 Subject: [PATCH] Unit test for the move assignment operator and move constructor in FPoint, FSize and FRect --- ChangeLog | 4 ++++ src/fpoint.cpp | 5 +++-- src/frect.cpp | 18 ++++++++++-------- src/fsize.cpp | 5 +++-- src/include/final/fpoint.h | 7 +++++++ src/include/final/frect.h | 12 ++++++++++++ src/include/final/fsize.h | 9 ++++++++- test/fpoint-test.cpp | 27 +++++++++++++++++++++++--- test/frect-test.cpp | 39 ++++++++++++++++++++++++++++++++++++++ test/fsize-test.cpp | 29 +++++++++++++++++++++++++--- test/ftermfreebsd-test.cpp | 13 +++---------- 11 files changed, 139 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index a409039b..3e1c40e4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2019-07-31 Markus Gans + * Unit test for the move assignment operator and move constructor + in FPoint, FSize and FRect + 2019-07-28 Markus Gans * FreeBSD can now change the frequency and duration of the pc speaker signal diff --git a/src/fpoint.cpp b/src/fpoint.cpp index 460ca50d..a861fcc5 100644 --- a/src/fpoint.cpp +++ b/src/fpoint.cpp @@ -44,8 +44,9 @@ FPoint& FPoint::operator = (const FPoint& p) //---------------------------------------------------------------------- FPoint& FPoint::operator = (FPoint&& p) { - xpos = std::move(p.xpos); - ypos = std::move(p.ypos); + xpos = p.xpos; + ypos = p.ypos; + p.xpos = p.ypos = 0; return *this; } diff --git a/src/frect.cpp b/src/frect.cpp index 7b70d069..cef8954f 100644 --- a/src/frect.cpp +++ b/src/frect.cpp @@ -299,20 +299,22 @@ FRect FRect::combined (const FRect& r) const //---------------------------------------------------------------------- FRect& FRect::operator = (const FRect& r) { - X1 = r.getX1(); - Y1 = r.getY1(); - X2 = r.getX2(); - Y2 = r.getY2(); + X1 = r.X1; + Y1 = r.Y1; + X2 = r.X2; + Y2 = r.Y2; return *this; } //---------------------------------------------------------------------- FRect& FRect::operator = (FRect&& r) { - X1 = std::move(r.getX1()); - Y1 = std::move(r.getY1()); - X2 = std::move(r.getX2()); - Y2 = std::move(r.getY2()); + X1 = r.X1; + Y1 = r.Y1; + X2 = r.X2; + Y2 = r.Y2; + r.X1 = r.Y1 = 0; + r.X2 = r.Y2 = -1; return *this; } diff --git a/src/fsize.cpp b/src/fsize.cpp index 76772427..5bac040a 100644 --- a/src/fsize.cpp +++ b/src/fsize.cpp @@ -44,8 +44,9 @@ FSize& FSize::operator = (const FSize& s) //---------------------------------------------------------------------- FSize& FSize::operator = (FSize&& s) { - width = std::move(s.width); - height = std::move(s.height); + width = s.width; + height = s.height; + s.width = s.height = 0; std::cerr << " move operator\n"; return *this; } diff --git a/src/include/final/fpoint.h b/src/include/final/fpoint.h index d2d7f27c..058c993d 100644 --- a/src/include/final/fpoint.h +++ b/src/include/final/fpoint.h @@ -53,6 +53,7 @@ class FPoint // Constructors FPoint () = default; FPoint (const FPoint&); // copy constructor + FPoint (FPoint&&); // move constructor FPoint (int, int); // Destructor @@ -103,6 +104,12 @@ inline FPoint::FPoint (const FPoint& p) // copy constructor , ypos(p.ypos) { } +//---------------------------------------------------------------------- +inline FPoint::FPoint (FPoint&& p) // move constructor + : xpos(p.xpos) + , ypos(p.ypos) +{ p.xpos = p.ypos = 0; } + //---------------------------------------------------------------------- inline FPoint::FPoint (int x, int y) : xpos(x) diff --git a/src/include/final/frect.h b/src/include/final/frect.h index e014d03d..179a416a 100644 --- a/src/include/final/frect.h +++ b/src/include/final/frect.h @@ -61,6 +61,7 @@ class FRect // Constructors FRect () = default; FRect (const FRect&); // copy constructor + FRect (FRect&&); // move constructor FRect (int, int, std::size_t, std::size_t); FRect (const FPoint&, const FSize&); FRect (const FPoint&, const FPoint&); @@ -153,6 +154,17 @@ inline FRect::FRect (const FRect& r) // copy constructor , Y2(r.Y2) { } +//---------------------------------------------------------------------- +inline FRect::FRect (FRect&& r) // move constructor + : X1(r.X1) + , Y1(r.Y1) + , X2(r.X2) + , Y2(r.Y2) +{ + r.X1 = r.Y1 = 0; + r.X2 = r.Y2 = -1; +} + //---------------------------------------------------------------------- inline FRect::FRect (int x, int y, std::size_t width, std::size_t height) : X1(x) diff --git a/src/include/final/fsize.h b/src/include/final/fsize.h index 6c5e2a47..4b06d0f3 100644 --- a/src/include/final/fsize.h +++ b/src/include/final/fsize.h @@ -55,6 +55,7 @@ class FSize // Constructors FSize () = default; FSize (const FSize&); // copy constructor + FSize (FSize&&); // move constructor FSize (std::size_t, std::size_t); // Destructor @@ -108,7 +109,13 @@ class FSize inline FSize::FSize (const FSize& s) // copy constructor : width(s.width) , height(s.height) -{ } +{ std::cerr << " copy ctor\n";} + +//---------------------------------------------------------------------- +inline FSize::FSize (FSize&& s) // move constructor + : width(s.width) + , height(s.height) +{ s.width = s.height = 0; std::cerr << " move ctor\n";} //---------------------------------------------------------------------- inline FSize::FSize (std::size_t w, std::size_t h) diff --git a/test/fpoint-test.cpp b/test/fpoint-test.cpp index 27c1f465..d315bf63 100644 --- a/test/fpoint-test.cpp +++ b/test/fpoint-test.cpp @@ -49,6 +49,7 @@ class FPointTest : public CPPUNIT_NS::TestFixture void classNameTest(); void noArgumentTest(); void copyConstructorTest(); + void moveConstructorTest(); void assignmentTest(); void additionAssignmentTest(); void subtractionAssignmentTest(); @@ -68,6 +69,7 @@ class FPointTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST (classNameTest); CPPUNIT_TEST (noArgumentTest); CPPUNIT_TEST (copyConstructorTest); + CPPUNIT_TEST (moveConstructorTest); CPPUNIT_TEST (assignmentTest); CPPUNIT_TEST (additionAssignmentTest); CPPUNIT_TEST (subtractionAssignmentTest); @@ -110,6 +112,17 @@ void FPointTest::copyConstructorTest() CPPUNIT_ASSERT ( p2.getY() == 10 ); } +//---------------------------------------------------------------------- +void FPointTest::moveConstructorTest() +{ + finalcut::FPoint p1 (25,16); + const finalcut::FPoint p2 (std::move(p1)); + CPPUNIT_ASSERT ( p1.getX() == 0 ); + CPPUNIT_ASSERT ( p1.getY() == 0 ); + CPPUNIT_ASSERT ( p2.getX() == 25 ); + CPPUNIT_ASSERT ( p2.getY() == 16 ); +} + //---------------------------------------------------------------------- void FPointTest::assignmentTest() { @@ -138,11 +151,19 @@ void FPointTest::assignmentTest() CPPUNIT_ASSERT ( p2.getX() == 40 ); CPPUNIT_ASSERT ( p2.getY() == 12 ); + // Move assignment operator + finalcut::FPoint p3 = std::move(p2); + CPPUNIT_ASSERT ( p2.getX() == 0 ); + CPPUNIT_ASSERT ( p2.getY() == 0 ); + CPPUNIT_ASSERT ( p2.isOrigin() ); + CPPUNIT_ASSERT ( p3.getX() == 40 ); + CPPUNIT_ASSERT ( p3.getY() == 12 ); + // Value limit - const finalcut::FPoint p3 ( std::numeric_limits::min() + const finalcut::FPoint p4 ( std::numeric_limits::min() , std::numeric_limits::max() ); - CPPUNIT_ASSERT ( p3.getX() == std::numeric_limits::min() ); - CPPUNIT_ASSERT ( p3.getY() == std::numeric_limits::max() ); + CPPUNIT_ASSERT ( p4.getX() == std::numeric_limits::min() ); + CPPUNIT_ASSERT ( p4.getY() == std::numeric_limits::max() ); } //---------------------------------------------------------------------- diff --git a/test/frect-test.cpp b/test/frect-test.cpp index ef14fa72..1fa309e4 100644 --- a/test/frect-test.cpp +++ b/test/frect-test.cpp @@ -47,6 +47,7 @@ class FRectTest : public CPPUNIT_NS::TestFixture void classNameTest(); void noArgumentTest(); void copyConstructorTest(); + void moveConstructorTest(); void assignmentTest(); void equalTest(); void notEqualTest(); @@ -69,6 +70,7 @@ class FRectTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST (classNameTest); CPPUNIT_TEST (noArgumentTest); CPPUNIT_TEST (copyConstructorTest); + CPPUNIT_TEST (moveConstructorTest); CPPUNIT_TEST (assignmentTest); CPPUNIT_TEST (equalTest); CPPUNIT_TEST (notEqualTest); @@ -107,6 +109,7 @@ void FRectTest::noArgumentTest() CPPUNIT_ASSERT ( rectangle.isEmpty() ); CPPUNIT_ASSERT ( rectangle.getWidth() == 0 ); CPPUNIT_ASSERT ( rectangle.getHeight() == 0 ); + CPPUNIT_ASSERT ( rectangle.getSize() == finalcut::FSize(0, 0) ); CPPUNIT_ASSERT ( rectangle.getPos() == finalcut::FPoint(0, 0) ); CPPUNIT_ASSERT ( rectangle.getUpperLeftPos() == finalcut::FPoint(0, 0) ); CPPUNIT_ASSERT ( rectangle.getUpperRightPos() == finalcut::FPoint(-1, 0) ); @@ -126,6 +129,23 @@ void FRectTest::copyConstructorTest() CPPUNIT_ASSERT ( r2.getHeight() == 10 ); } +//---------------------------------------------------------------------- +void FRectTest::moveConstructorTest() +{ + finalcut::FRect r1(3, 3, 15, 7); + const finalcut::FRect r2 (std::move(r1)); + CPPUNIT_ASSERT ( r1.getX() == 0 ); + CPPUNIT_ASSERT ( r1.getY() == 0 ); + CPPUNIT_ASSERT ( r1.isEmpty() ); + CPPUNIT_ASSERT ( r1.getWidth() == 0 ); + CPPUNIT_ASSERT ( r1.getHeight() == 0 ); + CPPUNIT_ASSERT ( r2.getX() == 3 ); + CPPUNIT_ASSERT ( r2.getY() == 3 ); + CPPUNIT_ASSERT ( ! r2.isEmpty() ); + CPPUNIT_ASSERT ( r2.getWidth() == 15 ); + CPPUNIT_ASSERT ( r2.getHeight() == 7 ); +} + //---------------------------------------------------------------------- void FRectTest::assignmentTest() { @@ -336,6 +356,25 @@ void FRectTest::assignmentTest() CPPUNIT_ASSERT ( r5.getWidth() == 10 ); CPPUNIT_ASSERT ( r5.getHeight() == 10 ); CPPUNIT_ASSERT ( r5.getSize() == finalcut::FSize(10, 10) ); + + finalcut::FRect r6; + r6 = std::move(r5); // Move assignment operator + CPPUNIT_ASSERT ( r5.getX1() == 0 ); + CPPUNIT_ASSERT ( r5.getY1() == 0 ); + CPPUNIT_ASSERT ( r5.getX2() == -1 ); + CPPUNIT_ASSERT ( r5.getY2() == -1 ); + CPPUNIT_ASSERT ( r5.isEmpty() ); + CPPUNIT_ASSERT ( r5.getWidth() == 0 ); + CPPUNIT_ASSERT ( r5.getHeight() == 0 ); + CPPUNIT_ASSERT ( r5.getSize() == finalcut::FSize(0, 0) ); + CPPUNIT_ASSERT ( r5.getPos() == finalcut::FPoint(0, 0) ); + CPPUNIT_ASSERT ( r6.getX1() == 2 ); + CPPUNIT_ASSERT ( r6.getY1() == 9 ); + CPPUNIT_ASSERT ( r6.getX2() == 11 ); + CPPUNIT_ASSERT ( r6.getY2() == 18 ); + CPPUNIT_ASSERT ( r6.getWidth() == 10 ); + CPPUNIT_ASSERT ( r6.getHeight() == 10 ); + CPPUNIT_ASSERT ( r6.getSize() == finalcut::FSize(10, 10) ); } //---------------------------------------------------------------------- diff --git a/test/fsize-test.cpp b/test/fsize-test.cpp index 3da05bea..1c4c949c 100644 --- a/test/fsize-test.cpp +++ b/test/fsize-test.cpp @@ -49,6 +49,7 @@ class FSizeTest : public CPPUNIT_NS::TestFixture void classNameTest(); void noArgumentTest(); void copyConstructorTest(); + void moveConstructorTest(); void assignmentTest(); void additionAssignmentTest(); void subtractionAssignmentTest(); @@ -71,6 +72,7 @@ class FSizeTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST (classNameTest); CPPUNIT_TEST (noArgumentTest); CPPUNIT_TEST (copyConstructorTest); + CPPUNIT_TEST (moveConstructorTest); CPPUNIT_TEST (assignmentTest); CPPUNIT_TEST (additionAssignmentTest); CPPUNIT_TEST (subtractionAssignmentTest); @@ -116,6 +118,18 @@ void FSizeTest::copyConstructorTest() CPPUNIT_ASSERT ( s2.getHeight() == 80 ); } +//---------------------------------------------------------------------- +void FSizeTest::moveConstructorTest() +{ + finalcut::FSize s1 (120, 36); + const finalcut::FSize s2 (std::move(s1)); + CPPUNIT_ASSERT ( s1.getWidth() == 0 ); + CPPUNIT_ASSERT ( s1.getHeight() == 0 ); + CPPUNIT_ASSERT ( s1.isEmpty() ); + CPPUNIT_ASSERT ( s2.getWidth() == 120 ); + CPPUNIT_ASSERT ( s2.getHeight() == 36 ); +} + //---------------------------------------------------------------------- void FSizeTest::assignmentTest() { @@ -145,11 +159,20 @@ void FSizeTest::assignmentTest() CPPUNIT_ASSERT ( s2.getWidth() == 5 ); CPPUNIT_ASSERT ( s2.getHeight() == 4 ); + // Move assignment operator + finalcut::FSize s3; + s3 = std::move(s2); + CPPUNIT_ASSERT ( s2.getWidth() == 0 ); + CPPUNIT_ASSERT ( s2.getHeight() == 0 ); + CPPUNIT_ASSERT ( s2.isEmpty() ); + CPPUNIT_ASSERT ( s3.getWidth() == 5 ); + CPPUNIT_ASSERT ( s3.getHeight() == 4 ); + // Value limit - const finalcut::FSize s3 ( std::numeric_limits::min() + const finalcut::FSize s4 ( std::numeric_limits::min() , std::numeric_limits::max() ); - CPPUNIT_ASSERT ( s3.getWidth() == std::numeric_limits::min() ); - CPPUNIT_ASSERT ( s3.getHeight() == std::numeric_limits::max() ); + CPPUNIT_ASSERT ( s4.getWidth() == std::numeric_limits::min() ); + CPPUNIT_ASSERT ( s4.getHeight() == std::numeric_limits::max() ); } //---------------------------------------------------------------------- diff --git a/test/ftermfreebsd-test.cpp b/test/ftermfreebsd-test.cpp index 6a30c3d2..11b89567 100644 --- a/test/ftermfreebsd-test.cpp +++ b/test/ftermfreebsd-test.cpp @@ -684,15 +684,6 @@ void ftermfreebsdTest::freebsdConsoleTest() freebsd.restoreCursorStyle(); CPPUNIT_ASSERT ( freebsd.getCursorStyle() == finalcut::fc::blink_cursor ); - - for (std::size_t i = 0; i <= finalcut::fc::lastCharItem; i++) - if ( finalcut::fc::character[i][finalcut::fc::PC] < 0x1c ) - std::cerr << i << ":" << finalcut::fc::character[i][finalcut::fc::PC] << ", "; - - for (std::size_t i = 0; i <= finalcut::fc::lastCharItem; i++) - if ( finalcut::fc::character[i][finalcut::fc::PC] < 0x1c ) - std::cerr << i << ":" << finalcut::fc::character[i][finalcut::fc::ASCII] << ", "; - CPPUNIT_ASSERT ( finalcut::fc::character[2][finalcut::fc::PC] == 21 ); CPPUNIT_ASSERT ( finalcut::fc::character[3][finalcut::fc::PC] == 8 ); CPPUNIT_ASSERT ( finalcut::fc::character[4][finalcut::fc::PC] == 10 ); @@ -711,8 +702,8 @@ void ftermfreebsdTest::freebsdConsoleTest() CPPUNIT_ASSERT ( finalcut::fc::character[59][finalcut::fc::PC] == 16 ); CPPUNIT_ASSERT ( finalcut::fc::character[60][finalcut::fc::PC] == 17 ); CPPUNIT_ASSERT ( finalcut::fc::character[105][finalcut::fc::PC] == 4 ); + freebsd.initCharMap (finalcut::fc::character); - term_detection->detect(); CPPUNIT_ASSERT ( finalcut::fc::character[2][finalcut::fc::PC] == 36 ); CPPUNIT_ASSERT ( finalcut::fc::character[3][finalcut::fc::PC] == 42 ); @@ -733,6 +724,8 @@ void ftermfreebsdTest::freebsdConsoleTest() CPPUNIT_ASSERT ( finalcut::fc::character[60][finalcut::fc::PC] == 60 ); CPPUNIT_ASSERT ( finalcut::fc::character[105][finalcut::fc::PC] == 42 ); + term_detection->detect(); + #if DEBUG const finalcut::FString& sec_da = \ finalcut::FTerm::getFTermDebugData().getSecDAString();