[blivet:master 2/2] Correct bug where fractional quantities were shown as exact.

mulhern amulhern at redhat.com
Thu Sep 4 14:34:06 UTC 2014


To follow the basic engineering principle of not showing more
precision than actually exists.

Add a few more tests.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/size.py     | 21 +++++++++++----------
 tests/size_test.py | 14 +++++++++++---
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/blivet/size.py b/blivet/size.py
index b9f0089..9e930b1 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -312,20 +312,21 @@ class Size(Decimal):
                 return "%s %s" % (in_bytes, _("B"))
 
         retval = newcheck
-        if places is not None:
-            retval = round(newcheck, places)
-
-        if max_places is not None:
-            if places is not None:
-                limit = min((places, max_places))
-            else:
-                limit = max_places
-            retval = round(newcheck, limit)
-
         if retval == int(retval):
             # integer value, no point in showing ".0" at the end
             retval = int(retval)
 
+        else:
+            if places is not None:
+                retval = round(newcheck, places)
+
+            if max_places is not None:
+                if places is not None:
+                    limit = min((places, max_places))
+                else:
+                    limit = max_places
+                retval = round(newcheck, limit)
+
         # Format the value with '.' as the decimal separator
         # If necessary, substitute with a localized separator before returning
         retval_str = str(retval)
diff --git a/tests/size_test.py b/tests/size_test.py
index 1058968..3c4119b 100644
--- a/tests/size_test.py
+++ b/tests/size_test.py
@@ -115,10 +115,8 @@ class SizeTestCase(unittest.TestCase):
         s = Size(16384 - (1024/100 + 1))
         self.assertEquals(s.humanReadable(max_places=2), "15.99 KiB")
 
-    @unittest.expectedFailure
     def testHumanReadableFractionalQuantities(self):
-        # If the value deviates from the a whole number of units by
-        # less than 1%, but still deviates, a fractional quantity is not shown
+        # non-exact quantities are shown as such
         s = Size(0xfffffffffffff)
         self.assertEquals(s.humanReadable(max_places=2), "4096.0 TiB")
         s = Size(0xfffff)
@@ -127,6 +125,16 @@ class SizeTestCase(unittest.TestCase):
         self.assertEquals(s.humanReadable(max_places=2), "64.0 KiB")
         s = Size(16383)
         self.assertEquals(s.humanReadable(max_places=2), "16.0 KiB")
+        # deviation is less than 1/2 of 1% of 1024
+        s = Size(16384 - (1024/100/2))
+        self.assertEquals(s.humanReadable(max_places=2), "16.0 KiB")
+        # deviation is greater than 1/2 of 1% of 1024
+        s = Size(16384 - ((1024/100/2) + 1))
+        self.assertEquals(s.humanReadable(max_places=2), "15.99 KiB")
+
+        # exact quantities are shown as exact
+        s = Size(0x10000000000000)
+        self.assertEquals(s.humanReadable(max_places=2), "4096 TiB")
 
     def testNegative(self):
         s = Size("-500MiB")
-- 
1.9.3



More information about the anaconda-patches mailing list