From 546b62e74fe2d167e8de03ebf0aaf0d9f11633af Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 10 Jun 2014 06:21:41 -0700 Subject: [PATCH] More tests. --- test/format-test.cc | 12 ------------ test/printf-test.cc | 28 ++++++++++++++++++++++++---- test/util.cc | 13 +++++++++++++ test/util.h | 2 ++ 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/test/format-test.cc b/test/format-test.cc index 065f0cf8..6a9db1a0 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -109,17 +108,6 @@ struct WriteChecker { #define CHECK_WRITE_WCHAR(value) \ EXPECT_PRED_FORMAT1(WriteChecker(), value) -#ifdef _MSC_VER -# define vsnprintf vsprintf_s -#endif - -void SPrintf(char *buffer, const char *format, ...) { - std::va_list args; - va_start(args, format); - vsnprintf(buffer, BUFFER_SIZE, format, args); - va_end(args); -} - std::string ReadFile(StringRef filename) { std::ifstream out(filename.c_str()); std::stringstream content; diff --git a/test/printf-test.cc b/test/printf-test.cc index 0e5bca17..d6a3f3ab 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -165,6 +165,9 @@ TEST(PrintfTest, PlusFlag) { EXPECT_PRINTF("-42", "%+d", -42); EXPECT_PRINTF("+0042", "%+05d", 42); EXPECT_PRINTF("+0042", "%0++5d", 42); + + // '+' flag is ignored for non-numeric types. + EXPECT_PRINTF("x", "%+c", 'x'); } TEST(PrintfTest, MinusFlag) { @@ -177,6 +180,9 @@ TEST(PrintfTest, SpaceFlag) { EXPECT_PRINTF("-42", "% d", -42); EXPECT_PRINTF(" 0042", "% 05d", 42); EXPECT_PRINTF(" 0042", "%0 5d", 42); + + // ' ' flag is ignored for non-numeric types. + EXPECT_PRINTF("x", "% c", 'x'); } TEST(PrintfTest, HashFlag) { @@ -189,15 +195,29 @@ TEST(PrintfTest, HashFlag) { EXPECT_PRINTF("-0x42", "%#x", -0x42); EXPECT_PRINTF("0", "%#x", 0); + EXPECT_PRINTF("0x0042", "%#06x", 0x42); EXPECT_PRINTF("0x0042", "%0##6x", 0x42); - EXPECT_PRINTF("-42.0000", "%#g", -42.0); - //EXPECT_PRINTF("-4.200000e+01", "%#e", -42.0); + EXPECT_PRINTF("-42.000000", "%#f", -42.0); + EXPECT_PRINTF("-42.000000", "%#F", -42.0); - // TODO: more tests + char buffer[BUFFER_SIZE]; + SPrintf(buffer, "%#e", -42.0); + EXPECT_PRINTF(buffer, "%#e", -42.0); + SPrintf(buffer, "%#E", -42.0); + EXPECT_PRINTF(buffer, "%#E", -42.0); + + EXPECT_PRINTF("-42.0000", "%#g", -42.0); + EXPECT_PRINTF("-42.0000", "%#G", -42.0); + + // TODO + //EXPECT_PRINTF("-0x1.5p+5", "%#a", -42.0); + //EXPECT_PRINTF("-0x1.5A+5", "%#A", -42.0); + + // '#' flag is ignored for non-numeric types. + EXPECT_PRINTF("x", "%#c", 'x'); } // TODO: test '*' width, precision, length and type specifier #endif - diff --git a/test/util.cc b/test/util.cc index bfcd5a49..28c9603a 100644 --- a/test/util.cc +++ b/test/util.cc @@ -26,8 +26,21 @@ */ #include "util.h" +#include +#include #include +#ifdef _MSC_VER +# define vsnprintf vsprintf_s +#endif + +void SPrintf(char *buffer, const char *format, ...) { + std::va_list args; + va_start(args, format); + vsnprintf(buffer, BUFFER_SIZE, format, args); + va_end(args); +} + void Increment(char *s) { for (int i = static_cast(std::strlen(s)) - 1; i >= 0; --i) { if (s[i] != '9') { diff --git a/test/util.h b/test/util.h index b94d83b0..d5b152c4 100644 --- a/test/util.h +++ b/test/util.h @@ -27,5 +27,7 @@ enum {BUFFER_SIZE = 256}; +void SPrintf(char *buffer, const char *format, ...); + // Increment a number in a string. void Increment(char *s);