From eaeca45eaed2d07708b1670c83bfe84277b0ab1b Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 12 Dec 2009 11:30:20 -0500 Subject: [PATCH] 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. --- src/tests/util.t.cpp | 4 ++-- src/util.cpp | 39 +++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/tests/util.t.cpp b/src/tests/util.t.cpp index 8f8404317..9fd1451f0 100644 --- a/src/tests/util.t.cpp +++ b/src/tests/util.t.cpp @@ -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"); diff --git a/src/util.cpp b/src/util.cpp index ab4826042..c6401c4e9 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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 @@ -203,15 +203,14 @@ 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 - else - strcpy (formatted, "-"); // no 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 (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); } @@ -225,7 +224,7 @@ std::string formatBytes (size_t bytes) if (bytes >= 995000000) sprintf (formatted, "%.1f GiB", (bytes / 1000000000.0)); else if (bytes >= 995000) sprintf (formatted, "%.1f MiB", (bytes / 1000000.0)); else if (bytes >= 995) sprintf (formatted, "%.1f KiB", (bytes / 1000.0)); - else sprintf (formatted, "%d B", (int)bytes ); + else sprintf (formatted, "%d B", (int)bytes ); return commify (formatted); }