jaulib v1.3.0
Jau Support Library (C++, Java, ..)
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 <cassert>
25#include <cinttypes>
26#include <cstring>
27
29
31#include <jau/basic_types.hpp>
32
33#include "test_datatype01.hpp"
34
35using namespace jau;
36
37typedef std::vector<int> std_vec_int;
39
40typedef std_vec_int::iterator std_vec_int_iter;
42
43typedef std_vec_int::const_iterator std_vec_int_citer;
45
48
49typedef decltype( std::declval<std_vec_int_citer>().operator->() ) std_vec_int_citer_ptrop_retval;
50
51TEST_CASE( "JAU to_string() Test 00 - jau::to_string() std::string conversion", "[jau][std::string][to_string()]" ) {
52 int i1 = 1;
53 uint64_t u64_1 = 1116791496961ull;
54 void * p_v_1 = (void *)0xAFFE;
55 float float_1 = 1.65f;
56
57 Addr48Bit addr48bit_1(u64_1);
58
59 CHECK( "1" == jau::to_string<int>(i1) );
60 CHECK( "1116791496961" == jau::to_string(u64_1) );
61 if( sizeof(p_v_1) == 8 ) {
62 CHECK( "0x000000000000affe" == jau::to_string(p_v_1) );
63 } else if( sizeof(p_v_1) == 4 ) {
64 CHECK( "0x0000affe" == jau::to_string(p_v_1) );
65 }
66 CHECK( "1.650000" == jau::to_string(float_1) );
67
68 CHECK( "01:04:05:F5:E1:01" == jau::to_string(addr48bit_1) );
69
70 //
71 // Validating 'pointer std::vector::const_iterator.operator->()'
72 // and the to_string type trait logic of it.
73 //
74
75 // jau::type_cue<std_vec_int_citer>::print("std_vec_int_citer", jau::TypeTraitGroup::ALL);
76 // jau::type_cue<std_vec_int_citer_pointer>::print("std_vec_int_citer_pointer", jau::TypeTraitGroup::ALL);
77
78 // jau::type_cue<std_vec_int_citer_ptrop_retval>::print("std_vec_int_citer_ptrop_retval", jau::TypeTraitGroup::ALL);
79 printf("jau::has_member_of_pointer<std_vec_int_citer>) %d\n", jau::has_member_of_pointer<std_vec_int_citer>::value);
80
81 std_vec_int vec_int_1;
82 vec_int_1.push_back(1); vec_int_1.push_back(2); vec_int_1.push_back(3);
83 std_vec_int_citer vec_int_citer_1B = vec_int_1.cbegin();
84 uint8_t* vec_int_citer_1B_ptr = (uint8_t*)(vec_int_citer_1B.operator->());
85 std::string vec_int_citer_1B_str = to_hexstring(vec_int_citer_1B_ptr);
86
87 std_vec_int_citer vec_int_citer_1E = vec_int_1.cend();
88 uint8_t* vec_int_citer_1E_ptr = (uint8_t*)(vec_int_citer_1E.operator->());
89 std::string vec_int_citer_1E_str = to_hexstring(vec_int_citer_1E_ptr);
90
91 std::ptrdiff_t vec_int_citer_1E_1B_ptrdiff = vec_int_citer_1E_ptr - vec_int_citer_1B_ptr;
92 size_t vec_int_citer_1E_1B_ptr_count = vec_int_citer_1E_1B_ptrdiff / sizeof(int);
93 size_t vec_int_citer_1E_1B_itr_count = vec_int_citer_1E - vec_int_citer_1B;
94
95 printf("vec_int_citer_1E - vec_int_citer_1B = itr_count %zu, ptr_count %zu\n",
96 vec_int_citer_1E_1B_itr_count, vec_int_citer_1E_1B_ptr_count);
97 printf("vec_int_citer_1E - vec_int_citer_1B = %zu\n", vec_int_citer_1E_1B_itr_count);
98 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());
99
100 CHECK(vec_int_citer_1E_1B_itr_count == 3);
101 CHECK(vec_int_citer_1E_1B_itr_count == vec_int_citer_1E_1B_ptr_count);
102
103 CHECK( vec_int_citer_1E_str == jau::to_string(vec_int_citer_1E) );
104}
105
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
#define JAU_TYPENAME_CUE_ALL(A)
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
STL namespace.
Checker for member of pointer '->' operator with convertible pointer return, no arguments.
int printf(const char *format,...)
Operating Systems predefined macros.
TEST_CASE("JAU to_string() Test 00 - jau::to_string() std::string conversion", "[jau][std::string][to_string()]")
std::vector< int > std_vec_int
std_vec_int_citer::pointer std_vec_int_citer_pointer
decltype(std::declval< std_vec_int_citer >().operator->()) std_vec_int_citer_ptrop_retval
std_vec_int::const_iterator std_vec_int_citer
std_vec_int::iterator std_vec_int_iter