aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_to_string.cpp
blob: e00d2f0112192ae91db9757f52df27d69947759c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cstring>

#define CATCH_CONFIG_MAIN
#include <catch2/catch_amalgamated.hpp>

#include <jau/type_traits_queries.hpp>
#include <jau/basic_types.hpp>

#include "test_datatype01.hpp"

using namespace jau;

typedef std::vector<int> std_vec_int;
JAU_TYPENAME_CUE_ALL(std_vec_int)

typedef std_vec_int::iterator std_vec_int_iter;
JAU_TYPENAME_CUE_ALL(std_vec_int_iter)

typedef std_vec_int::const_iterator std_vec_int_citer;
JAU_TYPENAME_CUE_ALL(std_vec_int_citer)

typedef std_vec_int_citer::pointer std_vec_int_citer_pointer;
JAU_TYPENAME_CUE_ALL(std_vec_int_citer_pointer)

typedef decltype( std::declval<std_vec_int_citer>().operator->() ) std_vec_int_citer_ptrop_retval;

TEST_CASE( "JAU to_string() Test 00 - jau::to_string() std::string conversion", "[jau][std::string][to_string()]" ) {
    int i1 = 1;
    uint64_t u64_1 = 1116791496961ull;
    void * p_v_1 = (void *)0xAFFE;
    float float_1 = 1.65f;

    Addr48Bit addr48bit_1(u64_1);

    CHECK( "1" == jau::to_string<int>(i1) );
    CHECK( "1116791496961" == jau::to_string(u64_1) );
    CHECK( "0x000000000000affe" == jau::to_string(p_v_1) );
    CHECK( "1.650000" == jau::to_string(float_1) );

    CHECK( "01:04:05:F5:E1:01" == jau::to_string(addr48bit_1) );

    //
    // Validating 'pointer std::vector::const_iterator.operator->()'
    // and the to_string type trait logic of it.
    //

    // jau::type_cue<std_vec_int_citer>::print("std_vec_int_citer", jau::TypeTraitGroup::ALL);
    // jau::type_cue<std_vec_int_citer_pointer>::print("std_vec_int_citer_pointer", jau::TypeTraitGroup::ALL);

    // jau::type_cue<std_vec_int_citer_ptrop_retval>::print("std_vec_int_citer_ptrop_retval", jau::TypeTraitGroup::ALL);
    printf("jau::has_member_of_pointer<std_vec_int_citer>) %d\n", jau::has_member_of_pointer<std_vec_int_citer>::value);

    std_vec_int vec_int_1;
    vec_int_1.push_back(1); vec_int_1.push_back(2); vec_int_1.push_back(3);
    std_vec_int_citer vec_int_citer_1B = vec_int_1.cbegin();
    uint8_t* vec_int_citer_1B_ptr = (uint8_t*)(vec_int_citer_1B.operator->());
    std::string vec_int_citer_1B_str = aptrHexString(vec_int_citer_1B_ptr);

    std_vec_int_citer vec_int_citer_1E = vec_int_1.cend();
    uint8_t* vec_int_citer_1E_ptr = (uint8_t*)(vec_int_citer_1E.operator->());
    std::string vec_int_citer_1E_str = aptrHexString(vec_int_citer_1E_ptr);

    std::ptrdiff_t vec_int_citer_1E_1B_ptrdiff = vec_int_citer_1E_ptr - vec_int_citer_1B_ptr;
    int vec_int_citer_1E_1B_ptr_count = vec_int_citer_1E_1B_ptrdiff / sizeof(int);
    int vec_int_citer_1E_1B_itr_count = vec_int_citer_1E - vec_int_citer_1B;

    printf("vec_int_citer_1E - vec_int_citer_1B = itr_count %d, ptr_count %d\n",
            vec_int_citer_1E_1B_itr_count, vec_int_citer_1E_1B_ptr_count);
    printf("vec_int_citer_1E - vec_int_citer_1B = %d\n", vec_int_citer_1E_1B_itr_count);
    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());

    CHECK(vec_int_citer_1E_1B_itr_count == 3);
    CHECK(vec_int_citer_1E_1B_itr_count == vec_int_citer_1E_1B_ptr_count);

    CHECK( vec_int_citer_1E_str == jau::to_string(vec_int_citer_1E) );
}