Make tests more portable.

This commit is contained in:
Victor Zverovich 2012-12-13 15:10:02 -08:00
parent e4ada5bc7f
commit 6e1d142a56

View File

@ -73,6 +73,32 @@ using fmt::FormatError;
FORMAT_TEST_THROW_(statement, expected_exception, expected_message, \ FORMAT_TEST_THROW_(statement, expected_exception, expected_message, \
GTEST_NONFATAL_FAILURE_) GTEST_NONFATAL_FAILURE_)
// Increment a number in a string.
void Increment(char *s) {
for (int i = static_cast<int>(std::strlen(s)) - 1; i >= 0; --i) {
if (s[i] != '9') {
++s[i];
break;
}
s[i] = '0';
}
}
TEST(UtilTest, Increment) {
char s[10] = "123";
Increment(s);
EXPECT_STREQ("124", s);
s[2] = '8';
Increment(s);
EXPECT_STREQ("129", s);
Increment(s);
EXPECT_STREQ("130", s);
EXPECT_STREQ("130", s);
s[1] = s[2] = '9';
Increment(s);
EXPECT_STREQ("200", s);
}
class TestString { class TestString {
private: private:
std::string value_; std::string value_;
@ -205,25 +231,21 @@ TEST(FormatterTest, ArgErrors) {
EXPECT_THROW_MSG(Format("{0"), FormatError, "unmatched '{' in format"); EXPECT_THROW_MSG(Format("{0"), FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(Format("{0}"), FormatError, EXPECT_THROW_MSG(Format("{0}"), FormatError,
"argument index is out of range in format"); "argument index is out of range in format");
char format[256]; char format[256];
std::sprintf(format, "{%u", UINT_MAX); std::sprintf(format, "{%u", UINT_MAX);
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format"); EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
std::sprintf(format, "{%u}", UINT_MAX); std::sprintf(format, "{%u}", UINT_MAX);
EXPECT_THROW_MSG(Format(format), FormatError, EXPECT_THROW_MSG(Format(format), FormatError,
"argument index is out of range in format"); "argument index is out of range in format");
if (ULONG_MAX > UINT_MAX) {
std::sprintf(format, "{%lu", UINT_MAX + 1l); std::sprintf(format, "{%u", UINT_MAX);
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format"); Increment(format + 1);
std::sprintf(format, "{%lu}", UINT_MAX + 1l); EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
EXPECT_THROW_MSG(Format(format), std::size_t size = std::strlen(format);
FormatError, "number is too big in format"); format[size] = '}';
} else { format[size + 1] = 0;
std::sprintf(format, "{%u0", UINT_MAX); EXPECT_THROW_MSG(Format(format), FormatError, "number is too big in format");
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
std::sprintf(format, "{%u0}", UINT_MAX);
EXPECT_THROW_MSG(Format(format),
FormatError, "number is too big in format");
}
} }
TEST(FormatterTest, EmptySpecs) { TEST(FormatterTest, EmptySpecs) {
@ -275,19 +297,15 @@ TEST(FormatterTest, ZeroFlag) {
TEST(FormatterTest, Width) { TEST(FormatterTest, Width) {
char format[256]; char format[256];
if (ULONG_MAX > UINT_MAX) { std::sprintf(format, "{0:%u", UINT_MAX);
std::sprintf(format, "{0:%lu", INT_MAX + 1l); Increment(format + 3);
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format"); EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:%lu}", UINT_MAX + 1l); std::size_t size = std::strlen(format);
EXPECT_THROW_MSG(Format(format) << 0, format[size] = '}';
FormatError, "number is too big in format"); format[size + 1] = 0;
} else { EXPECT_THROW_MSG(Format(format) << 0,
std::sprintf(format, "{0:%u0", UINT_MAX); FormatError, "number is too big in format");
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:%u0}", UINT_MAX);
EXPECT_THROW_MSG(Format(format) << 0,
FormatError, "number is too big in format");
}
std::sprintf(format, "{0:%u", INT_MAX + 1u); std::sprintf(format, "{0:%u", INT_MAX + 1u);
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format"); EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:%u}", INT_MAX + 1u); std::sprintf(format, "{0:%u}", INT_MAX + 1u);
@ -308,19 +326,14 @@ TEST(FormatterTest, Width) {
TEST(FormatterTest, Precision) { TEST(FormatterTest, Precision) {
char format[256]; char format[256];
if (ULONG_MAX > UINT_MAX) { std::sprintf(format, "{0:.%u", UINT_MAX);
std::sprintf(format, "{0:.%lu", INT_MAX + 1l); Increment(format + 4);
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format"); EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:.%lu}", UINT_MAX + 1l); std::size_t size = std::strlen(format);
EXPECT_THROW_MSG(Format(format) << 0, format[size] = '}';
FormatError, "number is too big in format"); format[size + 1] = 0;
} else { EXPECT_THROW_MSG(Format(format) << 0,
std::sprintf(format, "{0:.%u0", UINT_MAX); FormatError, "number is too big in format");
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:.%u0}", UINT_MAX);
EXPECT_THROW_MSG(Format(format) << 0,
FormatError, "number is too big in format");
}
std::sprintf(format, "{0:.%u", INT_MAX + 1u); std::sprintf(format, "{0:.%u", INT_MAX + 1u);
EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format"); EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
@ -377,27 +390,17 @@ TEST(FormatterTest, Precision) {
TEST(FormatterTest, RuntimePrecision) { TEST(FormatterTest, RuntimePrecision) {
char format[256]; char format[256];
if (ULONG_MAX > UINT_MAX) { std::sprintf(format, "{0:.{%u", UINT_MAX);
std::sprintf(format, "{0:.{%lu", INT_MAX + 1l); Increment(format + 4);
EXPECT_THROW_MSG(Format(format) << 0, EXPECT_THROW_MSG(Format(format), FormatError, "unmatched '{' in format");
FormatError, "unmatched '{' in format"); std::size_t size = std::strlen(format);
std::sprintf(format, "{0:.{%lu}", INT_MAX + 1l); format[size] = '}';
EXPECT_THROW_MSG(Format(format) << 0, format[size + 1] = 0;
FormatError, "unmatched '{' in format"); EXPECT_THROW_MSG(Format(format) << 0, FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:.{%lu}}", UINT_MAX + 1l); format[size + 1] = '}';
EXPECT_THROW_MSG(Format(format) << 0, format[size + 2] = 0;
FormatError, "number is too big in format"); EXPECT_THROW_MSG(Format(format) << 0,
} else { FormatError, "number is too big in format");
std::sprintf(format, "{0:.{%u0", UINT_MAX);
EXPECT_THROW_MSG(Format(format) << 0,
FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:.{%u0}", UINT_MAX);
EXPECT_THROW_MSG(Format(format) << 0,
FormatError, "unmatched '{' in format");
std::sprintf(format, "{0:.{%u0}}", UINT_MAX);
EXPECT_THROW_MSG(Format(format) << 0,
FormatError, "number is too big in format");
}
EXPECT_THROW_MSG(Format("{0:.{") << 0, EXPECT_THROW_MSG(Format("{0:.{") << 0,
FormatError, "unmatched '{' in format"); FormatError, "unmatched '{' in format");