File: limits.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 (260 lines) | stat: -rw-r--r-- 7,564 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2023 Matt Borland
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/config.hpp>

#ifdef BOOST_HAS_INT128

// We need to define these operator<< overloads before
// including boost/core/lightweight_test.hpp, or they
// won't be visible to BOOST_TEST_EQ
// LCOV_EXCL_START

#include <ostream>

static char* mini_to_chars( char (&buffer)[ 64 ], boost::uint128_type v )
{
    char* p = buffer + 64;
    *--p = '\0';

    do
    {
        *--p = "0123456789"[ v % 10 ];
        v /= 10;
    }
    while ( v != 0 );

    return p;
}

std::ostream& operator<<( std::ostream& os, boost::uint128_type v )
{
    char buffer[ 64 ];

    os << mini_to_chars( buffer, v );
    return os;
}

std::ostream& operator<<( std::ostream& os, boost::int128_type v )
{
    char buffer[ 64 ];
    char* p;

    if( v >= 0 )
    {
        p = mini_to_chars( buffer, static_cast<boost::uint128_type>(v) );
    }
    else
    {
        p = mini_to_chars( buffer, -static_cast<boost::uint128_type>(v) );
        *--p = '-';
    }

    os << p;
    return os;
}

// LCOV_EXCL_STOP

#endif // #ifdef BOOST_HAS_INT128

#include <boost/charconv/limits.hpp>
#include <boost/charconv/to_chars.hpp>
#include <boost/charconv/from_chars.hpp>
#include <boost/core/lightweight_test.hpp>
#include <system_error>
#include <limits>
#include <string>

void test_odr_use( int const* );

template<typename T> void test_integral( T value )
{
    // no base
    {
        char buffer[ boost::charconv::limits<T>::max_chars10 ];
        auto r = boost::charconv::to_chars( buffer, buffer + sizeof( buffer ), value );
        BOOST_TEST(r.ec == std::errc());

        T v2 = 0;
        auto r2 = boost::charconv::from_chars( buffer, r.ptr, v2 );

        if( BOOST_TEST( r2.ec == std::errc() ) && BOOST_TEST( v2 == value ) )
        {
        }
        else
        {
            std::cerr << "... test failure for value=" << value << "; buffer='" << std::string( buffer, r.ptr ) << "'" << std::endl; // LCOV_EXCL_LINE
        }
    }

    // base 10
    {
        char buffer[ boost::charconv::limits<T>::max_chars10 ];
        auto r = boost::charconv::to_chars( buffer, buffer + sizeof( buffer ), value, 10 );
        BOOST_TEST(r.ec == std::errc());

        T v2 = 0;
        auto r2 = boost::charconv::from_chars( buffer, r.ptr, v2, 10 );

        if( BOOST_TEST( r2.ec == std::errc() ) && BOOST_TEST( v2 == value ) )
        {
        }
        else
        {
            std::cerr << "... test failure for value=" << value << "; buffer='" << std::string( buffer, r.ptr ) << "'" << std::endl; // LCOV_EXCL_LINE
        }
    }

    // any base
    for( int base = 2; base <= 36; ++base )
    {
        char buffer[ boost::charconv::limits<T>::max_chars ];
        auto r = boost::charconv::to_chars( buffer, buffer + sizeof( buffer ), value, base );
        BOOST_TEST(r.ec == std::errc());

        T v2 = 0;
        auto r2 = boost::charconv::from_chars( buffer, r.ptr, v2, base );

        if( BOOST_TEST( r2.ec == std::errc() ) && BOOST_TEST( v2 == value ) )
        {
        }
        else
        {
            std::cerr << "... test failure for value=" << value << "; buffer='" << std::string( buffer, r.ptr ) << "'" << std::endl; // LCOV_EXCL_LINE
        }
    }
}

template<typename T> void test_integral()
{
    BOOST_TEST_GE( boost::charconv::limits<T>::max_chars10, std::numeric_limits<T>::digits10 );
    BOOST_TEST_GE( boost::charconv::limits<T>::max_chars, std::numeric_limits<T>::digits );

    test_odr_use( &boost::charconv::limits<T>::max_chars10 );
    test_odr_use( &boost::charconv::limits<T>::max_chars );

    test_integral( (std::numeric_limits<T>::min)() );
    test_integral( (std::numeric_limits<T>::max)() );
}

