mirror of
https://github.com/fmtlib/fmt.git
synced 2026-08-03 20:14:20 +02:00
add support for positional arguments as width and precision specifiers (#4643)
* make positional arguments work as width and precision specifiers with floating-point formats too * fix test case failing ci tests
This commit is contained in:
@@ -296,6 +296,35 @@ TEST(printf_test, dynamic_precision) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(printf_test, positional_width) {
|
||||
EXPECT_EQ(" 42", test_sprintf("%2$*1$d", 5, 42));
|
||||
EXPECT_EQ("42 ", test_sprintf("%2$*1$d", -5, 42));
|
||||
EXPECT_EQ(" abc", test_sprintf("%2$*1$s", 5, "abc"));
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$*1$d", 5.0, 42), format_error,
|
||||
"width is not integer");
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$*1$d"), format_error, "argument not found");
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$*1$d", big_num, 42), format_error,
|
||||
"number is too big");
|
||||
}
|
||||
|
||||
TEST(printf_test, positional_precision) {
|
||||
EXPECT_EQ("00042", test_sprintf("%2$.*1$d", 5, 42));
|
||||
EXPECT_EQ("42", test_sprintf("%2$.*1$d", -5, 42));
|
||||
EXPECT_EQ("Hell", test_sprintf("%2$.*1$s", 4, "Hello"));
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d", 5.0, 42), format_error,
|
||||
"precision is not integer");
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d"), format_error, "argument not found");
|
||||
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d", big_num, 42), format_error,
|
||||
"number is too big");
|
||||
}
|
||||
|
||||
TEST(printf_test, positional_width_and_precision) {
|
||||
EXPECT_EQ(" 00042", test_sprintf("%3$*1$.*2$d", 7, 5, 42));
|
||||
EXPECT_EQ(" ab", test_sprintf("%3$*1$.*2$s", 7, 2, "abcdef"));
|
||||
EXPECT_EQ(" 00042", test_sprintf("%3$*1$.*2$x", 7, 5, 0x42));
|
||||
EXPECT_EQ("100.4400000", test_sprintf("%6$-*5$.*4$f%3$s%2$s%1$s", "", "", "", 7, 4, 100.44));
|
||||
}
|
||||
|
||||
template <typename T> struct make_signed {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user