[icinga-checkins] icinga.org: icinga2/master: Fix unit tests for Convert::ToString

git at icinga.org git at icinga.org
Thu Jun 16 15:32:42 CEST 2016


Module: icinga2
Branch: master
Commit: 039461e218b76cd2a4a4f23dad0b0da58207c53b
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=039461e218b76cd2a4a4f23dad0b0da58207c53b

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Thu Jun 16 15:32:29 2016 +0200

Fix unit tests for Convert::ToString

refs #11483

---

 lib/base/convert.cpp         |    6 ++++++
 lib/base/value-operators.cpp |    9 +--------
 test/base-convert.cpp        |    4 ++--
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/lib/base/convert.cpp b/lib/base/convert.cpp
index 45f6a96..5454a2f 100644
--- a/lib/base/convert.cpp
+++ b/lib/base/convert.cpp
@@ -35,6 +35,12 @@ String Convert::ToString(const Value& val)
 
 String Convert::ToString(double val)
 {
+	double integral;
+	double fractional = std::modf(val, &integral);
+
+	if (fractional == 0)
+		return Convert::ToString(static_cast<long>(val));
+
 	std::ostringstream msgbuf;
 	msgbuf << std::fixed << val;
 	return msgbuf.str();
diff --git a/lib/base/value-operators.cpp b/lib/base/value-operators.cpp
index a658180..580fafc 100644
--- a/lib/base/value-operators.cpp
+++ b/lib/base/value-operators.cpp
@@ -56,19 +56,12 @@ Value::operator double(void) const
 Value::operator String(void) const
 {
 	Object *object;
-	double dvalue, integral, fractional;
 
 	switch (GetType()) {
 		case ValueEmpty:
 			return String();
 		case ValueNumber:
-			dvalue = boost::get<double>(m_Value);
-			fractional = std::modf(dvalue, &integral);
-
-			if (fractional != 0)
-				return Convert::ToString(dvalue);
-			else
-				return Convert::ToString(static_cast<long>(dvalue));
+			return Convert::ToString(boost::get<double>(m_Value));
 		case ValueBoolean:
 			if (boost::get<bool>(m_Value))
 				return "true";
diff --git a/test/base-convert.cpp b/test/base-convert.cpp
index 57da580..16e84da 100644
--- a/test/base-convert.cpp
+++ b/test/base-convert.cpp
@@ -46,14 +46,14 @@ BOOST_AUTO_TEST_CASE(todouble)
 BOOST_AUTO_TEST_CASE(tostring)
 {
 	BOOST_CHECK(Convert::ToString(7) == "7");
-	BOOST_CHECK(Convert::ToString(7.5) == "7.5");
+	BOOST_CHECK(Convert::ToString(7.5) == "7.500000");
 	BOOST_CHECK(Convert::ToString("hello") == "hello");
 
 	String str = "hello";
 	BOOST_CHECK(Convert::ToString(str) == "hello");
 
 	BOOST_CHECK(Convert::ToString(Value(7)) == "7");
-	BOOST_CHECK(Convert::ToString(Value(7.5)) == "7.5");
+	BOOST_CHECK(Convert::ToString(Value(7.5)) == "7.500000");
 	BOOST_CHECK(Convert::ToString(Value("hello")) == "hello");
 	BOOST_CHECK(Convert::ToString(Value("hello hello")) == "hello hello");
 }



More information about the icinga-checkins mailing list