From 8ff3a1675d9282d4ca3170ba397218cad97999e9 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 4 Jul 2011 14:22:33 -0400 Subject: [PATCH] Duration - Implemented operator+=, operator-=. - Added unit tests. --- src/Duration.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++-- src/Duration.h | 7 ++++-- test/duration.t.cpp | 32 +++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/Duration.cpp b/src/Duration.cpp index 423558f6e..15e91c749 100644 --- a/src/Duration.cpp +++ b/src/Duration.cpp @@ -155,9 +155,60 @@ Duration& Duration::operator= (const Duration& other) } //////////////////////////////////////////////////////////////////////////////// -Duration& Duration::operator- (const Duration& other) +Duration Duration::operator- (const Duration& other) { - throw std::string ("Error: Duration::operator- unimplemented"); + int left = mSecs * ( mNegative ? -1 : 1); + int right = other.mSecs * (other.mNegative ? -1 : 1); + + left -= right; + + Duration result; + result.mSecs = abs (left); + result.mNegative = left < 0; + + return result; +} + +//////////////////////////////////////////////////////////////////////////////// +Duration Duration::operator+ (const Duration& other) +{ + int left = mSecs * ( mNegative ? -1 : 1); + int right = other.mSecs * (other.mNegative ? -1 : 1); + + left += right; + + Duration result; + result.mSecs = abs (left); + result.mNegative = left < 0; + + return result; +} + +//////////////////////////////////////////////////////////////////////////////// +Duration& Duration::operator-= (const Duration& other) +{ + int left = mSecs * ( mNegative ? -1 : 1); + int right = other.mSecs * (other.mNegative ? -1 : 1); + + left -= right; + + mSecs = abs (left); + mNegative = left < 0; + + return *this; +} + +//////////////////////////////////////////////////////////////////////////////// +Duration& Duration::operator+= (const Duration& other) +{ + int left = mSecs * ( mNegative ? -1 : 1); + int right = other.mSecs * (other.mNegative ? -1 : 1); + + left += right; + + mSecs = abs (left); + mNegative = left < 0; + return *this; } diff --git a/src/Duration.h b/src/Duration.h index ceef31a71..065135bac 100644 --- a/src/Duration.h +++ b/src/Duration.h @@ -43,7 +43,10 @@ public: bool operator> (const Duration&); bool operator>= (const Duration&); Duration& operator= (const Duration&); - Duration& operator- (const Duration&); + Duration operator- (const Duration&); + Duration operator+ (const Duration&); + Duration& operator-= (const Duration&); + Duration& operator+= (const Duration&); ~Duration (); // Destructor operator time_t () const; @@ -56,7 +59,7 @@ public: static bool valid (const std::string&); void parse (const std::string&); -private: +protected: time_t mSecs; bool mNegative; }; diff --git a/test/duration.t.cpp b/test/duration.t.cpp index a9892b153..d76ab4f86 100644 --- a/test/duration.t.cpp +++ b/test/duration.t.cpp @@ -48,7 +48,7 @@ int convertDuration (const std::string& input) int main (int argc, char** argv) { - UnitTest t (618); + UnitTest t (628); Duration d; @@ -741,6 +741,36 @@ int main (int argc, char** argv) left = Duration ("1sec"); right = Duration ("2secs"); t.notok (left >= right, "duration NOT 1sec >= 2secs"); left = Duration ("2secs"); right = Duration ("2secs"); t.ok (left >= right, "duration 1sec >= 2secs"); left = Duration ("2secs"); right = Duration ("1secs"); t.ok (left >= right, "duration 1sec >= 2secs"); + + // operator+ + left = Duration (1); + right = Duration (2); + Duration result = left + right; + t.is ((int)(time_t)left, 1, "1 + 2 = 3, 1 is still 1"); + t.is ((int)(time_t)right, 2, "1 + 2 = 3, 2 is still 2"); + t.is ((int)(time_t)result, 3, "1 + 2 = 3"); + + // operator+= + left = Duration (1); + right = Duration (2); + left += right; + t.is ((int)(time_t)left, 3, "1 += 2, 1 is now 3"); + t.is ((int)(time_t)right, 2, "1 += 2, 2 is still 2"); + + // operator- + left = Duration (3); + right = Duration (2); + result = left - right; + t.is ((int)(time_t)left, 3, "3 - 2 = 1, 3 is still 3"); + t.is ((int)(time_t)right, 2, "3 - 2 = 1, 2 is still 2"); + t.is ((int)(time_t)result, 1, "3 - 2 = 1"); + + // operator-= + left = Duration (3); + right = Duration (2); + left -= right; + t.is ((int)(time_t)left, 1, "3 -= 2, 3 is now 1"); + t.is ((int)(time_t)right, 2, "3 -= 2, 2 is still 2"); } catch (const std::string& e) { t.diag (e); }