CharArrayFormatter: using %g instead of %f for better readability

This commit is contained in:
Pavel Kirienko
2014-08-16 13:43:24 +04:00
parent e2e358bb06
commit d816d58b30
3 changed files with 7 additions and 7 deletions
@@ -29,7 +29,7 @@ class UAVCAN_EXPORT CharArrayFormatter
typename std::enable_if<std::is_floating_point<T>::value>::type
writeValue(T value)
{
array_.template appendFormatted<double>("%f", double(value));
array_.template appendFormatted<double>("%g", double(value));
}
template <typename T>
@@ -29,8 +29,8 @@ TEST(CharArrayFormatter, Basic)
f.write(" abc%idef ", 123);
ASSERT_STREQ("Don't Panic. abc123def ", f.getArray().c_str());
f.write("%f", 0.0);
ASSERT_STREQ("Don't Panic. abc123def 0.000000", f.getArray().c_str());
f.write("%g", 0.0);
ASSERT_STREQ("Don't Panic. abc123def 0", f.getArray().c_str());
a.clear();
ASSERT_STREQ("", f.getArray().c_str());
@@ -54,11 +54,11 @@ TEST(CharArrayFormatter, Hardcore)
f.write(
"%% char='%*' double='%*' long='%*' unsigned long='%*' int='%s' long double='%*' bool='%*' const char='%*' %%",
'%', -12.3456, -123456789123456789L, 987654321, -123456789, 0.000001L, true, "Don't Panic.");
'%', -12.3456, -123456789123456789L, 987654321, -123456789, 0.000000001L, true, "Don't Panic.");
static const std::string Reference =
"% char='%' double='-12.345600' long='-123456789123456789' unsigned long='987654321' int='-123456789' "
"long double='0.000001' bool='1' const char='Don't"; // 8 chars truncated!
"% char='%' double='-12.3456' long='-123456789123456789' unsigned long='987654321' int='-123456789' "
"long double='1e-09' bool='1' const char='Don't Pani"; // few chars truncated!
ASSERT_STREQ(Reference.c_str(), f.getArray().c_str());
+1 -1
View File
@@ -136,7 +136,7 @@ TEST(Logger, Cpp11Formatting)
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10));
ASSERT_EQ(log_sub.collector.msg->level.value, uavcan::protocol::debug::LogLevel::WARNING);
ASSERT_EQ(log_sub.collector.msg->source, "foo");
ASSERT_EQ(log_sub.collector.msg->text, "char='$', double is 12.340000");
ASSERT_EQ(log_sub.collector.msg->text, "char='$', double is 12.34");
}
#endif