Add support for IEC and SI size units to wxFileName::GetHumanReadableSize().

Allow choosing the multiplier to use (1000 or 1024) and the names of the units
(SI or IEC). By default, still use traditional convention, i.e. multiplier of
1024 but SI unit names. This will probably need to be changed to use IEC units
in the future.

Also added unit test for this function.

Closes #10673.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63870 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-04-06 14:53:04 +00:00
parent 2028c33ab5
commit b2edb8f3c5
5 changed files with 151 additions and 44 deletions

View File

@@ -125,6 +125,7 @@ private:
CPPUNIT_TEST( TestStrip );
CPPUNIT_TEST( TestNormalize );
CPPUNIT_TEST( TestReplace );
CPPUNIT_TEST( TestGetHumanReadable );
#ifdef __WINDOWS__
CPPUNIT_TEST( TestShortLongPath );
#endif // __WINDOWS__
@@ -139,6 +140,7 @@ private:
void TestStrip();
void TestNormalize();
void TestReplace();
void TestGetHumanReadable();
#ifdef __WINDOWS__
void TestShortLongPath();
#endif // __WINDOWS__
@@ -478,6 +480,39 @@ void FileNameTestCase::TestReplace()
fn.GetFullPath(wxPATH_UNIX) );
}
void FileNameTestCase::TestGetHumanReadable()
{
static const struct TestData
{
const char *result;
wxULongLong size;
int prec;
wxSizeConvention conv;
} testData[] =
{
{ "NA", 0, 1, wxSIZE_CONV_TRADIONAL },
{ "2.0 KB", 2000, 1, wxSIZE_CONV_TRADIONAL },
{ "1.953 KiB", 2000, 3, wxSIZE_CONV_IEC },
{ "2.000 KB", 2000, 3, wxSIZE_CONV_SI },
{ "297 KB", 304351, 0, wxSIZE_CONV_TRADIONAL },
{ "304 KB", 304351, 0, wxSIZE_CONV_SI },
};
for ( unsigned n = 0; n < WXSIZEOF(testData); n++ )
{
const TestData& td = testData[n];
CPPUNIT_ASSERT_EQUAL
(
td.result,
wxFileName::GetHumanReadableSize(td.size, "NA", td.prec, td.conv)
);
}
// also test the default convention value
CPPUNIT_ASSERT_EQUAL( "1.4 MB", wxFileName::GetHumanReadableSize(1512993, "") );
}
void FileNameTestCase::TestStrip()
{
CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") );