29 #define CATCH_CONFIG_MAIN
30 #include <catch2/catch_amalgamated.hpp>
36 #define SHOW_DECIMAL_STRING_STATS 0
38 #if SHOW_DECIMAL_STRING_STATS
40 void show_decimal_string_stats(
const std::string msg,
const T value,
const bool use_separator,
const int min_width) {
41 const nsize_t max_digits10 = std::numeric_limits<T>::is_signed ?
42 jau::digits10<T>(std::numeric_limits<T>::min()) :
43 jau::digits10<T>(std::numeric_limits<T>::max());
46 const T max_value = std::numeric_limits<T>::max();
47 const T min_value = std::numeric_limits<T>::min();
48 const nsize_t max_digits10_1 = jau::digits10<T>(min_value);
49 const nsize_t max_digits10_2 = jau::digits10<T>(max_value);
51 const nsize_t max_commas = use_separator ? ( max_digits10 - 1 ) / 3 : 0;
52 const nsize_t max_chars = max_digits10 + max_commas;
54 const nsize_t digit10_count = jau::digits10<T>(value);
56 const nsize_t comma_count = use_separator ? ( digit10_count - 1 ) / 3 : 0;
57 const nsize_t net_chars = digit10_count + comma_count;
58 const nsize_t total_chars = std::min<nsize_t>(max_chars, std::max<nsize_t>(min_width, net_chars));
69 std::string s = to_decstring<T>(value, use_separator ?
',' : 0, min_width);
74 static void test_int32_t(
const std::string msg,
const int32_t v,
const size_t expStrLen,
const std::string expStr) {
75 #if SHOW_DECIMAL_STRING_STATS
76 show_decimal_string_stats<int32_t>(msg, v,
true , 0 );
81 REQUIRE(str.length() == expStrLen);
82 REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
85 static void test_uint32_t(
const std::string msg,
const uint32_t v,
const size_t expStrLen,
const std::string expStr) {
86 #if SHOW_DECIMAL_STRING_STATS
87 show_decimal_string_stats<uint32_t>(msg, v,
true , 0 );
93 REQUIRE(str.length() == expStrLen);
94 REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
97 static void test_uint64_t(
const std::string msg,
const uint64_t v,
const size_t expStrLen,
const std::string expStr) {
98 #if SHOW_DECIMAL_STRING_STATS
99 show_decimal_string_stats<uint64_t>(msg, v,
true , 0 );
105 REQUIRE(str.length() == expStrLen);
106 REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
109 TEST_CASE(
"Integer Decimal String Test 01",
"[datatype][int_dec_string]" ) {
110 test_int32_t(
"INT32_MIN", INT32_MIN, 14,
"-2,147,483,648");
114 test_int32_t(
"INT32_MAX", INT32_MAX, 13,
"2,147,483,647");
119 test_uint32_t(
"UINT32_MAX", UINT32_MAX, 13,
"4,294,967,295");
124 test_uint64_t(
"UINT64_MAX", UINT64_MAX, 26,
"18,446,744,073,709,551,615");