34#define SHOW_DECIMAL_STRING_STATS 0
36#if SHOW_DECIMAL_STRING_STATS
38 void show_decimal_string_stats(
const std::string msg,
const T value,
const bool use_separator,
const int min_width) {
39 const nsize_t max_digits10 = std::numeric_limits<T>::is_signed ?
46 const nsize_t max_digits10_1 = jau::digits10<T>(min_value);
47 const nsize_t max_digits10_2 = jau::digits10<T>(max_value);
49 const nsize_t max_commas = use_separator ? ( max_digits10 - 1 ) / 3 : 0;
50 const nsize_t max_chars = max_digits10 + max_commas;
52 const nsize_t digit10_count = jau::digits10<T>(value);
54 const nsize_t comma_count = use_separator ? ( digit10_count - 1 ) / 3 : 0;
55 const nsize_t net_chars = digit10_count + comma_count;
56 const nsize_t total_chars = std::min<nsize_t>(max_chars, std::max<nsize_t>(min_width, net_chars));
67 std::string s = to_decstring<T>(value, use_separator ?
',' : 0, min_width);
72static void test_int32_t(
const std::string& msg,
const int32_t v,
const size_t expStrLen,
const std::string& expStr) {
73#if SHOW_DECIMAL_STRING_STATS
74 show_decimal_string_stats<int32_t>(msg, v,
true , 0 );
79 REQUIRE(str.length() == expStrLen);
80 REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
83static void test_uint32_t(
const std::string& msg,
const uint32_t v,
const size_t expStrLen,
const std::string& expStr) {
84#if SHOW_DECIMAL_STRING_STATS
85 show_decimal_string_stats<uint32_t>(msg, v,
true , 0 );
91 REQUIRE(str.length() == expStrLen);
92 REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
95static void test_uint64_t(
const std::string& msg,
const uint64_t v,
const size_t expStrLen,
const std::string& expStr) {
96#if SHOW_DECIMAL_STRING_STATS
97 show_decimal_string_stats<uint64_t>(msg, v,
true , 0 );
103 REQUIRE(str.length() == expStrLen);
104 REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
107TEST_CASE(
"Integer Decimal String Test 01",
"[datatype][int_dec_string]" ) {
108 test_int32_t(
"INT32_MIN", INT32_MIN, 14,
"-2,147,483,648");
112 test_int32_t(
"INT32_MAX", INT32_MAX, 13,
"2,147,483,647");
117 test_uint32_t(
"UINT32_MAX", UINT32_MAX, 13,
"4,294,967,295");
122 test_uint64_t(
"UINT64_MAX", UINT64_MAX, 26,
"18,446,744,073,709,551,615");
std::string to_string(const alphabet &v) noexcept
constexpr nsize_t digits10(const T x, const snsize_t x_sign, const bool sign_is_digit=true) noexcept
Returns the number of decimal digits of the given integral value number using std::log10<T>().
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
std::string to_decstring(const value_type &v, const char separator=',', const nsize_t width=0) noexcept
Produce a decimal string representation of an integral integer value.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
TEST_CASE("Integer Decimal String Test 01", "[datatype][int_dec_string]")
static void test_uint64_t(const std::string &msg, const uint64_t v, const size_t expStrLen, const std::string &expStr)
static void test_int32_t(const std::string &msg, const int32_t v, const size_t expStrLen, const std::string &expStr)
static void test_uint32_t(const std::string &msg, const uint32_t v, const size_t expStrLen, const std::string &expStr)