]> git.saurik.com Git - apt.git/commitdiff
don't perform int<float in progress bar drawing
authorDavid Kalnischkies <david@kalnischkies.de>
Fri, 12 Aug 2016 07:07:59 +0000 (09:07 +0200)
committerDavid Kalnischkies <david@kalnischkies.de>
Fri, 12 Aug 2016 09:12:04 +0000 (11:12 +0200)
Comparing floating numbers is always fun and in this instance a 9 < 9.0
is "somehow" true on hurd-i386 letting the tests fail by reporting that
too much progress achieved. A bit mysterious, but with some rework we
can use code which avoids dealing with the floats in this way entirely
and make our testcases happy.

apt-pkg/install-progress.cc
test/libapt/install_progress_test.cc

index c77c240c33800c91cc52d4739b76e8fe48ea0d4d..be5b8b5c7a2f5d8b79ebb035aaa3f65f07694730 100644 (file)
@@ -14,6 +14,7 @@
 #include <algorithm>
 #include <stdio.h>
 #include <sstream>
+#include <cmath>
 
 #include <apti18n.h>
 
@@ -321,19 +322,14 @@ std::string
 PackageManagerFancy::GetTextProgressStr(float Percent, int OutputSize)
 {
    std::string output;
-   int i;
-   
-   // should we raise a exception here instead?
-   if (Percent < 0.0 || Percent > 1.0 || OutputSize < 3)
+   if (unlikely(OutputSize < 3))
       return output;
-   
-   int BarSize = OutputSize - 2; // bar without the leading "[" and trailing "]"
-   output += "[";
-   for(i=0; i < BarSize*Percent; i++)
-      output += "#";
-   for (/*nothing*/; i < BarSize; i++)
-      output += ".";
-   output += "]";
+
+   int const BarSize = OutputSize - 2; // bar without the leading "[" and trailing "]"
+   int const BarDone = std::max(0, std::min(BarSize, static_cast<int>(std::floor(Percent * BarSize))));
+   output.append("[");
+   std::fill_n(std::fill_n(std::back_inserter(output), BarDone, '#'), BarSize - BarDone, '.');
+   output.append("]");
    return output;
 }
 
index a70fc9261b8f30ee97a8cb21a1621125b55ab1f6..b63b4716fbde736273aad5fc58b0993761ebdeda 100644 (file)
@@ -12,9 +12,14 @@ TEST(InstallProgressTest, FancyGetTextProgressStr)
 
    EXPECT_EQ(60, p.GetTextProgressStr(0.5, 60).size());
    EXPECT_EQ("[#.]", p.GetTextProgressStr(0.5, 4));
+   EXPECT_EQ("[..........]", p.GetTextProgressStr(0.0, 12));
    EXPECT_EQ("[#.........]", p.GetTextProgressStr(0.1, 12));
+   EXPECT_EQ("[####......]", p.GetTextProgressStr(0.4999, 12));
+   EXPECT_EQ("[#####.....]", p.GetTextProgressStr(0.5001, 12));
    EXPECT_EQ("[#########.]", p.GetTextProgressStr(0.9, 12));
+   EXPECT_EQ("[##########]", p.GetTextProgressStr(1.0, 12));
 
    // deal with incorrect inputs gracefully (or should we die instead?)
-   EXPECT_EQ("", p.GetTextProgressStr(-999, 12));
+   EXPECT_EQ("[..........]", p.GetTextProgressStr(-1.0, 12));
+   EXPECT_EQ("[##########]", p.GetTextProgressStr(2.0, 12));
 }