Merge bitcoin/bitcoin#30933: test: Prove+document ConstevalFormatString/tinyformat parity
Some checks are pending
CI / test each commit (push) Waiting to run
CI / macOS 14 native, arm64, no depends, sqlite only, gui (push) Waiting to run
CI / macOS 14 native, arm64, fuzz (push) Waiting to run
CI / Win64 native, VS 2022 (push) Waiting to run
CI / Win64 native fuzz, VS 2022 (push) Waiting to run
CI / ASan + LSan + UBSan + integer, no depends, USDT (push) Waiting to run

c93bf0e6e2 test: Add missing %c character test (Hodlinator)
76cca4aa6f test: Document non-parity between tinyformat and ConstevalFormatstring (Hodlinator)
533013cba2 test: Prove+document ConstevalFormatString/tinyformat parity (Hodlinator)
b81a465995 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 https://github.com/bitcoin/bitcoin/pull/30546#discussion_r1755013263 and https://github.com/bitcoin/bitcoin/pull/30546#discussion_r1756495304.

ACKs for top commit:
  maflcko:
    re-ACK c93bf0e6e2 🗜
  l0rinc:
    ACK c93bf0e6e2
  ryanofsky:
    Code review ACK c93bf0e6e2. Just a few cleanups tweaking function declarations and commit comments and consolidating some test cases since last review.

Tree-SHA512: 5ecc893b26cf2761c0009861be392ec4c4fceb0ef95052a2f6f9df76b2e459cfb3f9e257f61be07c3bb2ecc6e525e72c5ca853be1f63b70b52785323d3db6b42
This commit is contained in:
Ryan Ofsky 2024-12-10 21:57:38 -05:00
commit 676936845b
No known key found for this signature in database
GPG key ID: 46800E30FC748A66
2 changed files with 40 additions and 4 deletions

View file

@ -8,21 +8,36 @@
#include <test/util/setup_common.h>
using namespace util;
using util::detail::CheckNumFormatSpecifiers;
BOOST_AUTO_TEST_SUITE(util_string_tests)
template <unsigned NumArgs>
void TfmFormatZeroes(const std::string& fmt)
{
std::apply([&](auto... args) {
(void)tfm::format(fmt, args...);
}, std::array<int, NumArgs>{});
}
// Helper to allow compile-time sanity checks while providing the number of
// args directly. Normally PassFmt<sizeof...(Args)> would be used.
template <unsigned NumArgs>
inline void PassFmt(util::ConstevalFormatString<NumArgs> fmt)
void PassFmt(ConstevalFormatString<NumArgs> fmt)
{
// Execute compile-time check again at run-time to get code coverage stats
util::detail::CheckNumFormatSpecifiers<NumArgs>(fmt.fmt);
BOOST_CHECK_NO_THROW(CheckNumFormatSpecifiers<NumArgs>(fmt.fmt));
// If ConstevalFormatString didn't throw above, make sure tinyformat doesn't
// throw either for the same format string and parameter count combination.
// Proves that we have some extent of protection from runtime errors
// (tinyformat may still throw for some type mismatches).
BOOST_CHECK_NO_THROW(TfmFormatZeroes<NumArgs>(fmt.fmt));
}
template <unsigned WrongNumArgs>
inline void FailFmtWithError(const char* wrong_fmt, std::string_view error)
void FailFmtWithError(const char* wrong_fmt, std::string_view error)
{
BOOST_CHECK_EXCEPTION(util::detail::CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt), const char*, HasReason{error});
BOOST_CHECK_EXCEPTION(CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt), const char*, HasReason{error});
}
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
@ -30,6 +45,7 @@ BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
PassFmt<0>("");
PassFmt<0>("%%");
PassFmt<1>("%s");
PassFmt<1>("%c");
PassFmt<0>("%%s");
PassFmt<0>("s%%");
PassFmt<1>("%%%s");
@ -110,6 +126,24 @@ BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
FailFmtWithError<2>("%1$*2$", err_term);
FailFmtWithError<2>("%1$.*2$", err_term);
FailFmtWithError<2>("%1$9.*2$", err_term);
// Non-parity between tinyformat and ConstevalFormatString.
// tinyformat throws but ConstevalFormatString does not.
BOOST_CHECK_EXCEPTION(tfm::format(ConstevalFormatString<1>{"%n"}, 0), tfm::format_error,
HasReason{"tinyformat: %n conversion spec not supported"});
BOOST_CHECK_EXCEPTION(tfm::format(ConstevalFormatString<2>{"%*s"}, "hi", "hi"), tfm::format_error,
HasReason{"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
BOOST_CHECK_EXCEPTION(tfm::format(ConstevalFormatString<2>{"%.*s"}, "hi", "hi"), tfm::format_error,
HasReason{"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
// Ensure that tinyformat throws if format string contains wrong number
// of specifiers. PassFmt relies on this to verify tinyformat successfully
// formats the strings, and will need to be updated if tinyformat is changed
// not to throw on failure.
BOOST_CHECK_EXCEPTION(TfmFormatZeroes<2>("%s"), tfm::format_error,
HasReason{"tinyformat: Not enough conversion specifiers in format string"});
BOOST_CHECK_EXCEPTION(TfmFormatZeroes<1>("%s %s"), tfm::format_error,
HasReason{"tinyformat: Too many conversion specifiers in format string"});
}
BOOST_AUTO_TEST_SUITE_END()

View file

@ -15,6 +15,8 @@ import sys
FALSE_POSITIVES = [
("src/clientversion.cpp", "strprintf(_(COPYRIGHT_HOLDERS), COPYRIGHT_HOLDERS_SUBSTITUTION)"),
("src/test/translation_tests.cpp", "strprintf(format, arg)"),
("src/test/util_string_tests.cpp", 'tfm::format(ConstevalFormatString<2>{"%*s"}, "hi", "hi")'),
("src/test/util_string_tests.cpp", 'tfm::format(ConstevalFormatString<2>{"%.*s"}, "hi", "hi")'),
]