9#include <boost/test/unit_test.hpp>
15BOOST_AUTO_TEST_SUITE(util_string_tests)
17template <
unsigned NumArgs>
20 std::apply([&](
auto...
args) {
22 }, std::array<int, NumArgs>{});
27template <
unsigned NumArgs>
39template <
unsigned WrongNumArgs>
42 BOOST_CHECK_EXCEPTION(CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt),
const char*,
HasReason{error});
47 auto span = std::as_bytes(std::span(str));
48 return {span.begin(), span.end()};
63 PassFmt<1>(
"%1$s%1$s");
65 PassFmt<2>(
"%2$s 4$s %2$s");
66 PassFmt<129>(
"%129$s 999$s %2$s");
87 PassFmt<3>(
"%2$*3$d");
88 PassFmt<3>(
"%2$*3$.9d");
89 PassFmt<3>(
"%2$.*3$d");
90 PassFmt<3>(
"%2$9.*3$d");
91 PassFmt<3>(
"%2$+9.*3$d");
92 PassFmt<4>(
"%3$*2$.*4$f");
95 PassFmt<3>(
"'%- 0+*.*f'");
96 PassFmt<3>(
"'%1$- 0+*3$.*2$f'");
98 auto err_mix{
"Format specifiers must be all positional or all non-positional!"};
99 FailFmtWithError<1>(
"%s%1$s", err_mix);
100 FailFmtWithError<2>(
"%2$*d", err_mix);
101 FailFmtWithError<2>(
"%*2$d", err_mix);
102 FailFmtWithError<2>(
"%.*3$d", err_mix);
103 FailFmtWithError<2>(
"%2$.*d", err_mix);
105 auto err_num{
"Format specifier count must match the argument count!"};
106 FailFmtWithError<1>(
"", err_num);
107 FailFmtWithError<0>(
"%s", err_num);
108 FailFmtWithError<2>(
"%s", err_num);
109 FailFmtWithError<0>(
"%1$s", err_num);
110 FailFmtWithError<2>(
"%1$s", err_num);
111 FailFmtWithError<1>(
"%*c", err_num);
113 auto err_0_pos{
"Positional format specifier must have position of at least 1"};
114 FailFmtWithError<1>(
"%$s", err_0_pos);
115 FailFmtWithError<1>(
"%$", err_0_pos);
116 FailFmtWithError<0>(
"%0$", err_0_pos);
117 FailFmtWithError<0>(
"%0$s", err_0_pos);
118 FailFmtWithError<2>(
"%2$*$d", err_0_pos);
119 FailFmtWithError<2>(
"%2$*0$d", err_0_pos);
120 FailFmtWithError<3>(
"%3$*2$.*$f", err_0_pos);
121 FailFmtWithError<3>(
"%3$*2$.*0$f", err_0_pos);
123 auto err_term{
"Format specifier incorrectly terminated by end of string"};
124 FailFmtWithError<1>(
"%", err_term);
125 FailFmtWithError<1>(
"%9", err_term);
126 FailFmtWithError<1>(
"%9.", err_term);
127 FailFmtWithError<1>(
"%9.9", err_term);
128 FailFmtWithError<1>(
"%*", err_term);
129 FailFmtWithError<1>(
"%+*", err_term);
130 FailFmtWithError<1>(
"%.*", err_term);
131 FailFmtWithError<1>(
"%9.*", err_term);
132 FailFmtWithError<1>(
"%1$", err_term);
133 FailFmtWithError<1>(
"%1$9", err_term);
134 FailFmtWithError<2>(
"%1$*2$", err_term);
135 FailFmtWithError<2>(
"%1$.*2$", err_term);
136 FailFmtWithError<2>(
"%1$9.*2$", err_term);
141 HasReason{
"tinyformat: %n conversion spec not supported"});
143 HasReason{
"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
145 HasReason{
"tinyformat: Cannot convert from argument type to integer for use as variable width or precision"});
152 HasReason{
"tinyformat: Not enough conversion specifiers in format string"});
154 HasReason{
"tinyformat: Too many conversion specifiers in format string"});
184 BOOST_CHECK_NE(hsh(
"A"), hsh(
"B"));
185 BOOST_CHECK_NE(hsh(
"AA"), hsh(
"A"));
195 const std::vector<std::byte> input{
StringToBuffer(
"once upon a time\n there was a dog \r\nwho liked food\n")};
197 std::optional<std::string> line1{reader.
ReadLine()};
199 std::optional<std::string> line2{reader.
ReadLine()};
201 std::optional<std::string> line3{reader.
ReadLine()};
202 std::optional<std::string> line4{reader.
ReadLine()};
214 const std::vector<std::byte> input{
StringToBuffer(
"once upon a time there\nwas a dog who liked tea\n")};
220 BOOST_CHECK_EXCEPTION(reader1.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
248 BOOST_CHECK_EXCEPTION(reader.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
259 const std::vector<std::byte> input{
StringToBuffer(
"a\nbaboon\n")};
263 BOOST_CHECK_EXCEPTION(reader.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
268 BOOST_CHECK_EXCEPTION(reader.
ReadLine(), std::runtime_error,
HasReason{
"max_line_length exceeded by LineReader"});
275 const std::vector<std::byte> input{
StringToBuffer(
"once upon a time there")};
287 const std::vector<std::byte> input{
StringToBuffer(
"once upon a time\n there was a dog \r\nwho liked food")};
293 BOOST_CHECK_EXCEPTION(reader.
ReadLength(128), std::runtime_error,
HasReason{
"Not enough data in buffer"});
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
BOOST_AUTO_TEST_SUITE_END()
static constexpr void CheckNumFormatSpecifiers(const char *str)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK_NO_THROW(stmt)
#define BOOST_CHECK(expr)
size_t Remaining() const
Returns remaining size of bytes in buffer.
std::string ReadLength(size_t len)
Returns string from current iterator position of specified length if possible and advances iterator o...
std::optional< std::string > ReadLine()
Returns a string from current iterator position up to (but not including) next and advances iterator...
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
void TfmFormatZeroes(const std::string &fmt)
std::vector< std::byte > StringToBuffer(const std::string &str)
void PassFmt(ConstevalFormatString< NumArgs > fmt)
void FailFmtWithError(const char *wrong_fmt, std::string_view error)