mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Duration
- Implemented operator+=, operator-=. - Added unit tests.
This commit is contained in:
parent
b70f4e8528
commit
8ff3a1675d
3 changed files with 89 additions and 5 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue