From ecb032a239f88ead2087aa98cbcf28522a523922 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 9 Jun 2008 08:11:45 +0000 Subject: - Patch #151902 by MadHarold et al: a better format_size() (and removed some excessive white space). --- includes/common.inc | 18 +++++++++++------- includes/common.test | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ includes/xmlrpc.test | 2 +- 3 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 includes/common.test (limited to 'includes') diff --git a/includes/common.inc b/includes/common.inc index bdf3d1867..883371ff4 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1079,17 +1079,21 @@ function parse_size($size) { * A translated string representation of the size. */ function format_size($size, $langcode = NULL) { - if ($size < 1024) { + if ($size < 1000) { return format_plural($size, '1 byte', '@count bytes', array(), $langcode); } else { - $size = round($size / 1024, 2); - $suffix = t('KB', array(), $langcode); - if ($size >= 1024) { - $size = round($size / 1024, 2); - $suffix = t('MB', array(), $langcode); + $size = $size / 1000; // convert bytes to kilobytes (1000 bytes) + $units = array('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); + foreach ($units as $suffix) { + if (round($size, 2) >= 1000) { + $size = $size / 1000; + } + else { + break; + } } - return t('@size @suffix', array('@size' => $size, '@suffix' => $suffix), $langcode); + return t('@size @suffix', array('@size' => round($size, 2), '@suffix' => $suffix), $langcode); } } diff --git a/includes/common.test b/includes/common.test new file mode 100644 index 000000000..d3f907608 --- /dev/null +++ b/includes/common.test @@ -0,0 +1,54 @@ + t('Format size test'), + 'description' => t('Parse a predefined amount of bytes and compare the output with the expected value.'), + 'group' => t('System') + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + $this->exact_test_cases = array( + '1 byte' => 1, // byte + '1 KB' => 1000, // kilobyte + '1 MB' => 1000000, // megabyte + '1 GB' => 1000000000, // gigabyte + '1 TB' => 1000000000000, // terabyte + '1 PB' => 1000000000000000, // petabyte + '1 EB' => 1000000000000000000, // exabyte + '1 ZB' => 1000000000000000000000, // zettabyte + '1 YB' => 1000000000000000000000000, // yottabyte + ); + $this->rounded_test_cases = array( + '2 bytes' => 2, // bytes + '1 MB' => 999999, // 1 MB (not 1000 kilobyte!) + '3.62 MB' => 3623651, // megabytes + '67.23 PB' => 67234178751368124, // petabytes + '235.35 YB' => 235346823821125814962843827, // yottabytes + ); + parent::setUp(); + } + + /** + * testCommonFormatSize + */ + function testCommonFormatSize() { + foreach (array($this->exact_test_cases, $this->rounded_test_cases) as $test_cases) { + foreach ($test_cases as $expected => $size) { + $this->assertTrue( + ($result = format_size($size, NULL)) == $expected, + $expected . " == " . $result . " (" . $size . " bytes) %s" + ); + } + } + } +} diff --git a/includes/xmlrpc.test b/includes/xmlrpc.test index df5ed66d0..72a230e43 100644 --- a/includes/xmlrpc.test +++ b/includes/xmlrpc.test @@ -12,7 +12,7 @@ class XMLRPCValidator1Test extends DrupalWebTestCase { 'group' => t('XML-RPC') ); } - + function setUp() { parent::setUp('simpletest_xmlrpc'); } -- cgit v1.2.3