[mapguide-commits] r9224 - in sandbox/jng/geoprocessing: Common/Foundation/System Server/src/UnitTesting
svn_mapguide at osgeo.org
svn_mapguide at osgeo.org
Sun Jun 25 05:37:29 PDT 2017
Author: jng
Date: 2017-06-25 05:37:28 -0700 (Sun, 25 Jun 2017)
New Revision: 9224
Modified:
sandbox/jng/geoprocessing/Common/Foundation/System/Util.cpp
sandbox/jng/geoprocessing/Common/Foundation/System/Util.h
sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.cpp
sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.h
Log:
Add an overload of MgUtil::DoubleToString() to allow specifying a specific number of decimals.
Add unit tests to exercise this
Modified: sandbox/jng/geoprocessing/Common/Foundation/System/Util.cpp
===================================================================
--- sandbox/jng/geoprocessing/Common/Foundation/System/Util.cpp 2017-06-25 10:38:15 UTC (rev 9223)
+++ sandbox/jng/geoprocessing/Common/Foundation/System/Util.cpp 2017-06-25 12:37:28 UTC (rev 9224)
@@ -1083,6 +1083,22 @@
str = &buf[0];
}
+void MgUtil::DoubleToString(double val, string& str, INT32 decimals)
+{
+ char buf[64] = { 0 };
+
+ ::sprintf(buf, "%.*g", decimals + 1, val);
+ str = &buf[0];
+}
+
+void MgUtil::DoubleToString(double val, STRING& str, INT32 decimals)
+{
+ wchar_t buf[64] = { 0 };
+
+ ::swprintf(buf, 64, L"%.*g", decimals + 1, val);
+ str = &buf[0];
+}
+
bool MgUtil::ValuesEqual(double value1, double value2, double tolerance, bool output)
{
bool valuesEqual = true;
Modified: sandbox/jng/geoprocessing/Common/Foundation/System/Util.h
===================================================================
--- sandbox/jng/geoprocessing/Common/Foundation/System/Util.h 2017-06-25 10:38:15 UTC (rev 9223)
+++ sandbox/jng/geoprocessing/Common/Foundation/System/Util.h 2017-06-25 12:37:28 UTC (rev 9224)
@@ -553,6 +553,8 @@
static void SingleToString(float val, STRING& str);
static void DoubleToString(double val, string& str);
static void DoubleToString(double val, STRING& str);
+ static void DoubleToString(double val, string& str, INT32 decimals);
+ static void DoubleToString(double val, STRING& str, INT32 decimals);
static bool ValuesEqual(double value1, double value2,
double tolerance = MgUtil::DefaultCompareTolerance,
Modified: sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.cpp
===================================================================
--- sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.cpp 2017-06-25 10:38:15 UTC (rev 9223)
+++ sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.cpp 2017-06-25 12:37:28 UTC (rev 9224)
@@ -643,4 +643,51 @@
{
throw;
}
+}
+
+void TestMisc::TestCase_DoubleToStringWithDecimals()
+{
+ try
+ {
+ double d = 1.23456789032748754;
+
+ std::string s;
+ STRING ws;
+
+ MgUtil::DoubleToString(d, ws, 4);
+ MgUtil::DoubleToString(d, s, 4);
+
+ CPPUNIT_ASSERT(L"1.2346" == ws);
+ CPPUNIT_ASSERT("1.2346" == s);
+
+ ws.clear();
+ s.clear();
+
+ MgUtil::DoubleToString(d, ws, 8);
+ MgUtil::DoubleToString(d, s, 8);
+
+ CPPUNIT_ASSERT(L"1.23456789" == ws);
+ CPPUNIT_ASSERT("1.23456789" == s);
+
+ double d1 = 1.1;
+
+ ws.clear();
+ s.clear();
+
+ MgUtil::DoubleToString(d1, ws, 4);
+ MgUtil::DoubleToString(d1, s, 4);
+
+ CPPUNIT_ASSERT(L"1.1" == ws);
+ CPPUNIT_ASSERT("1.1" == s);
+ }
+ catch (MgException* e)
+ {
+ STRING message = e->GetDetails(TEST_LOCALE);
+ SAFE_RELEASE(e);
+ CPPUNIT_FAIL(MG_WCHAR_TO_CHAR(message.c_str()));
+ }
+ catch (...)
+ {
+ throw;
+ }
}
\ No newline at end of file
Modified: sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.h
===================================================================
--- sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.h 2017-06-25 10:38:15 UTC (rev 9223)
+++ sandbox/jng/geoprocessing/Server/src/UnitTesting/TestMisc.h 2017-06-25 12:37:28 UTC (rev 9224)
@@ -31,6 +31,7 @@
CPPUNIT_TEST(TestCase_1304);
CPPUNIT_TEST(TestCase_MapLayerCollections);
CPPUNIT_TEST(TestCase_ApiVersionCheck);
+ CPPUNIT_TEST(TestCase_DoubleToStringWithDecimals);
CPPUNIT_TEST(TestEnd); // This must be the very last unit test
CPPUNIT_TEST_SUITE_END();
@@ -51,6 +52,7 @@
void TestCase_MapLayerCollections();
void TestCase_CreateMapWithInitialDisplayParams();
void TestCase_ApiVersionCheck();
+ void TestCase_DoubleToStringWithDecimals();
private:
Ptr<MgSiteConnection> m_siteConnection;
More information about the mapguide-commits
mailing list