Skip to content

Commit 4c6483e

Browse files
edomora97lw
authored andcommitted
Fix bug if used memory is between 1000 and 1024 KiB, MiB, ...
If the memory used (or other values used as parameter of format_size) ends in the range [1000, 1024) KiB/MiB/GiB... AWS and CWS will fail rendering that size and the submission details wont be shown! This because the number of digits of the number was supposed being at most 3, not true anymore since 1024 is used as the base. Allowing an extra significant digit for those cases is the proposed solution.
1 parent 790f68e commit 4c6483e

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

cms/locale/locale.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# Copyright © 2014 Fabian Gundlach <320pointsguy@gmail.com>
1212
# Copyright © 2015 William Di Luigi <williamdiluigi@gmail.com>
1313
# Copyright © 2016 Myungwoo Chun <mc.tamaki@gmail.com>
14+
# Copyright © 2018 Edoardo Morassutto <edoardo.morassutto@gmail.com>
1415
#
1516
# This program is free software: you can redistribute it and/or modify
1617
# it under the terms of the GNU Affero General Public License as
@@ -238,7 +239,8 @@ def format_size(self, n):
238239
if n < self.PREFIX_FACTOR:
239240
f = copy.copy(self.locale.decimal_formats[None])
240241
# We need int because floor returns a float in py2.
241-
d = int(2 - math.floor(math.log10(n)))
242+
# if 1000 <= n < 1024 d can be negative, cap to 0 decimals
243+
d = max(0, int(2 - math.floor(math.log10(n))))
242244
f.frac_prec = (d, d)
243245
return (self.gettext(unit)
244246
% babel.numbers.format_decimal(n, format=f,

cmstestsuite/unit_tests/locale/locale_test.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Contest Management System - http://cms-dev.github.io/
55
# Copyright © 2018 Luca Wehrstedt <luca.wehrstedt@gmail.com>
6+
# Copyright © 2018 Edoardo Morassutto <edoardo.morassutto@gmail.com>
67
#
78
# This program is free software: you can redistribute it and/or modify
89
# it under the terms of the GNU Affero General Public License as
@@ -471,15 +472,33 @@ def test_small_values(self):
471472
self.assertEqual(ENGLISH.format_size(2),
472473
"2 bytes")
473474

474-
def test_cutoff(self):
475-
# Cutoff is at 1000, not 1024, as we use kilo, mega, ... rather
476-
# than kibi, mebi, ...
475+
def test_cutoff_kib(self):
477476
self.assertEqual(ENGLISH.format_size(999),
478477
"999 bytes")
479478
self.assertEqual(ENGLISH.format_size(1000),
480479
"1,000 bytes")
480+
self.assertEqual(ENGLISH.format_size(1001),
481+
"1,001 bytes")
482+
self.assertEqual(ENGLISH.format_size(1023),
483+
"1,023 bytes")
481484
self.assertEqual(ENGLISH.format_size(1024),
482485
"1.00 KiB")
486+
self.assertEqual(ENGLISH.format_size(1025),
487+
"1.00 KiB")
488+
489+
def test_cutoff_mib(self):
490+
self.assertEqual(ENGLISH.format_size(999 * 1024),
491+
"999 KiB")
492+
self.assertEqual(ENGLISH.format_size(1000 * 1024),
493+
"1,000 KiB")
494+
self.assertEqual(ENGLISH.format_size(1001 * 1024),
495+
"1,001 KiB")
496+
self.assertEqual(ENGLISH.format_size(1023 * 1024),
497+
"1,023 KiB")
498+
self.assertEqual(ENGLISH.format_size(1024 * 1024),
499+
"1.00 MiB")
500+
self.assertEqual(ENGLISH.format_size(1025 * 1024),
501+
"1.00 MiB")
483502

484503
def test_large(self):
485504
# Ensure larger units are used for larger values, with rounding
@@ -505,13 +524,34 @@ def test_localized_small_values(self):
505524
self.assertEqual(FRENCH.format_size(2),
506525
"2 octets")
507526

508-
def test_localized_cutoff(self):
527+
def test_localized_cutoff_kib(self):
509528
self.assertEqual(FRENCH.format_size(999),
510529
"999 octets")
511530
self.assertEqual(FRENCH.format_size(1000),
512531
"1\N{NO-BREAK SPACE}000 octets")
532+
self.assertEqual(FRENCH.format_size(1001),
533+
"1\N{NO-BREAK SPACE}001 octets")
534+
self.assertEqual(FRENCH.format_size(1023),
535+
"1\N{NO-BREAK SPACE}023 octets")
513536
self.assertEqual(FRENCH.format_size(1024),
514537
"1,00 Kio")
538+
self.assertEqual(FRENCH.format_size(1025),
539+
"1,00 Kio")
540+
541+
def test_localized_cutoff_mib(self):
542+
self.assertEqual(FRENCH.format_size(999 * 1024),
543+
"999 Kio")
544+
self.assertEqual(FRENCH.format_size(1000 * 1024),
545+
"1\N{NO-BREAK SPACE}000 Kio")
546+
self.assertEqual(FRENCH.format_size(1001 * 1024),
547+
"1\N{NO-BREAK SPACE}001 Kio")
548+
self.assertEqual(FRENCH.format_size(1023 * 1024),
549+
"1\N{NO-BREAK SPACE}023 Kio")
550+
self.assertEqual(FRENCH.format_size(1024 * 1024),
551+
"1,00 Mio")
552+
self.assertEqual(FRENCH.format_size(1025 * 1024),
553+
"1,00 Mio")
554+
515555

516556
def test_localized_large(self):
517557
self.assertEqual(FRENCH.format_size(2.345 * 1000000),

0 commit comments

Comments
 (0)