Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
test_to_string.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 
33 #include <jau/basic_types.hpp>
34 
35 #include "test_datatype01.hpp"
36 
37 using namespace jau;
38 
39 typedef std::vector<int> std_vec_int;
41 
42 typedef std_vec_int::iterator std_vec_int_iter;
44 
45 typedef std_vec_int::const_iterator std_vec_int_citer;
47 
50 
51 typedef decltype( std::declval<std_vec_int_citer>().operator->() ) std_vec_int_citer_ptrop_retval;
52 
53 TEST_CASE( "JAU to_string() Test 00 - jau::to_string() std::string conversion", "[jau][std::string][to_string()]" ) {
54  int i1 = 1;
55  uint64_t u64_1 = 1116791496961ull;
56  void * p_v_1 = (void *)0xAFFE;
57  float float_1 = 1.65f;
58 
59  Addr48Bit addr48bit_1(u64_1);
60 
61  CHECK( "1" == jau::to_string<int>(i1) );
62  CHECK( "1116791496961" == jau::to_string(u64_1) );
63  if( sizeof(p_v_1) == 8 ) {
64  CHECK( "0x000000000000affe" == jau::to_string(p_v_1) );
65  } else if( sizeof(p_v_1) == 4 ) {
66  CHECK( "0x0000affe" == jau::to_string(p_v_1) );
67  }
68  CHECK( "1.650000" == jau::to_string(float_1) );
69 
70  CHECK( "01:04:05:F5:E1:01" == jau::to_string(addr48bit_1) );
71 
72  //
73  // Validating 'pointer std::vector::const_iterator.operator->()'
74  // and the to_string type trait logic of it.
75  //
76 
77  // jau::type_cue<std_vec_int_citer>::print("std_vec_int_citer", jau::TypeTraitGroup::ALL);
78  // jau::type_cue<std_vec_int_citer_pointer>::print("std_vec_int_citer_pointer", jau::TypeTraitGroup::ALL);
79 
80  // jau::type_cue<std_vec_int_citer_ptrop_retval>::print("std_vec_int_citer_ptrop_retval", jau::TypeTraitGroup::ALL);
81  printf("jau::has_member_of_pointer<std_vec_int_citer>) %d\n", jau::has_member_of_pointer<std_vec_int_citer>::value);
82 
83  std_vec_int vec_int_1;
84  vec_int_1.push_back(1); vec_int_1.push_back(2); vec_int_1.push_back(3);
85  std_vec_int_citer vec_int_citer_1B = vec_int_1.cbegin();
86  uint8_t* vec_int_citer_1B_ptr = (uint8_t*)(vec_int_citer_1B.operator->());
87  std::string vec_int_citer_1B_str = to_hexstring(vec_int_citer_1B_ptr);
88 
89  std_vec_int_citer vec_int_citer_1E = vec_int_1.cend();
90  uint8_t* vec_int_citer_1E_ptr = (uint8_t*)(vec_int_citer_1E.operator->());
91  std::string vec_int_citer_1E_str = to_hexstring(vec_int_citer_1E_ptr);
92 
93  std::ptrdiff_t vec_int_citer_1E_1B_ptrdiff = vec_int_citer_1E_ptr - vec_int_citer_1B_ptr;
94  int vec_int_citer_1E_1B_ptr_count = vec_int_citer_1E_1B_ptrdiff / sizeof(int);
95  int vec_int_citer_1E_1B_itr_count = vec_int_citer_1E - vec_int_citer_1B;
96 
97  printf("vec_int_citer_1E - vec_int_citer_1B = itr_count %d, ptr_count %d\n",
98  vec_int_citer_1E_1B_itr_count, vec_int_citer_1E_1B_ptr_count);
99  printf("vec_int_citer_1E - vec_int_citer_1B = %d\n", vec_int_citer_1E_1B_itr_count);
100  printf("vec_int_citer_1B_ptr %s, vec_int_citer_1E1_ptr = %s\n", vec_int_citer_1B_str.c_str(), vec_int_citer_1E_str.c_str());
101 
102  CHECK(vec_int_citer_1E_1B_itr_count == 3);
103  CHECK(vec_int_citer_1E_1B_itr_count == vec_int_citer_1E_1B_ptr_count);
104 
105  CHECK( vec_int_citer_1E_str == jau::to_string(vec_int_citer_1E) );
106 }
107 
std_vec_int
std::vector< int > std_vec_int
Definition: test_to_string.cpp:39
std_vec_int_citer_ptrop_retval
decltype(std::declval< std_vec_int_citer >().operator->()) typedef std_vec_int_citer_ptrop_retval
Definition: test_to_string.cpp:51
jau
Definition: basic_algos.hpp:34
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
test_datatype01.hpp
jau::has_member_of_pointer
Checker for member of pointer '->' operator with convertible pointer return, no arguments.
Definition: type_traits_queries.hpp:309
type_traits_queries.hpp
std_vec_int_citer
std_vec_int::const_iterator std_vec_int_citer
Definition: test_to_string.cpp:45
JAU_TYPENAME_CUE_ALL
#define JAU_TYPENAME_CUE_ALL(A)
Definition: type_traits_queries.hpp:223
jau::to_hexstring
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
Definition: string_util.hpp:104
std_vec_int_citer_pointer
std_vec_int_citer::pointer std_vec_int_citer_pointer
Definition: test_to_string.cpp:48
TEST_CASE
TEST_CASE("JAU to_string() Test 00 - jau::to_string() std::string conversion", "[jau][std::string][to_string()]")
Definition: test_to_string.cpp:53
std_vec_int_iter
std_vec_int::iterator std_vec_int_iter
Definition: test_to_string.cpp:42
basic_types.hpp
Addr48Bit
Definition: test_datatype01.hpp:113