From 5d4ef3387f38de93946dda30c9ee1108d9f5d052 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 28 Dec 2012 07:18:30 -0800 Subject: [PATCH] Test examples. --- format_test.cc | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/format_test.cc b/format_test.cc index 4ab1e642..f6c54053 100644 --- a/format_test.cc +++ b/format_test.cc @@ -774,6 +774,14 @@ TEST(FormatterTest, FormatDouble) { sprintf(buffer, "%E", 392.65); EXPECT_EQ(buffer, str(Format("{0:E}") << 392.65)); EXPECT_EQ("+0000392.6", str(Format("{0:+010.4g}") << 392.65)); + double nan = std::numeric_limits::quiet_NaN(); + EXPECT_EQ("nan", str(Format("{}") << nan)); + EXPECT_EQ("-nan", str(Format("{}") << -nan)); + EXPECT_EQ("NAN", str(Format("{:F}") << nan)); + double inf = std::numeric_limits::infinity(); + EXPECT_EQ("inf", str(Format("{}") << inf)); + EXPECT_EQ("-inf", str(Format("{}") << -inf)); + EXPECT_EQ("INF", str(Format("{:F}") << inf)); } TEST(FormatterTest, FormatLongDouble) { @@ -995,7 +1003,43 @@ fmt::TempFormatter ReportError(const char *format) { return fmt::TempFormatter(format); } -TEST(TempFormatterTest, Example) { +TEST(TempFormatterTest, Examples) { + EXPECT_EQ("First, thou shalt count to three", + str(Format("First, thou shalt count to {0}") << "three")); + EXPECT_EQ("Bring me a shrubbery", + str(Format("Bring me a {}") << "shrubbery")); + EXPECT_EQ("From 1 to 3", str(Format("From {} to {}") << 1 << 3)); + + char buffer[256]; + sprintf(buffer, "%03.2f", -1.2); + EXPECT_EQ(buffer, str(Format("{:03.2f}") << -1.2)); + + EXPECT_EQ("a, b, c", str(Format("{0}, {1}, {2}") << 'a' << 'b' << 'c')); + EXPECT_EQ("a, b, c", str(Format("{}, {}, {}") << 'a' << 'b' << 'c')); + EXPECT_EQ("c, b, a", str(Format("{2}, {1}, {0}") << 'a' << 'b' << 'c')); + EXPECT_EQ("abracadabra", str(Format("{0}{1}{0}") << "abra" << "cad")); + + EXPECT_EQ("left aligned ", + str(Format("{:<30}") << "left aligned")); + EXPECT_EQ(" right aligned", + str(Format("{:>30}") << "right aligned")); + EXPECT_EQ(" centered ", + str(Format("{:^30}") << "centered")); + EXPECT_EQ("***********centered***********", + str(Format("{:*^30}") << "centered")); + + EXPECT_EQ("+3.140000; -3.140000", + str(Format("{:+f}; {:+f}") << 3.14 << -3.14)); + EXPECT_EQ(" 3.140000; -3.140000", + str(Format("{: f}; {: f}") << 3.14 << -3.14)); + EXPECT_EQ("3.140000; -3.140000", + str(Format("{:-f}; {:-f}") << 3.14 << -3.14)); + + EXPECT_EQ("int: 42; hex: 2a; oct: 52", + str(Format("int: {0:d}; hex: {0:x}; oct: {0:o}") << 42)); + EXPECT_EQ("int: 42; hex: 0x2a; oct: 052", + str(Format("int: {0:d}; hex: {0:#x}; oct: {0:#o}") << 42)); + std::string path = "somefile"; ReportError("File not found: {0}") << path; }