From 9fbdc32adc002d2ac175153a1ae8c75e24ac101d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 2 Jul 2014 08:38:31 -0700 Subject: [PATCH] Test MakeArg and handle bool, signed char and unsigned char. --- format.h | 8 ++-- test/util-test.cc | 98 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/format.h b/format.h index f7120cff..fdff32e2 100644 --- a/format.h +++ b/format.h @@ -351,7 +351,7 @@ class CharTraits; template <> class CharTraits : public BasicCharTraits { private: - // Conversion from wchar_t to char is not supported. + // Conversion from wchar_t to char is not allowed. static char ConvertChar(wchar_t); public: @@ -596,9 +596,9 @@ struct Arg { union { int int_value; unsigned uint_value; - double double_value; LongLong long_long_value; ULongLong ulong_long_value; + double double_value; long double long_double_value; const void *pointer_value; StringValue string; @@ -623,7 +623,7 @@ class MakeArg : public Arg { public: MakeArg() {} - // TODO: unsigned char & signed char + MakeArg(bool value) { type = INT; int_value = value; } MakeArg(short value) { type = INT; int_value = value; } MakeArg(unsigned short value) { type = UINT; uint_value = value; } MakeArg(int value) { type = INT; int_value = value; } @@ -651,6 +651,8 @@ class MakeArg : public Arg { MakeArg(float value) { type = DOUBLE; double_value = value; } MakeArg(double value) { type = DOUBLE; double_value = value; } MakeArg(long double value) { type = LONG_DOUBLE; long_double_value = value; } + MakeArg(signed char value) { type = CHAR; int_value = value; } + MakeArg(unsigned char value) { type = CHAR; int_value = value; } MakeArg(char value) { type = CHAR; int_value = value; } MakeArg(wchar_t value) { type = CHAR; diff --git a/test/util-test.cc b/test/util-test.cc index 52a3b04b..3fc8748d 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -25,6 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include +#include #include #include #include "gtest-extra.h" @@ -70,6 +72,102 @@ TEST(UtilTest, Increment) { EXPECT_STREQ("200", s); } +#define EXPECT_ARG_(Char, type_code, Type, field, value) { \ + Type expected_value = value; \ + fmt::internal::Arg arg = \ + fmt::internal::MakeArg(expected_value); \ + EXPECT_EQ(fmt::internal::Arg::type_code, arg.type); \ + EXPECT_EQ(expected_value, arg.field); \ +} + +#define EXPECT_ARG(type_code, Type, field, value) \ + EXPECT_ARG_(char, type_code, Type, field, value) + +#define EXPECT_ARGW(type_code, Type, field, value) \ + EXPECT_ARG_(wchar_t, type_code, Type, field, value) + +TEST(UtilTest, MakeArg) { + // Test bool. + EXPECT_ARG(INT, bool, int_value, true); + + // Test char. + EXPECT_ARG(CHAR, signed char, int_value, 42); + EXPECT_ARG(CHAR, signed char, int_value, SCHAR_MIN); + EXPECT_ARG(CHAR, signed char, int_value, SCHAR_MAX); + EXPECT_ARG(CHAR, unsigned char, int_value, 42); + EXPECT_ARG(CHAR, unsigned char, int_value, UCHAR_MAX ); + EXPECT_ARG(CHAR, char, int_value, 'a'); + EXPECT_ARG(CHAR, char, int_value, CHAR_MIN); + EXPECT_ARG(CHAR, char, int_value, CHAR_MAX); + + // Test wchar_t. + EXPECT_ARGW(CHAR, wchar_t, int_value, 42); + EXPECT_ARGW(CHAR, wchar_t, int_value, WCHAR_MIN); + EXPECT_ARGW(CHAR, wchar_t, int_value, WCHAR_MAX); + + // Test short. + EXPECT_ARG(INT, short, int_value, 42); + EXPECT_ARG(INT, short, int_value, SHRT_MIN); + EXPECT_ARG(INT, short, int_value, SHRT_MAX); + EXPECT_ARG(UINT, unsigned short, uint_value, 42); + EXPECT_ARG(UINT, unsigned short, uint_value, USHRT_MAX); + + // Test int. + EXPECT_ARG(INT, int, int_value, 42); + EXPECT_ARG(INT, int, int_value, INT_MIN); + EXPECT_ARG(INT, int, int_value, INT_MAX); + EXPECT_ARG(UINT, unsigned, uint_value, 42); + EXPECT_ARG(UINT, unsigned, uint_value, UINT_MAX); + + // Test long. +#if LONG_MAX == INT_MAX +# define LONG INT +# define ULONG UINT +# define long_value int_value +# define ulong_value iint_value +#else +# define LONG LONG_LONG +# define ULONG ULONG_LONG +# define long_value long_long_value +# define ulong_value ulong_long_value +#endif + EXPECT_ARG(LONG, long, long_value, 42); + EXPECT_ARG(LONG, long, long_value, LONG_MIN); + EXPECT_ARG(LONG, long, long_value, LONG_MAX); + EXPECT_ARG(ULONG, unsigned long, ulong_value, 42); + EXPECT_ARG(ULONG, unsigned long, ulong_value, ULONG_MAX); + + // Test long long. + EXPECT_ARG(LONG_LONG, fmt::LongLong, long_long_value, 42); + EXPECT_ARG(LONG_LONG, fmt::LongLong, long_long_value, LLONG_MIN); + EXPECT_ARG(LONG_LONG, fmt::LongLong, long_long_value, LLONG_MAX); + EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ulong_long_value, 42); + EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ulong_long_value, ULLONG_MAX); + + // Test float. + EXPECT_ARG(DOUBLE, float, double_value, 4.2); + EXPECT_ARG(DOUBLE, float, double_value, FLT_MIN); + EXPECT_ARG(DOUBLE, float, double_value, FLT_MAX); + + // Test double. + EXPECT_ARG(DOUBLE, double, double_value, 4.2); + EXPECT_ARG(DOUBLE, double, double_value, DBL_MIN); + EXPECT_ARG(DOUBLE, double, double_value, DBL_MAX); + + // Test long double. + EXPECT_ARG(LONG_DOUBLE, long double, long_double_value, 4.2); + EXPECT_ARG(LONG_DOUBLE, long double, long_double_value, LDBL_MIN); + EXPECT_ARG(LONG_DOUBLE, long double, long_double_value, LDBL_MAX); + + // Test string. + char STR[] = "test"; + EXPECT_ARG(STRING, char*, string.value, STR); + EXPECT_ARG(STRING, const char*, string.value, STR); + //EXPECT_ARG(STRING, volatile char*, string.value, STR); + + // TODO: test pointers +} + // Tests fmt::internal::CountDigits for integer type Int. template void TestCountDigits(Int) {