Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/bs_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,8 @@ char* bs_size_convert_to (const BSSize size, BSUnit unit, BSError **error) {

/**
* bs_size_human_readable:
* @min_unit: the smallest unit the returned representation should use
* @min_unit: the smallest unit the returned representation should use.
* Values outside the binary unit range are treated as #BS_BUNIT_B.
* @max_places: maximum number of decimal places the representation should use
* @xlate: whether to try to translate the representation or not
*
Expand All @@ -853,7 +854,7 @@ char* bs_size_human_readable (const BSSize size, BSBunit min_unit, int max_place
mpf_init2 (cur_val, BS_FLOAT_PREC_BITS);
mpf_set_z (cur_val, size->bytes);

if (min_unit == BS_BUNIT_UNDEF)
if (min_unit < BS_BUNIT_B || min_unit > BS_BUNIT_YiB)
min_unit = BS_BUNIT_B;

sign = mpf_sgn (cur_val);
Expand Down
9 changes: 9 additions & 0 deletions tests/lbs_py_override_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ def testConvertTo(self):

locale.setlocale(locale.LC_ALL,'en_US.UTF-8')

def testHumanReadableDecimalMinUnit(self):
for spec in ("0 B", "1.5 KiB", "-1.5 KiB"):
size = Size(spec)
for xlate in (False, True):
expected = size.human_readable(xlate=xlate)
for min_unit in ("KB", "MB"):
with self.subTest(spec=spec, min_unit=min_unit, xlate=xlate):
self.assertEqual(size.human_readable(min_unit, xlate=xlate), expected)

def testRoundToNearest(self):
size = Size("1.5 KiB")
conv = size.round_to_nearest(Size("1 KiB"), rounding=ROUND_UP)
Expand Down
17 changes: 16 additions & 1 deletion tests/libbytesize_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from locale_utils import get_avail_locales, missing_locales, requires_locales

from bytesize import KiB, MB, GiB, ROUND_UP, ROUND_DOWN, ROUND_HALF_UP, OverflowError, InvalidSpecError
from bytesize import B, KiB, MB, GiB, YiB, ROUND_UP, ROUND_DOWN, ROUND_HALF_UP, OverflowError, InvalidSpecError

# SizeStruct is part of the 'private' API and needs to be imported differently
# when running from locally build tree and when using installed library
Expand Down Expand Up @@ -423,6 +423,12 @@ def testGetBytesStr(self):
#enddef

def testHumanReadable(self):
strSizeStruct = SizeStruct.new_from_str("1.5 KiB").human_readable(B, 2, False)
self.assertEqual(strSizeStruct, "1.5 KiB")

strSizeStruct = SizeStruct.new_from_str("1.5 YiB").human_readable(YiB, 2, False)
self.assertEqual(strSizeStruct, "1.5 YiB")

strSizeStruct = SizeStruct.new_from_str("12 KiB").human_readable(KiB, 2, False)
self.assertEqual(strSizeStruct, "12 KiB")

Expand All @@ -447,6 +453,15 @@ def testHumanReadable(self):
self.assertEqual(SizeStruct.new_from_str(strSizeStruct).get_bytes(), (100 * 1024**3, 1))
#enddef

def testHumanReadableInvalidMinUnit(self):
for spec in ("0 B", "1.5 KiB", "-1.5 KiB"):
size = SizeStruct.new_from_str(spec)
for xlate in (False, True):
expected = size.human_readable(B, 2, xlate)
for min_unit in (9, -1, 10, 21, 100):
with self.subTest(spec=spec, min_unit=min_unit, xlate=xlate):
self.assertEqual(size.human_readable(min_unit, 2, xlate), expected)

@requires_locales({'cs_CZ.UTF-8'})
def testHumanReadableLocale(self):
locale.setlocale(locale.LC_ALL, 'cs_CZ.UTF-8')
Expand Down
Loading