mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-31 03:07:36 +02:00
Add more tests. Check if argument index is in range.
This commit is contained in:
21
format.cc
21
format.cc
@ -11,6 +11,13 @@
|
|||||||
|
|
||||||
using std::size_t;
|
using std::size_t;
|
||||||
|
|
||||||
|
static void CheckClosingBrace(const char *s) {
|
||||||
|
while (*s && *s != '}')
|
||||||
|
++s;
|
||||||
|
if (!*s)
|
||||||
|
throw fmt::FormatError("unmatched '{' in format");
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void fmt::Formatter::FormatArg(
|
void fmt::Formatter::FormatArg(
|
||||||
const char *format, const T &arg, int width, int precision) {
|
const char *format, const T &arg, int width, int precision) {
|
||||||
@ -40,21 +47,25 @@ void fmt::Formatter::Format() {
|
|||||||
buffer_.reserve(500);
|
buffer_.reserve(500);
|
||||||
const char *start = format_;
|
const char *start = format_;
|
||||||
const char *s = start;
|
const char *s = start;
|
||||||
for (; *s; ++s) {
|
while (*s) {
|
||||||
if (*s != '{') continue;
|
if (*s++ != '{') continue;
|
||||||
buffer_.insert(buffer_.end(), start, s);
|
buffer_.insert(buffer_.end(), start, s - 1);
|
||||||
++s;
|
|
||||||
|
|
||||||
// Parse argument index.
|
// Parse argument index.
|
||||||
unsigned arg_index = 0;
|
unsigned arg_index = 0;
|
||||||
if ('0' <= *s && *s <= '9') {
|
if ('0' <= *s && *s <= '9') {
|
||||||
|
// TODO: check overflow
|
||||||
do {
|
do {
|
||||||
arg_index = arg_index * 10 + (*s++ - '0');
|
arg_index = arg_index * 10 + (*s++ - '0');
|
||||||
} while ('0' <= *s && *s <= '9');
|
} while ('0' <= *s && *s <= '9');
|
||||||
} else {
|
} else {
|
||||||
|
CheckClosingBrace(s);
|
||||||
throw FormatError("missing argument index in format string");
|
throw FormatError("missing argument index in format string");
|
||||||
}
|
}
|
||||||
// TODO: check if argument index is correct
|
if (arg_index >= args_.size()) {
|
||||||
|
CheckClosingBrace(s);
|
||||||
|
throw std::out_of_range("argument index is out of range in format");
|
||||||
|
}
|
||||||
|
|
||||||
char arg_format[8]; // longest format: %+0*.*ld
|
char arg_format[8]; // longest format: %+0*.*ld
|
||||||
char *arg_format_ptr = arg_format;
|
char *arg_format_ptr = arg_format;
|
||||||
|
5
format.h
5
format.h
@ -28,7 +28,7 @@ class ArgFormatterWithCallback;
|
|||||||
// fit all the output.
|
// fit all the output.
|
||||||
class Formatter {
|
class Formatter {
|
||||||
private:
|
private:
|
||||||
std::vector<char> buffer_;
|
std::vector<char> buffer_; // Output buffer.
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
CHAR, INT, UINT, LONG, ULONG, DOUBLE, LONG_DOUBLE, STRING, WSTRING, POINTER
|
CHAR, INT, UINT, LONG, ULONG, DOUBLE, LONG_DOUBLE, STRING, WSTRING, POINTER
|
||||||
@ -63,8 +63,7 @@ class Formatter {
|
|||||||
|
|
||||||
std::vector<Arg> args_;
|
std::vector<Arg> args_;
|
||||||
|
|
||||||
// Pointer to the part of the format string that hasn't been written yet.
|
const char *format_; // Format string.
|
||||||
const char *format_;
|
|
||||||
|
|
||||||
friend class ArgFormatter;
|
friend class ArgFormatter;
|
||||||
|
|
||||||
|
@ -14,29 +14,81 @@ using std::sprintf;
|
|||||||
|
|
||||||
using fmt::Formatter;
|
using fmt::Formatter;
|
||||||
using fmt::Format;
|
using fmt::Format;
|
||||||
|
using fmt::FormatError;
|
||||||
|
|
||||||
|
#define FORMAT_TEST_THROW_(statement, expected_exception, message, fail) \
|
||||||
|
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||||
|
if (::testing::internal::ConstCharPtr gtest_msg = "") { \
|
||||||
|
bool gtest_caught_expected = false; \
|
||||||
|
std::string actual_message; \
|
||||||
|
try { \
|
||||||
|
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
|
||||||
|
} \
|
||||||
|
catch (expected_exception const& e) { \
|
||||||
|
gtest_caught_expected = true; \
|
||||||
|
actual_message = e.what(); \
|
||||||
|
} \
|
||||||
|
catch (...) { \
|
||||||
|
gtest_msg.value = \
|
||||||
|
"Expected: " #statement " throws an exception of type " \
|
||||||
|
#expected_exception ".\n Actual: it throws a different type."; \
|
||||||
|
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||||
|
} \
|
||||||
|
if (!gtest_caught_expected) { \
|
||||||
|
gtest_msg.value = \
|
||||||
|
"Expected: " #statement " throws an exception of type " \
|
||||||
|
#expected_exception ".\n Actual: it throws nothing."; \
|
||||||
|
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||||
|
} else if (actual_message != message) {\
|
||||||
|
gtest_msg.value = \
|
||||||
|
"Expected: " #statement " throws an exception of type " \
|
||||||
|
#expected_exception " with message \"" message "\"."; \
|
||||||
|
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||||
|
} \
|
||||||
|
} else \
|
||||||
|
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
|
||||||
|
fail(gtest_msg.value)
|
||||||
|
|
||||||
|
#define EXPECT_THROW_MSG(statement, expected_exception, expected_message) \
|
||||||
|
FORMAT_TEST_THROW_(statement, expected_exception, expected_message, \
|
||||||
|
GTEST_NONFATAL_FAILURE_)
|
||||||
|
|
||||||
TEST(FormatterTest, FormatNoArgs) {
|
TEST(FormatterTest, FormatNoArgs) {
|
||||||
Formatter format;
|
EXPECT_EQ("abracadabra", str(Format("{0}{1}{0}") << "abra" << "cad"));
|
||||||
format("test");
|
EXPECT_EQ("test", str(Format("test")));
|
||||||
EXPECT_STREQ("test", format.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, FormatComplex) {
|
TEST(FormatterTest, FormatArgs) {
|
||||||
EXPECT_STREQ("1.2340000000:0042:+3.13:str:0x3e8:X:%",
|
EXPECT_EQ("42", str(Format("{0}") << 42));
|
||||||
c_str(Format("{0:0.10f}:{1:04}:{2:+g}:{3}:{4}:{5}:%")
|
EXPECT_EQ("before 42", str(Format("before {0}") << 42));
|
||||||
<< 1.234 << 42 << 3.13 << "str" << reinterpret_cast<void*>(1000)
|
EXPECT_EQ("42 after", str(Format("{0} after") << 42));
|
||||||
<< 'X'));
|
EXPECT_EQ("before 42 after", str(Format("before {0} after") << 42));
|
||||||
printf("%0.*f:%04d:%+g:%s:%p:%c:%%\n",
|
EXPECT_EQ("answer = 42", str(Format("{0} = {1}") << "answer" << 42));
|
||||||
10, 1.234, 42, 3.13, "str", (void*)1000, (int)'X');
|
EXPECT_EQ("42 is the answer", str(Format("{1} is the {0}") << "answer" << 42));
|
||||||
printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
|
EXPECT_EQ("abracadabra", str(Format("{0}{1}{0}") << "abra" << "cad"));
|
||||||
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
|
}
|
||||||
|
|
||||||
|
TEST(FormatterTest, InvalidFormat) {
|
||||||
|
//Format("{");
|
||||||
|
EXPECT_THROW_MSG(Format("{}"), FormatError,
|
||||||
|
"missing argument index in format string");
|
||||||
|
EXPECT_THROW_MSG(Format("{"), FormatError, "unmatched '{' in format");
|
||||||
|
EXPECT_THROW_MSG(Format("{0"), FormatError, "unmatched '{' in format");
|
||||||
|
EXPECT_THROW_MSG(Format("{0}"), std::out_of_range,
|
||||||
|
"argument index is out of range in format");
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(FormatterTest, FormatBig) {
|
||||||
|
EXPECT_EQ("1.2340000000:0042:+3.13:str:0x3e8:X:%",
|
||||||
|
str(Format("{0:0.10f}:{1:04}:{2:+g}:{3}:{4}:{5}:%")
|
||||||
|
<< 1.234 << 42 << 3.13 << "str"
|
||||||
|
<< reinterpret_cast<void*>(1000) << 'X'));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, FormatInt) {
|
TEST(FormatterTest, FormatInt) {
|
||||||
EXPECT_STREQ("42", c_str(Format("{0}") << 42));
|
EXPECT_EQ("42", str(Format("{0}") << 42));
|
||||||
EXPECT_STREQ("before 42 after", c_str(Format("before {0} after") << 42));
|
EXPECT_EQ("before 42 after", str(Format("before {0} after") << 42));
|
||||||
printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
|
|
||||||
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
Reference in New Issue
Block a user