Test printf length.

This commit is contained in:
Victor Zverovich 2014-08-05 07:52:59 -07:00
parent fc10d10b6b
commit b797d39dd1

View File

@ -282,13 +282,26 @@ bool IsSupported(const std::string &format) {
#endif
}
template <typename T, typename U>
std::string sprintf_int(std::string format, U value) {
char buffer[BUFFER_SIZE];
char type = format[format.size() - 1];
if (type == 'd' || type == 'i') {
safe_sprintf(buffer, format.c_str(), static_cast<T>(value));
} else {
typedef typename fmt::internal::MakeUnsigned<T>::Type Unsigned;
safe_sprintf(buffer, format.c_str(), static_cast<Unsigned>(value));
}
return buffer;
}
#define EXPECT_STD_PRINTF(format, T, arg) { \
char buffer[BUFFER_SIZE]; \
if (IsSupported(format)) \
if (IsSupported(format)) { \
safe_sprintf(buffer, fmt::StringRef(format).c_str(), arg); \
else \
safe_sprintf(buffer, fmt::StringRef(format).c_str(), static_cast<T>(arg)); \
EXPECT_PRINTF(buffer, format, arg); \
} \
EXPECT_PRINTF(sprintf_int<T>(format, arg), format, arg); \
}
template <typename T>
@ -316,12 +329,14 @@ void TestLength(const char *length_spec) {
}
TEST(PrintfTest, Length) {
EXPECT_EQ("-128", sprintf_int<signed char>("%hhd", SCHAR_MAX + 1));
EXPECT_EQ("128", sprintf_int<signed char>("%hhu", SCHAR_MAX + 1));
//TestLength<signed char>("hh");
//TestLength<unsigned char>("hh");
TestLength<short>("h");
TestLength<unsigned short>("h");
TestLength<long>("l");
TestLength<unsigned long>("l");
//TestLength<short>("h");
//TestLength<unsigned short>("h");
//TestLength<long>("l");
//TestLength<unsigned long>("l");
// TODO: more tests
}