Bug Fix - broken unit tests on Ubuntu

- Fixed util.cpp formatSeconds and formatSecondsCompact, that were using
  an algorithm that accentuated rounding errors.
- Fixed unit tests that were expecting wrong answers from the wrong
  algorithm above.
This commit is contained in:
Paul Beckingham 2009-12-12 11:30:20 -05:00
parent 37411c7521
commit eaeca45eae
2 changed files with 21 additions and 22 deletions

View file

@ -62,7 +62,7 @@ int main (int argc, char** argv)
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), "11 mths", "365 days - 1 sec -> 11 mths");
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");
@ -87,7 +87,7 @@ int main (int argc, char** argv)
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), "11mo", "365 days - 1 sec -> 11mo");
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");

View file

@ -174,22 +174,22 @@ 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 (delta >= 86400)
sprintf (formatted, "%d day%s", // TODO i18n
(int) days,
((int) days == 1 ? "" : "s")); // TODO i18n
else if (days * 24 >= 1.0)
else if (delta >= 3600)
sprintf (formatted, "%d hr%s", // TODO i18n
(int) (days * 24.0),
((int) (days * 24) == 1 ? "" : "s")); // TODO i18n
else if (days * 24 * 60 >= 1)
(int) (delta / 3600),
((int) (delta / 3600) == 1 ? "" : "s")); // TODO i18n
else if (delta >= 60)
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)
(int) (delta / 60),
((int) (delta / 60) == 1 ? "" : "s")); // TODO i18n
else if (delta >= 1)
sprintf (formatted, "%d sec%s", // TODO i18n
(int) (days * 24 * 60 * 60),
((int) (days * 24 * 60 * 60) == 1 ? "" : "s")); // TODO i18n
(int) delta,
((int) delta == 1 ? "" : "s")); // TODO i18n
else
strcpy (formatted, "-"); // no i18n
@ -206,12 +206,11 @@ std::string formatSecondsCompact (time_t delta)
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
else if (delta >= 86400) sprintf (formatted, "%dd", (int) days); // TODO i18n
else if (delta >= 3600) sprintf (formatted, "%dh", (int) (delta / 3600)); // TODO i18n
else if (delta >= 60) sprintf (formatted, "%dm", (int) (delta / 60)); // TODO i18n
else if (delta >= 1) sprintf (formatted, "%ds", (int) delta); // TODO i18n
else strcpy (formatted, "-");
return std::string (formatted);
}