diff --git a/subiquity/models/filesystem.py b/subiquity/models/filesystem.py index b31655c3..a0119949 100644 --- a/subiquity/models/filesystem.py +++ b/subiquity/models/filesystem.py @@ -34,11 +34,14 @@ class FS: def humanize_size(size): - size = abs(size) if size == 0: return "0B" - p = math.floor(math.log(size, 2) / 10) - return "%.3f%s" % (size / math.pow(1024, p), HUMAN_UNITS[int(p)]) + p = int(math.floor(math.log(size, 2) / 10)) + # We want to truncate the non-integral part, not round to nearest. + s = "{:.17f}".format(size / 2**(10*p)) + i = s.index('.') + s = s[:i+4] + return s + HUMAN_UNITS[int(p)] def dehumanize_size(size): diff --git a/subiquity/models/tests/__init__.py b/subiquity/models/tests/__init__.py new file mode 100644 index 00000000..792d6005 --- /dev/null +++ b/subiquity/models/tests/__init__.py @@ -0,0 +1 @@ +# diff --git a/subiquity/models/tests/test_filesystem.py b/subiquity/models/tests/test_filesystem.py index 43181520..b2a9be89 100644 --- a/subiquity/models/tests/test_filesystem.py +++ b/subiquity/models/tests/test_filesystem.py @@ -1,7 +1,23 @@ import unittest -from subiquity.models.filesystem import dehumanize_size +from subiquity.models.filesystem import dehumanize_size, humanize_size + +class TestHumanizeSize(unittest.TestCase): + + + basics = [ + ('1.000M', 2**20), + ('1.500M', 2**20+2**19), + ('1.500M', 2**20+2**19), + ('1023.000M', 1023*2**20), + ('1.000G', 1024*2**20), + ] + + def test_basics(self): + for string, integer in self.basics: + with self.subTest(input=string): + self.assertEqual(string, humanize_size(integer)) class TestDehumanizeSize(unittest.TestCase):