mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-30 22:43:24 +02:00
Unit Tests - util.t
- Added unit tests for formatSeconds and formatSecondsCompact. - Fixed small boundary but in formatSeconds.
This commit is contained in:
parent
42c1b30c31
commit
8d43a35ca4
2 changed files with 63 additions and 15 deletions
|
@ -34,14 +34,62 @@ Context context;
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main (int argc, char** argv)
|
||||
{
|
||||
UnitTest t (21);
|
||||
UnitTest t (67);
|
||||
|
||||
// TODO bool confirm (const std::string&);
|
||||
// TODO int confirm3 (const std::string&);
|
||||
// TODO int confirm4 (const std::string&);
|
||||
// TODO void delay (float);
|
||||
// TODO std::string formatSeconds (time_t);
|
||||
// TODO std::string formatSecondsCompact (time_t);
|
||||
|
||||
// std::string formatSeconds (time_t);
|
||||
t.is (formatSeconds (0), "-", "0 -> -");
|
||||
t.is (formatSeconds (1), "1 sec", "1 -> 1 sec");
|
||||
t.is (formatSeconds (2), "2 secs", "2 -> 2 secs");
|
||||
t.is (formatSeconds (59), "59 secs", "59 -> 59 secs");
|
||||
t.is (formatSeconds (60), "1 min", "60 -> 1 min");
|
||||
t.is (formatSeconds (119), "1 min", "119 -> 1 min");
|
||||
t.is (formatSeconds (120), "2 mins", "120 -> 2 mins");
|
||||
t.is (formatSeconds (121), "2 mins", "121 -> 2 mins");
|
||||
t.is (formatSeconds (3599), "59 mins", "3599 -> 59 mins");
|
||||
t.is (formatSeconds (3600), "1 hr", "3600 -> 1 hr");
|
||||
t.is (formatSeconds (3601), "1 hr", "3601 -> 1 hr");
|
||||
t.is (formatSeconds (86399), "23 hrs", "86399 -> 23 hrs");
|
||||
t.is (formatSeconds (86400), "1 day", "86400 -> 1 day");
|
||||
t.is (formatSeconds (86401), "1 day", "86401 -> 1 day");
|
||||
t.is (formatSeconds (14 * 86400 - 1), "1 wk", "14 days - 1 sec -> 1 wk");
|
||||
t.is (formatSeconds (14 * 86400), "2 wks", "14 days -> 2 wks");
|
||||
t.is (formatSeconds (14 * 86400 + 1), "2 wks", "14 days + 1 sec -> 2 wks");
|
||||
t.is (formatSeconds (85 * 86400 - 1), "2 mths", "85 days - 1 sec -> 2 mths");
|
||||
t.is (formatSeconds (85 * 86400), "2 mths", "85 days -> 2 mths");
|
||||
t.is (formatSeconds (85 * 86400 + 1), "2 mths", "85 days + 1 sec -> 2 mths");
|
||||
t.is (formatSeconds (365 * 86400 - 1), "1.0 yrs", "365 days - 1 sec -> 1.0 yrs");
|
||||
t.is (formatSeconds (365 * 86400), "1.0 yrs", "365 days -> 1.0 yrs");
|
||||
t.is (formatSeconds (365 * 86400 + 1), "1.0 yrs", "365 days + 1 sec -> 1.0 yrs");
|
||||
|
||||
// std::string formatSecondsCompact (time_t);
|
||||
t.is (formatSecondsCompact (0), "-", "0 -> -");
|
||||
t.is (formatSecondsCompact (1), "1s", "1 -> 1s");
|
||||
t.is (formatSecondsCompact (2), "2s", "2 -> 2s");
|
||||
t.is (formatSecondsCompact (59), "59s", "59 -> 59s");
|
||||
t.is (formatSecondsCompact (60), "1m", "60 -> 1m");
|
||||
t.is (formatSecondsCompact (119), "1m", "119 -> 1m");
|
||||
t.is (formatSecondsCompact (120), "2m", "120 -> 2m");
|
||||
t.is (formatSecondsCompact (121), "2m", "121 -> 2m");
|
||||
t.is (formatSecondsCompact (3599), "59m", "3599 -> 59m");
|
||||
t.is (formatSecondsCompact (3600), "1h", "3600 -> 1h");
|
||||
t.is (formatSecondsCompact (3601), "1h", "3601 -> 1h");
|
||||
t.is (formatSecondsCompact (86399), "23h", "86399 -> 23h");
|
||||
t.is (formatSecondsCompact (86400), "1d", "86400 -> 1d");
|
||||
t.is (formatSecondsCompact (86401), "1d", "86401 -> 1d");
|
||||
t.is (formatSecondsCompact (14 * 86400 - 1), "1wk", "14 days - 1 sec -> 1wk");
|
||||
t.is (formatSecondsCompact (14 * 86400), "2wk", "14 days -> 2wk");
|
||||
t.is (formatSecondsCompact (14 * 86400 + 1), "2wk", "14 days + 1 sec -> 2wk");
|
||||
t.is (formatSecondsCompact (85 * 86400 - 1), "2mo", "85 days - 1 sec -> 2mo");
|
||||
t.is (formatSecondsCompact (85 * 86400), "2mo", "85 days -> 2mo");
|
||||
t.is (formatSecondsCompact (85 * 86400 + 1), "2mo", "85 days + 1 sec -> 2mo");
|
||||
t.is (formatSecondsCompact (365 * 86400 - 1), "1.0y", "365 days - 1 sec -> 1.0y");
|
||||
t.is (formatSecondsCompact (365 * 86400), "1.0y", "365 days -> 1.0y");
|
||||
t.is (formatSecondsCompact (365 * 86400 + 1), "1.0y", "365 days + 1 sec -> 1.0y");
|
||||
|
||||
// std::string formatBytes (size_t);
|
||||
t.is (formatBytes (0), "0 B", "0 -> 0 B");
|
||||
|
|
24
src/util.cpp
24
src/util.cpp
|
@ -164,7 +164,7 @@ std::string formatSeconds (time_t delta)
|
|||
char formatted[24];
|
||||
float days = (float) delta / 86400.0;
|
||||
|
||||
if (days > 365)
|
||||
if (days >= 365)
|
||||
sprintf (formatted, "%.1f yrs", (days / 365.2422)); // TODO i18n
|
||||
else if (days > 84)
|
||||
sprintf (formatted, "%1d mth%s", // TODO i18n
|
||||
|
@ -174,19 +174,19 @@ std::string formatSeconds (time_t delta)
|
|||
sprintf (formatted, "%d wk%s", // TODO i18n
|
||||
(int) (days / 7.0),
|
||||
((int) (days / 7.0) == 1 ? "" : "s")); // TODO i18n
|
||||
else if (days > 1.0)
|
||||
else if (days >= 1.0)
|
||||
sprintf (formatted, "%d day%s", // TODO i18n
|
||||
(int) days,
|
||||
((int) days == 1 ? "" : "s")); // TODO i18n
|
||||
else if (days * 24 > 1.0)
|
||||
else if (days * 24 >= 1.0)
|
||||
sprintf (formatted, "%d hr%s", // TODO i18n
|
||||
(int) (days * 24.0),
|
||||
((int) (days * 24) == 1 ? "" : "s")); // TODO i18n
|
||||
else if (days * 24 * 60 > 1)
|
||||
else if (days * 24 * 60 >= 1)
|
||||
sprintf (formatted, "%d min%s", // TODO i18n
|
||||
(int) (days * 24 * 60),
|
||||
((int) (days * 24 * 60) == 1 ? "" : "s")); // TODO i18n
|
||||
else if (days * 24 * 60 * 60 > 1)
|
||||
else if (days * 24 * 60 * 60 >= 1)
|
||||
sprintf (formatted, "%d sec%s", // TODO i18n
|
||||
(int) (days * 24 * 60 * 60),
|
||||
((int) (days * 24 * 60 * 60) == 1 ? "" : "s")); // TODO i18n
|
||||
|
@ -203,13 +203,13 @@ std::string formatSecondsCompact (time_t delta)
|
|||
char formatted[24];
|
||||
float days = (float) delta / 86400.0;
|
||||
|
||||
if (days > 365) sprintf (formatted, "%.1fy", (days / 365.2422)); // TODO i18n
|
||||
else if (days > 84) sprintf (formatted, "%1dmo", (int) (days / 30.6)); // TODO i18n
|
||||
else if (days > 13) sprintf (formatted, "%dwk", (int) (days / 7.0)); // TODO i18n
|
||||
else if (days > 1.0) sprintf (formatted, "%dd", (int) days); // TODO i18n
|
||||
else if (days * 24 > 1.0) sprintf (formatted, "%dh", (int) (days * 24.0)); // TODO i18n
|
||||
else if (days * 24 * 60 > 1) sprintf (formatted, "%dm", (int) (days * 24 * 60)); // TODO i18n
|
||||
else if (days * 24 * 3600 > 1) sprintf (formatted, "%ds", (int) (days * 24 * 60 * 60)); // TODO i18n
|
||||
if (days >= 365) sprintf (formatted, "%.1fy", (days / 365.2422)); // TODO i18n
|
||||
else if (days > 84) sprintf (formatted, "%1dmo", (int) (days / 30.6)); // TODO i18n
|
||||
else if (days > 13) sprintf (formatted, "%dwk", (int) (days / 7.0)); // TODO i18n
|
||||
else if (days >= 1.0) sprintf (formatted, "%dd", (int) days); // TODO i18n
|
||||
else if (days * 24 >= 1.0) sprintf (formatted, "%dh", (int) (days * 24.0)); // TODO i18n
|
||||
else if (days * 24 * 60 >= 1) sprintf (formatted, "%dm", (int) (days * 24 * 60)); // TODO i18n
|
||||
else if (days * 24 * 3600 >= 1) sprintf (formatted, "%ds", (int) (days * 24 * 60 * 60)); // TODO i18n
|
||||
else
|
||||
strcpy (formatted, "-"); // no i18n
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue