forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_string_tests.cpp
149 lines (132 loc) · 5.69 KB
/
util_string_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2024-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <util/string.h>
#include <boost/test/unit_test.hpp>
#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>
void PassFmt(ConstevalFormatString<NumArgs> fmt)
{
// Execute compile-time check again at run-time to get code coverage stats
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>
void FailFmtWithError(const char* wrong_fmt, std::string_view error)
{
BOOST_CHECK_EXCEPTION(CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt), const char*, HasReason{error});
}
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");
PassFmt<1>("%s%%");
PassFmt<0>(" 1$s");
PassFmt<1>("%1$s");
PassFmt<1>("%1$s%1$s");
PassFmt<2>("%2$s");
PassFmt<2>("%2$s 4$s %2$s");
PassFmt<129>("%129$s 999$s %2$s");
PassFmt<1>("%02d");
PassFmt<1>("%+2s");
PassFmt<1>("%.6i");
PassFmt<1>("%5.2f");
PassFmt<1>("%5.f");
PassFmt<1>("%.f");
PassFmt<1>("%#x");
PassFmt<1>("%1$5i");
PassFmt<1>("%1$-5i");
PassFmt<1>("%1$.5i");
// tinyformat accepts almost any "type" spec, even '%', or '_', or '\n'.
PassFmt<1>("%123%");
PassFmt<1>("%123%s");
PassFmt<1>("%_");
PassFmt<1>("%\n");
PassFmt<2>("%*c");
PassFmt<2>("%+*c");
PassFmt<2>("%.*f");
PassFmt<3>("%*.*f");
PassFmt<3>("%2$*3$d");
PassFmt<3>("%2$*3$.9d");
PassFmt<3>("%2$.*3$d");
PassFmt<3>("%2$9.*3$d");
PassFmt<3>("%2$+9.*3$d");
PassFmt<4>("%3$*2$.*4$f");
// Make sure multiple flag characters "- 0+" are accepted
PassFmt<3>("'%- 0+*.*f'");
PassFmt<3>("'%1$- 0+*3$.*2$f'");
auto err_mix{"Format specifiers must be all positional or all non-positional!"};
FailFmtWithError<1>("%s%1$s", err_mix);
FailFmtWithError<2>("%2$*d", err_mix);
FailFmtWithError<2>("%*2$d", err_mix);
FailFmtWithError<2>("%.*3$d", err_mix);
FailFmtWithError<2>("%2$.*d", err_mix);
auto err_num{"Format specifier count must match the argument count!"};
FailFmtWithError<1>("", err_num);
FailFmtWithError<0>("%s", err_num);
FailFmtWithError<2>("%s", err_num);
FailFmtWithError<0>("%1$s", err_num);
FailFmtWithError<2>("%1$s", err_num);
FailFmtWithError<1>("%*c", err_num);
auto err_0_pos{"Positional format specifier must have position of at least 1"};
FailFmtWithError<1>("%$s", err_0_pos);
FailFmtWithError<1>("%$", err_0_pos);
FailFmtWithError<0>("%0$", err_0_pos);
FailFmtWithError<0>("%0$s", err_0_pos);
FailFmtWithError<2>("%2$*$d", err_0_pos);
FailFmtWithError<2>("%2$*0$d", err_0_pos);
FailFmtWithError<3>("%3$*2$.*$f", err_0_pos);
FailFmtWithError<3>("%3$*2$.*0$f", err_0_pos);
auto err_term{"Format specifier incorrectly terminated by end of string"};
FailFmtWithError<1>("%", err_term);
FailFmtWithError<1>("%9", err_term);
FailFmtWithError<1>("%9.", err_term);
FailFmtWithError<1>("%9.9", err_term);
FailFmtWithError<1>("%*", err_term);
FailFmtWithError<1>("%+*", err_term);
FailFmtWithError<1>("%.*", err_term);
FailFmtWithError<1>("%9.*", err_term);
FailFmtWithError<1>("%1$", err_term);
FailFmtWithError<1>("%1$9", err_term);
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()