File: from_chars_string_view.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (156 lines) | stat: -rw-r--r-- 4,347 bytes parent folder | download | duplicates (5)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/charconv.hpp>
#include <boost/config.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/detail/string_view.hpp>
#include <random>
#include <string>
#include <limits>
#include <iomanip>

#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
#  include <string_view>
#endif

static std::mt19937_64 rng(42);
constexpr std::size_t N = 1024;

template <typename T, typename StringViewType = boost::core::string_view>
void test_int()
{
    std::uniform_int_distribution<T> dist((std::numeric_limits<T>::min)(), (std::numeric_limits<T>::max)());

    for (std::size_t i = 0; i < N; ++i)
    {
        const auto value = dist(rng);
        std::string str_value = std::to_string(value);
        StringViewType sv = str_value;

        T v = 0;
        auto r = boost::charconv::from_chars(sv, v);
        BOOST_TEST(r);
        BOOST_TEST_EQ(v, value);
    }
}

template <typename T, typename StringViewType = boost::core::string_view>
void test_float()
{
    std::uniform_real_distribution<T> dist(T(-1e3), T(1e3));

    for (std::size_t i = 0; i < N; ++i)
    {
        const auto value = dist(rng);
        std::string str_value = std::to_string(value);
        StringViewType sv = str_value;

        T v = 0;
        auto r = boost::charconv::from_chars(sv, v);

        T v2 = 0;
        auto r2 = boost::charconv::from_chars(str_value.data(), str_value.data() + str_value.size(), v2);

        if( BOOST_TEST( r.ec == std::errc() ) && BOOST_TEST( r2.ec == std::errc() ) && BOOST_TEST( v == v2 ) )
        {
        }
        else
        {
            std::cerr << std::setprecision(std::numeric_limits<T>::digits10)
                      << "... test failure for value=" << value << "; buffer='" << str_value << "'; errc=" << static_cast<int>(r.ec) << "," << static_cast<int>(r2.ec) << std::endl; // LCOV_EXCL_LINE
        }
    }
}

int main()
{
    // MSVC does not allow (un)signed char in uniform_int_distribution
    // GCC and clang do
    #ifndef _MSC_VER
    test_int<signed char>();
    test_int<unsigned char>();
    #endif

    test_int<short>();
    test_int<unsigned short>();
    test_int<int>();
    test_int<unsigned>();
    test_int<long>();
    test_int<unsigned long>();
    test_int<long long>();
    test_int<unsigned long long>();

    #ifndef _MSC_VER
    test_int<signed char, std::string>();
    test_int<unsigned char, std::string>();
    #endif

    test_int<short, std::string>();
    test_int<unsigned short, std::string>();
    test_int<int, std::string>();
    test_int<unsigned, std::string>();
    test_int<long, std::string>();
    test_int<unsigned long, std::string>();
    test_int<long long, std::string>();
    test_int<unsigned long long, std::string>();

    #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)

    #ifndef _MSC_VER
    test_int<signed char, std::string_view>();
    test_int<unsigned char, std::string_view>();
    #endif

    test_int<short, std::string_view>();
    test_int<unsigned short, std::string_view>();
    test_int<int, std::string_view>();
    test_int<unsigned, std::string_view>();
    test_int<long, std::string_view>();
    test_int<unsigned long, std::string_view>();
    test_int<long long, std::string_view>();
    test_int<unsigned long long, std::string_view>();

    #endif

    test_float<float>();
    test_float<double>();

    test_float<float, std::string>();
    test_float<double, std::string>();

    #ifndef BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE
    test_float<long double>();
    test_float<long double, std::string>();
    #endif

    #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)

    test_float<float, std::string_view>();
    test_float<double, std::string_view>();

    #ifndef BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE
    test_float<long double, std::string_view>();
    #endif

    #endif

    #ifdef BOOST_CHARCONV_HAS_FLOAT32

    test_float<std::float32_t>();
    test_float<std::float32_t, std::string>();
    test_float<std::float32_t, std::string_view>();

    #endif

    #ifdef BOOST_CHARCONV_HAS_FLOAT64

    test_float<std::float64_t>();
    test_float<std::float64_t, std::string>();
    test_float<std::float64_t, std::string_view>();

    #endif

    return boost::report_errors();
}