Skip to content

Commit 6769368

Browse files
committed
Merge bitcoin/bitcoin#30933: test: Prove+document ConstevalFormatString/tinyformat parity
c93bf0e test: Add missing %c character test (Hodlinator) 76cca4a test: Document non-parity between tinyformat and ConstevalFormatstring (Hodlinator) 533013c test: Prove+document ConstevalFormatString/tinyformat parity (Hodlinator) b81a465 refactor test: Profit from using namespace + using detail function (Hodlinator) Pull request description: Clarifies and puts the extent of parity under test. Broken out from #30546 based on bitcoin/bitcoin#30546 (comment) and bitcoin/bitcoin#30546 (comment). ACKs for top commit: maflcko: re-ACK c93bf0e 🗜 l0rinc: ACK c93bf0e ryanofsky: Code review ACK c93bf0e. Just a few cleanups tweaking function declarations and commit comments and consolidating some test cases since last review. Tree-SHA512: 5ecc893b26cf2761c0009861be392ec4c4fceb0ef95052a2f6f9df76b2e459cfb3f9e257f61be07c3bb2ecc6e525e72c5ca853be1f63b70b52785323d3db6b42
2 parents 8ad2c90 + c93bf0e commit 6769368

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/test/util_string_tests.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,44 @@
88
#include <test/util/setup_common.h>
99

1010
using namespace util;
11+
using util::detail::CheckNumFormatSpecifiers;
1112

1213
BOOST_AUTO_TEST_SUITE(util_string_tests)
1314

15+
template <unsigned NumArgs>
16+
void TfmFormatZeroes(const std::string& fmt)
17+
{
18+
std::apply([&](auto... args) {
19+
(void)tfm::format(fmt, args...);
20+
}, std::array<int, NumArgs>{});
21+
}
22+
1423
// Helper to allow compile-time sanity checks while providing the number of
1524
// args directly. Normally PassFmt<sizeof...(Args)> would be used.
1625
template <unsigned NumArgs>
17-
inline void PassFmt(util::ConstevalFormatString<NumArgs> fmt)
26+
void PassFmt(ConstevalFormatString<NumArgs> fmt)
1827
{
1928
// Execute compile-time check again at run-time to get code coverage stats
20-
util::detail::CheckNumFormatSpecifiers<NumArgs>(fmt.fmt);
29+
BOOST_CHECK_NO_THROW(CheckNumFormatSpecifiers<NumArgs>(fmt.fmt));
30+
31+
// If ConstevalFormatString didn't throw above, make sure tinyformat doesn't
32+
// throw either for the same format string and parameter count combination.
33+
// Proves that we have some extent of protection from runtime errors
34+
// (tinyformat may still throw for some type mismatches).
35+
BOOST_CHECK_NO_THROW(TfmFormatZeroes<NumArgs>(fmt.fmt));
2136
}
2237
template <unsigned WrongNumArgs>
23-
inline void FailFmtWithError(const char* wrong_fmt, std::string_view error)
38+
void FailFmtWithError(const char* wrong_fmt, std::string_view error)
2439
{
25-
BOOST_CHECK_EXCEPTION(util::detail::CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt), const char*, HasReason{error});
40+
BOOST_CHECK_EXCEPTION(CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt), const char*, HasReason{error});
2641
}
2742

2843
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
2944
{
3045
PassFmt<0>("");
3146
PassFmt<0>("%%");
3247
PassFmt<1>("%s");
48+
PassFmt<1>("%c");
3349
PassFmt<0>("%%s");
3450
PassFmt<0>("s%%");
3551
PassFmt<1>("%%%s");
@@ -110,6 +126,24 @@ BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
110126
FailFmtWithError<2>("%1$*2$", err_term);
111127
FailFmtWithError<2>("%1$.*2$", err_term);
112128
FailFmtWithError<2>("%1$9.*2$", err_term);
129+
130+
// Non-parity between tinyformat and ConstevalFormatString.
131+
// tinyformat throws but ConstevalFormatString does not.
132+
BOOST_CHECK_EXCEPTION(tfm::format(ConstevalFormatString<1>{"%n"}, 0), tfm::format_error,
133+
HasReason{"tinyformat: %n conversion spec not supported"});
134+
BOOST_CHECK_EXCEPTION(tfm::format(ConstevalFormatString<2>{"%*s"}, "hi", "hi"), tfm::format_error,
135+
HasReason{"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
136+
BOOST_CHECK_EXCEPTION(tfm::format(ConstevalFormatString<2>{"%.*s"}, "hi", "hi"), tfm::format_error,
137+
HasReason{"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
138+
139+
// Ensure that tinyformat throws if format string contains wrong number
140+
// of specifiers. PassFmt relies on this to verify tinyformat successfully
141+
// formats the strings, and will need to be updated if tinyformat is changed
142+
// not to throw on failure.
143+
BOOST_CHECK_EXCEPTION(TfmFormatZeroes<2>("%s"), tfm::format_error,
144+
HasReason{"tinyformat: Not enough conversion specifiers in format string"});
145+
BOOST_CHECK_EXCEPTION(TfmFormatZeroes<1>("%s %s"), tfm::format_error,
146+
HasReason{"tinyformat: Too many conversion specifiers in format string"});
113147
}
114148

115149
BOOST_AUTO_TEST_SUITE_END()

test/lint/run-lint-format-strings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
FALSE_POSITIVES = [
1616
("src/clientversion.cpp", "strprintf(_(COPYRIGHT_HOLDERS), COPYRIGHT_HOLDERS_SUBSTITUTION)"),
1717
("src/test/translation_tests.cpp", "strprintf(format, arg)"),
18+
("src/test/util_string_tests.cpp", 'tfm::format(ConstevalFormatString<2>{"%*s"}, "hi", "hi")'),
19+
("src/test/util_string_tests.cpp", 'tfm::format(ConstevalFormatString<2>{"%.*s"}, "hi", "hi")'),
1820
]
1921

2022

0 commit comments

Comments
 (0)