33#include <jau/test/catch2_ext.hpp>
42using namespace std::literals;
52 const size_t bsz = 1024;
56 nchars = std::snprintf(&str[0], bsz,
57 "format_000a: %f, %f, %zu, %" PRIu64
", %" PRIu64
", %" PRIx64
", %06" PRIu64
", %06" PRIx64
", %d\n",
58 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1,
59 a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64,
73 return jau::format_string(
"format_010a: %f, %f, %zu, %" PRIu64
", %" PRIu64
", %" PRIx64
", %06" PRIu64
", %06" PRIx64
", %d\n",
74 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1,
75 a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64,
80 return jau::format_string_n(1023,
"format_020a: %f, %f, %zu, %" PRIu64
", %" PRIu64
", %" PRIx64
", %06" PRIu64
", %06" PRIx64
", %d\n",
81 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1,
82 a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64,
87 std::ostringstream ss1;
88 ss1 <<
"format_030a: "
89 << fa + 1.0_f32 <<
", "
90 << fb + 1.0_f32 <<
", "
92 << a_u64 + 1_u64 <<
", "
98std::string format_040a_stdformat(
float fa,
float fb,
size_t sz1, uint64_t a_u64,
int i) {
99 return std::format(
"format_040a: {0}, {1}, {3}, {4}, {5}\n",
100 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1, a_u64 + 1_u64, i + 1);
108 const size_t bsz = 1024;
112 nchars = std::snprintf(&str[0], bsz,
113 "format_000b: %.2f, %2.2f, %zu, %" PRIu64
", %" PRIu64
", %" PRIx64
", %06" PRIu64
", %06" PRIx64
", %03d\n",
114 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1,
115 a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64,
128 return jau::format_string(
"format_010b: %.2f, %2.2f, %zu, %" PRIu64
", %" PRIu64
", %" PRIx64
", %06" PRIu64
", %06" PRIx64
", %03d\n",
129 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1,
130 a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64,
135 return jau::format_string_n(1023,
"format_020b: %.2f, %2.2f, %zu, %" PRIu64
", %" PRIu64
", %" PRIx64
", %06" PRIu64
", %06" PRIx64
", %03d\n",
136 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1,
137 a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64, a_u64 + 1_u64,
142 std::ostringstream ss1;
144 ss1 <<
"format_030b: "
145 << fa + 1.0_f32 <<
", ";
147 ss1 << fb + 1.0_f32 <<
", "
149 << a_u64 + 1_u64 <<
", ";
151 ss1 << i + 1 <<
"\n";
156std::string format_040b_stdformat(
float fa,
float fb,
size_t sz1, uint64_t a_u64,
int i) {
157 return std::format(
"format_040b: {0:.2f}, {1:2.2f}, {3}, {4}, {5:03d}\n",
158 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1, a_u64 + 1_u64, i + 1);
162template <
typename Func>
164 constexpr float fa = 1.1f, fb = 2.2f;
165 constexpr size_t sz1 = 1;
166 constexpr uint64_t sz2 = 2;
169 const std::string s =
func(fa, fb, sz1, sz2, i);
170 volatile size_t l = s.length();
200TEST_CASE(
"jau::cfmt_10",
"[jau][std::string][jau::cfmt]") {
202 constexpr float fa = 1.123456f, fb = 2.2f;
203 constexpr size_t sz1 = 1;
204 constexpr int64_t v_i64 = 2;
205 constexpr uint64_t v_u64 = 3;
207 const float *pf = &fa;
212 std::cerr <<
"XXX: " << __LINE__ <<
": " << r << std::endl;
217 jau::cfmt::Result r =
jau::cfmt::formatR(s,
"format_check: %.2f, %2.2f, %zu, %" PRIi64
", %" PRIi64
"d, %" PRIi64
"X, %06" PRIu64
"d, %06" PRIu64
"X, %03d\n",
218 fa, fb, sz1, v_i64, v_i64, v_i64, v_u64, v_u64, i);
219 std::cerr <<
"XXX: " << __LINE__ <<
": " << r << std::endl;
225 std::cerr <<
"XXX: " << __LINE__ <<
": " << r << std::endl;
237 static_assert(
false == std::is_signed_v<const float*>);
238 static_assert(
false == std::is_unsigned_v<const float*>);
240 static_assert(
true == std::is_signed_v<float>);
241 static_assert(
false == std::is_unsigned_v<float>);
245 using T =
unsigned int;
247 using U =
typename std::conditional_t<std::is_unsigned_v<T>, std::make_signed<T>, std::type_identity<T>>
::type;
248 static_assert( std::is_same_v<T, T> );
249 static_assert( std::is_same_v<E, U> );
255 using U =
typename std::conditional_t<std::is_unsigned_v<T>, std::make_signed<T>, std::type_identity<T>>
::type;
256 static_assert( std::is_same_v<T, T> );
257 static_assert( std::is_same_v<E, U> );
266 static_assert(0 ==
jau::cfmt::checkLine(
" %" PRIi64
", %" PRIi64
", %08" PRIi64
".", (int64_t)1, (int64_t)1, (int64_t)1));
271 static_assert(-1 ==
jau::cfmt::check(
"unsigned int -> int %d", (
unsigned int)1));
272 static_assert( 1 ==
jau::cfmt::check(
"unsigned char -> int %d", (
unsigned char)1));
273 static_assert( 1 ==
jau::cfmt::check(
"unsigned int -> unsigned int %u", (
unsigned int)1));
275 static_assert(-1 ==
jau::cfmt::check(
" uint64_t -> int64_t %" PRIi64, (uint64_t)1));
276 static_assert( 1 ==
jau::cfmt::check(
" int64_t -> uint64_t %" PRIu64, (int64_t)1));
284 printf(
"XXX: sizeof(long) %zu, %s\n",
sizeof(
long), res.
toString().c_str());
286 if constexpr (
sizeof(long) <=
sizeof(int) ) {
296 static_assert(3 ==
jau::cfmt::check(
" %" PRIi64
", %" PRIi64
", %08" PRIi64
".", (int64_t)1, (int64_t)1, (int64_t)1));
297 static_assert(3 ==
jau::cfmt::check(
" %" PRIi64
", %" PRIi64
", %08" PRIi64
".", v_i64, v_i64, v_i64));
299 const char* cs1 =
"lala";
319 static_assert(5 ==
jau::cfmt::checkR(
"Hello %" PRIi64
", %" PRIu64
", %" PRIx64
", %06" PRIu64
", %06" PRIx64
"",
329 if constexpr (
sizeof(long) <= 4 ) {
334 static_assert(-6 ==
jau::cfmt::checkR(
"Hello 1 %.2f, 2 %2.2f, 3 %zu, 4 %" PRIi64
", 5 %03d, 6 %p - end",
336 static_assert(-6 ==
jau::cfmt::check(
"Hello 1 %.2f, 2 %2.2f, 3 %zu, 4 %" PRIi64
", 5 %03d, 6 %p - end",
337 fa, fb, sz1, v_i64, i, i));
340 static_assert(0 <=
jau::cfmt::checkR(
"format_020a: %f, %f, %zu, %" PRIu64
", %d\n",
341 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1, v_i64 + 1_u64, i + 1).
argumentCount());
342 const std::string s =
jau_format_string(
"format_020a: %f, %f, %zu, %" PRIu64
", %d\n",
343 fa + 1.0_f32, fb + 1.0_f32, sz1 + 1, v_i64 + 1_u64, i + 1);
344 REQUIRE( s.size() > 0 );
348 REQUIRE( s0.size() > 0 );
356 REQUIRE(
"Hello 1" == s1);
359 std::cerr <<
"XXX: " << __LINE__ <<
": " << c1 << std::endl;
370 REQUIRE(9 == std::snprintf(buf,
sizeof(buf),
"Hello 1 %d", i));
378 std::cerr <<
"XXX: " << __LINE__ <<
": " << pc << std::endl;
379 REQUIRE(2 ==
jau::cfmt::checkR(
"Hello 1 %.2f, 2 %2.2f - end", fa, fb).argumentCount());
383 std::cerr <<
"XXX: " << __LINE__ <<
": " << pc << std::endl;
384 REQUIRE(3 ==
jau::cfmt::checkR(
"Hello 1 %.2f, 2 %2.2f, 3 %zu - end", fa, fb, sz1).argumentCount());
386 REQUIRE(4 ==
jau::cfmt::checkR(
"Hello 1 %.2f, 2 %2.2f, 3 %zu, 4 %" PRIi64
" - end", fa, fb, sz1, v_i64).argumentCount());
387 REQUIRE(5 ==
jau::cfmt::checkR(
"Hello 1 %.2f, 2 %2.2f, 3 %zu, 4 %" PRIi64
", 5 %03d - end", fa, fb, sz1, v_i64, i).argumentCount());
388 REQUIRE(6 ==
jau::cfmt::checkR(
"Hello 1 %.2f, 2 %2.2f, 3 %zu, 4 %" PRIi64
", 5 %03d, 6 %p - end", fa, fb, sz1, v_i64, i, pf).argumentCount());
394 if constexpr (
sizeof(long) <= 4 ) {
399 REQUIRE(-6 ==
jau::cfmt::checkR(
"Hello 1 %.2f, 2 %2.2f, 3 %zu, 4 %" PRIi64
", 5 %03d, 6 %p - end",
400 fa, fb, sz1, v_i64, i, i).argumentCount());
404TEST_CASE(
"jau::cfmt_01",
"[jau][std::string][format_string]") {
409TEST_CASE(
"jau::cfmt_10 debug",
"[jau][std::string][format_string][debug]") {
411 INFO_PRINT(
"lala002 %d, %f, %s", 1, 3.14,
"hello world");
412 std::string s1 =
"Hello";
413 std::string_view sv1 = s1;
414 const char * s1p = s1.c_str();
415 INFO_PRINT(
"lala003 %s, %s, %s", s1, sv1, s1p);
constexpr ssize_t argumentCount() const noexcept
Arguments processed.
std::string toString() const
constexpr bool success() const noexcept
true if operation was successful, otherwise indicates error
#define ERR_PRINT3(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
#define INFO_PRINT(fmt,...)
Use for unconditional informal messages, prefix '[elapsed_time] Info: '.
Result formatR(std::string &s, std::string_view fmt, const Targs &...args) noexcept
Strict format with type validation of arguments against the format string, appending to the given des...
consteval_cxx20 int checkLine(std::string_view fmt, const Targs &...) noexcept
Strict type validation of arguments against the format string.
consteval_cxx20 Result checkR(std::string_view fmt, const Targs &...) noexcept
Strict type validation of arguments against the format string.
#define jau_format_string(fmt,...)
Macro, safely returns a (non-truncated) string according to snprintf() formatting rules using a reser...
consteval_cxx20 ssize_t check(std::string_view fmt, const Targs &...) noexcept
Strict type validation of arguments against the format string.
std::string format_string_n(const std::size_t maxStrLen, std::string_view format, const Args &...args) noexcept
Safely returns a (potentially truncated) string according to snprintf() formatting rules and variable...
std::string format_string(std::string_view format, const Args &...args) noexcept
Safely returns a (non-truncated) string according to snprintf() formatting rules using a reserved str...
int printf(const char *format,...)
Operating Systems predefined macros.
static size_t test_format(const Func func, bool output)
static std::string format_020b_jaufmtstr_n(float fa, float fb, size_t sz1, uint64_t a_u64, int i)
static std::string format_snprintf_ffszu64d(float fa, float fb, size_t sz1, uint64_t a_u64, int i)
static std::string format_010a_jaufmtstr(float fa, float fb, size_t sz1, uint64_t a_u64, int i)
TEST_CASE("jau::cfmt_10", "[jau][std::string][jau::cfmt]")
static std::string format_000b_vsnprintf(float fa, float fb, size_t sz1, uint64_t a_u64, int i)
static std::string format_020a_jaufmtstr_n(float fa, float fb, size_t sz1, uint64_t a_u64, int i)
static std::string format_030a_strstream(float fa, float fb, size_t sz1, uint64_t a_u64, int i)
static std::string format_030b_strstream(float fa, float fb, size_t sz1, uint64_t a_u64, int i)
static std::string format_010b_jaufmtstr(float fa, float fb, size_t sz1, uint64_t a_u64, int i)