template<typename T> void test_floating_point( T value )
{
    // no base, max_chars10
    {
        char buffer[ boost::charconv::limits<T>::max_chars10 ];
        auto r = boost::charconv::to_chars( buffer, buffer + sizeof( buffer ), value );
        if (!BOOST_TEST(r.ec == std::errc()))
        {
            // LCOV_EXCL_START
            std::cerr << " Value: " << value
                      << "\nBuffer: " << std::string(buffer)
                      << "\n    Ec: " << static_cast<int>(r.ec) << std::endl;
            // LCOV_EXCL_STOP
        }

        T v2 = 0;
        auto r2 = boost::charconv::from_chars( buffer, r.ptr, v2 );

        if (!BOOST_TEST(r2.ec == std::errc()) && BOOST_TEST_EQ( v2, value ))
        {
            // LCOV_EXCL_START
            std::cerr << " Value: " << value
                      << "\nBuffer: " << std::string(buffer)
                      << "\nRetVal: " << v2
                      << "\n    Ec: " << static_cast<int>(r2.ec) << std::endl;
            // LCOV_EXCL_STOP
        }
    }

    // no base, max_chars
    {
        char buffer[ boost::charconv::limits<T>::max_chars ];
        auto r = boost::charconv::to_chars( buffer, buffer + sizeof( buffer ), value );
        if (!BOOST_TEST(r.ec == std::errc()))
        {
            // LCOV_EXCL_START
            std::cerr << " Value: " << value
                      << "\nBuffer: " << std::string(buffer)
                      << "\n    Ec: " << static_cast<int>(r.ec) << std::endl;
            // LCOV_EXCL_STOP
        }

        T v2 = 0;
        auto r2 = boost::charconv::from_chars( buffer, r.ptr, v2 );

        if (!BOOST_TEST(r2.ec == std::errc()) && BOOST_TEST_EQ( v2, value ))
        {
            // LCOV_EXCL_START
            std::cerr << " Value: " << value
                      << "\nBuffer: " << std::string(buffer)
                      << "\nRetVal: " << v2
                      << "\n    Ec: " << static_cast<int>(r2.ec) << std::endl;
            // LCOV_EXCL_STOP
        }
    }
}

template<typename T> void test_floating_point()
{
    BOOST_TEST_GE( boost::charconv::limits<T>::max_chars10, std::numeric_limits<T>::max_digits10 );
    BOOST_TEST_GE( boost::charconv::limits<T>::max_chars, std::numeric_limits<T>::max_digits10 );

    test_odr_use( &boost::charconv::limits<T>::max_chars10 );
    test_odr_use( &boost::charconv::limits<T>::max_chars );

    test_floating_point( (std::numeric_limits<T>::min)() );
    test_floating_point( -(std::numeric_limits<T>::min)() );
    test_floating_point( (std::numeric_limits<T>::max)() );
    test_floating_point( -(std::numeric_limits<T>::max)() );
}

int main()
{
    test_integral<char>();
    test_integral<signed char>();
    test_integral<unsigned char>();
    test_integral<short>();
    test_integral<unsigned short>();
    test_integral<int>();
    test_integral<unsigned>();
    test_integral<long>();
    test_integral<unsigned long>();
    test_integral<long long>();
    test_integral<unsigned long long>();

    test_floating_point<float>();
    test_floating_point<double>();

    #ifndef BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE
    test_floating_point<long double>();
    #endif

    #ifdef BOOST_CHARCONV_HAS_FLOAT16
    test_floating_point<std::float16_t>();
    #endif
    #ifdef BOOST_CHARCONV_HAS_FLOAT32
    test_floating_point<std::float32_t>();
    #endif
    #ifdef BOOST_CHARCONV_HAS_FLOAT64
    test_floating_point<std::float64_t>();
    #endif
    #ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
    test_floating_point<std::bfloat16_t>();
    #endif

#ifdef BOOST_CHARCONV_HAS_INT128

    test_integral<boost::int128_type>();
    test_integral<boost::uint128_type>();

#endif

    return boost::report_errors();
}

void test_odr_use( int const* )
{
}