Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
test_intdecstring01.cpp
Go to the documentation of this file.
1 /*
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include <iostream>
25 #include <cassert>
26 #include <cinttypes>
27 #include <cstring>
28 
29 #define CATCH_CONFIG_MAIN
30 #include <catch2/catch_amalgamated.hpp>
31 
32 #include <jau/basic_types.hpp>
33 
34 using namespace jau;
35 
36 #define SHOW_DECIMAL_STRING_STATS 0
37 
38 #if SHOW_DECIMAL_STRING_STATS
39  template<class T>
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());
44 
45  const nsize_t max_digits10_0 = std::numeric_limits<T>::digits10;
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);
50 
51  const nsize_t max_commas = use_separator ? ( max_digits10 - 1 ) / 3 : 0;
52  const nsize_t max_chars = max_digits10 + max_commas;
53 
54  const nsize_t digit10_count = jau::digits10<T>(value);
55 
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));
59 
60  INFO(msg+": value "+std::to_string(value)+", use_separator "+std::to_string(use_separator)+", min_width "+std::to_string(min_width));
61  INFO(msg+": min "+std::to_string(min_value)+", max "+std::to_string(max_value));
62  INFO(msg+": max_digits10 "+std::to_string(max_digits10)+" [ orig "+std::to_string(max_digits10_0)+", min "+std::to_string(max_digits10_1)+", max "+std::to_string(max_digits10_2)+"]");
63  INFO(msg+": max_commas "+std::to_string(max_commas));
64  INFO(msg+": max_chars "+std::to_string(max_chars));
65  INFO(msg+": value digits10 "+std::to_string(digit10_count));
66  INFO(msg+": value commas "+std::to_string(comma_count));
67  INFO(msg+": value net_chars "+std::to_string(net_chars));
68  INFO(msg+": value total_chars "+std::to_string(total_chars));
69  std::string s = to_decstring<T>(value, use_separator ? ',' : 0, min_width);
70  INFO(msg+": result '"+s+"', len "+std::to_string(s.length()));
71  }
72 #endif
73 
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 /* use_separator */, 0 /* min_width */);
77 #endif
78  const std::string str = to_decstring(v);
79  INFO(msg+": has '"+str+"', len "+std::to_string(str.length()));
80  INFO(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(str==expStr));
81  REQUIRE(str.length() == expStrLen);
82  REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
83 }
84 
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 /* use_separator */, 0 /* min_width */);
88 #endif
89 
90  const std::string str = to_decstring(v);
91  INFO(msg+": has '"+str+"', len "+std::to_string(str.length()));
92  INFO(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(str==expStr));
93  REQUIRE(str.length() == expStrLen);
94  REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
95 }
96 
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 /* use_separator */, 0 /* min_width */);
100 #endif
101 
102  const std::string str = to_decstring(v);
103  INFO(msg+": has '"+str+"', len "+std::to_string(str.length()));
104  INFO(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(str==expStr));
105  REQUIRE(str.length() == expStrLen);
106  REQUIRE_THAT(str, Catch::Matchers::Equals(expStr, Catch::CaseSensitive::Yes));
107 }
108 
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");
111  test_int32_t("int32_t -thousand", -1000, 6, "-1,000");
112  test_int32_t("int32_t one", 1, 1, "1");
113  test_int32_t("int32_t thousand", 1000, 5, "1,000");
114  test_int32_t("INT32_MAX", INT32_MAX, 13, "2,147,483,647");
115 
116  test_uint32_t("UINT32_MIN", 0, 1, "0");
117  test_uint32_t("uint32_t one", 1, 1, "1");
118  test_uint32_t("uint32_t thousand", 1000, 5, "1,000");
119  test_uint32_t("UINT32_MAX", UINT32_MAX, 13, "4,294,967,295");
120 
121  test_uint64_t("UINT64_MIN", 0, 1, "0");
122  test_uint64_t("uint64_t one", 1, 1, "1");
123  test_uint64_t("uint64_t thousand", 1000, 5, "1,000");
124  test_uint64_t("UINT64_MAX", UINT64_MAX, 26, "18,446,744,073,709,551,615");
125 }
126 
127 
jau::to_decstring
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.
Definition: string_util.hpp:143
test_int32_t
static void test_int32_t(const std::string msg, const int32_t v, const size_t expStrLen, const std::string expStr)
Definition: test_intdecstring01.cpp:74
TEST_CASE
TEST_CASE("Integer Decimal String Test 01", "[datatype][int_dec_string]")
Definition: test_intdecstring01.cpp:109
jau
Definition: basic_algos.hpp:34
test_uint32_t
static void test_uint32_t(const std::string msg, const uint32_t v, const size_t expStrLen, const std::string expStr)
Definition: test_intdecstring01.cpp:85
jau::to_string
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
Definition: byte_util.hpp:198
jau::nsize_t
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition: int_types.hpp:44
jau::digits10
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>().
Definition: int_math.hpp:124
basic_types.hpp
test_uint64_t
static void test_uint64_t(const std::string msg, const uint64_t v, const size_t expStrLen, const std::string expStr)
Definition: test_intdecstring01.cpp:97