Test double formatting.

This commit is contained in:
Victor Zverovich 2012-12-10 12:16:02 -08:00
parent 27b8ba06e8
commit d73306bdce
2 changed files with 49 additions and 4 deletions

View File

@ -157,11 +157,14 @@ void fmt::Formatter::FormatDouble(
T value, unsigned flags, int width, int precision, char type) { T value, unsigned flags, int width, int precision, char type) {
// Check type. // Check type.
switch (type) { switch (type) {
case 0:
type = 'g';
break;
case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
break; break;
default: default:
// TODO: error throw FormatError(
break; str(fmt::Format("unknown format code '{0}' for double") << type));
} }
// Build format string. // Build format string.
@ -181,7 +184,7 @@ void fmt::Formatter::FormatDouble(
} }
if (IsLongDouble<T>::VALUE) if (IsLongDouble<T>::VALUE)
*format_ptr++ = 'L'; *format_ptr++ = 'L';
*format_ptr++ = type ? type : 'g'; *format_ptr++ = type;
*format_ptr = '\0'; *format_ptr = '\0';
// Format using snprintf. // Format using snprintf.

View File

@ -258,11 +258,19 @@ TEST(FormatterTest, Precision) {
FormatError, "precision specifier requires floating-point argument"); FormatError, "precision specifier requires floating-point argument");
} }
TEST(FormatterTest, Type) { TEST(FormatterTest, FormatInt) {
EXPECT_THROW_MSG(Format("{0:v") << 42, EXPECT_THROW_MSG(Format("{0:v") << 42,
FormatError, "unmatched '{' in format"); FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(Format("{0:v}") << 42, EXPECT_THROW_MSG(Format("{0:v}") << 42,
FormatError, "unknown format code 'v' for integer"); FormatError, "unknown format code 'v' for integer");
EXPECT_THROW_MSG(Format("{0:c}") << 42,
FormatError, "unknown format code 'c' for integer");
EXPECT_THROW_MSG(Format("{0:e}") << 42,
FormatError, "unknown format code 'e' for integer");
EXPECT_THROW_MSG(Format("{0:f}") << 42,
FormatError, "unknown format code 'f' for integer");
EXPECT_THROW_MSG(Format("{0:g}") << 42,
FormatError, "unknown format code 'g' for integer");
} }
TEST(FormatterTest, FormatDec) { TEST(FormatterTest, FormatDec) {
@ -333,6 +341,40 @@ TEST(FormatterTest, FormatOct) {
EXPECT_EQ(buffer, str(Format("{0:o}") << ULONG_MAX)); EXPECT_EQ(buffer, str(Format("{0:o}") << ULONG_MAX));
} }
TEST(FormatterTest, FormatDouble) {
EXPECT_THROW_MSG(Format("{0:c}") << 1.2,
FormatError, "unknown format code 'c' for double");
EXPECT_THROW_MSG(Format("{0:d}") << 1.2,
FormatError, "unknown format code 'd' for double");
EXPECT_THROW_MSG(Format("{0:o}") << 1.2,
FormatError, "unknown format code 'o' for double");
EXPECT_THROW_MSG(Format("{0:x}") << 1.2,
FormatError, "unknown format code 'x' for double");
EXPECT_EQ("0", str(Format("{0:}") << 0.0));
EXPECT_EQ("0.000000", str(Format("{0:f}") << 0.0));
EXPECT_EQ("392.65", str(Format("{0:}") << 392.65));
EXPECT_EQ("392.65", str(Format("{0:g}") << 392.65));
EXPECT_EQ("392.65", str(Format("{0:G}") << 392.65));
EXPECT_EQ("392.650000", str(Format("{0:f}") << 392.65));
EXPECT_EQ("392.650000", str(Format("{0:F}") << 392.65));
EXPECT_EQ("3.926500e+02", str(Format("{0:e}") << 392.65));
EXPECT_EQ("3.926500E+02", str(Format("{0:E}") << 392.65));
EXPECT_EQ("+0000392.6", str(Format("{0:+010.4g}") << 392.65));
}
TEST(FormatterTest, FormatLongDouble) {
EXPECT_EQ("0", str(Format("{0:}") << 0.0l));
EXPECT_EQ("0.000000", str(Format("{0:f}") << 0.0l));
EXPECT_EQ("392.65", str(Format("{0:}") << 392.65l));
EXPECT_EQ("392.65", str(Format("{0:g}") << 392.65l));
EXPECT_EQ("392.65", str(Format("{0:G}") << 392.65l));
EXPECT_EQ("392.650000", str(Format("{0:f}") << 392.65l));
EXPECT_EQ("392.650000", str(Format("{0:F}") << 392.65l));
EXPECT_EQ("3.926500e+02", str(Format("{0:e}") << 392.65l));
EXPECT_EQ("3.926500E+02", str(Format("{0:E}") << 392.65l));
EXPECT_EQ("+0000392.6", str(Format("{0:+010.4g}") << 392.65l));
}
TEST(FormatterTest, FormatChar) { TEST(FormatterTest, FormatChar) {
EXPECT_EQ("a*b", str(Format("{0}{1}{2}") << 'a' << '*' << 'b')); EXPECT_EQ("a*b", str(Format("{0}{1}{2}") << 'a' << '*' << 'b'));
} }