File: dyn_bitset_unit_tests4.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 (313 lines) | stat: -rw-r--r-- 12,893 bytes parent folder | download | duplicates (3)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// -----------------------------------------------------------
//              Copyright (c) 2001 Jeremy Siek
//         Copyright (c) 2003-2006, 2025 Gennaro Prota
//
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)
//
// -----------------------------------------------------------

#include "bitset_test.hpp"
#include "boost/config.hpp"
#include "boost/dynamic_bitset/dynamic_bitset.hpp"
#include <assert.h>
#include <cstddef>
#include <fstream>
#include <stdexcept>
#include <string>

#if ! defined( BOOST_NO_STRINGSTREAM )
#    include <sstream>
#endif

#if defined BOOST_NO_STD_WSTRING
#    define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
#endif

#if ! defined BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
std::wstring
widen_string( const std::string & str, const std::locale & loc = std::locale() )
{
    std::wstring                 result;
    const std::string::size_type len = str.length();
    if ( len != 0 ) {
        typedef std::ctype< wchar_t >     ct_type;
        typedef std::wstring::traits_type tr_type;
        const ct_type &                   ct = std::use_facet< ct_type >( loc );

        result.resize( len );
        for ( std::size_t i = 0; i < len; ++i )
            tr_type::assign( result[ i ], ct.widen( str[ i ] ) );
    }
    return result;
}
#endif

template< typename Block, typename AllocatorOrContainer = std::allocator< Block > >
void
run_test_cases()
{
    typedef boost::dynamic_bitset< Block, AllocatorOrContainer > bitset_type;
    typedef bitset_test< bitset_type >     Tests;

    //=====================================================================
    // Test stream operator<<
    {
        // The test "variables" are: the stream type and its state, the
        // exception mask, the width, the fill char and the padding side (left/right)

        std::ios::iostate masks[] = {
            std::ios::goodbit,
            std::ios::eofbit,
            std::ios::failbit,
            std::ios::eofbit | std::ios::failbit
        };

        static std::string strings[] = {
            std::string( "" ),
            std::string( "0" ),
            std::string( "1" ),
            std::string( "11100" ),
            get_long_string()
        };

        char         fill_chars[] = { '*', 'x', ' ' };

        std::size_t  num_masks    = sizeof( masks ) / sizeof( masks[ 0 ] );
        std::size_t  num_strings  = sizeof( strings ) / sizeof( strings[ 0 ] );
        std::size_t  num_chars    = sizeof( fill_chars ) / sizeof( fill_chars[ 0 ] );

        std::fstream not_good_stream( "dynamic_bitset_tests - this file shouldn't exist", std::ios::in );

        for ( std::size_t mi = 0; mi < num_masks; ++mi ) {
            for ( std::size_t si = 0; si < num_strings; ++si ) {
                std::streamsize slen = (std::streamsize)( strings[ si ].length() );

                assert( ( std::numeric_limits< std::streamsize >::max )() >= (std::streamsize)( 1 + slen * 2 ) );

                for ( std::size_t ci = 0; ci < num_chars; ++ci ) {
                    // note how "negative widths" are tested too
                    const std::streamsize widths[]   = { -1 - slen / 2, 0, slen / 2, 1 + slen * 2 };
                    std::size_t           num_widths = sizeof( widths ) / sizeof( widths[ 0 ] );

                    for ( std::size_t wi = 0; wi < num_widths; ++wi ) {
                        std::streamsize w = widths[ wi ];
                        {
                            // test 0 - stream !good()
                            if ( not_good_stream.good() )
                                throw std::logic_error( "Error in operator << tests"
                                                        " - please, double check" );
                            bitset_type b( strings[ si ] );
                            not_good_stream.width( w );
                            not_good_stream.fill( fill_chars[ ci ] );
                            try {
                                not_good_stream.exceptions( masks[ mi ] );
                            } catch ( ... ) {
                            }

                            Tests::stream_inserter( b, not_good_stream, "<unused_string>" );
                        }
                        {
                            // test 1a - file stream
                            scoped_temp_file stf;
                            bitset_type      b( strings[ si ] );
                            std::ofstream    file( stf.path().string().c_str(), std::ios::trunc );
                            file.width( w );
                            file.fill( fill_chars[ ci ] );
                            file.exceptions( masks[ mi ] );
                            Tests::stream_inserter( b, file, stf.path().string().c_str() );
                        }
                        {
                            // NOTE: there are NO string stream tests
                        }
#if ! defined( BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS )
                        {
                            // test 1b - wide file stream
                            scoped_temp_file stf;
                            bitset_type      b( strings[ si ] );
                            std::wofstream   file( stf.path().string().c_str() );
                            file.width( w );
                            file.fill( fill_chars[ ci ] );
                            file.exceptions( masks[ mi ] );
                            Tests::stream_inserter( b, file, stf.path().string().c_str() );
                        }
#endif
                    }
                }
            }
        } // for (; mi..)
    }

    //=====================================================================
    // Test stream operator>>
    {
        // The test "variables" are: the stream type, the exception mask,
        // the actual contents (and/or state) of the stream, and width.
        //
        // With few exceptions, each test case consists of writing a different
        // assortment of digits and "whitespaces" to a text stream and then checking
        // that what was written gets read back unchanged. That's NOT guaranteed by
        // the standard, unless the assortment always ends with a '\n' and satisfies
        // other conditions (see C99, 7.19.2/2), however it works in practice and is
        // a good "real life" test. Some characters, such as '\v' and '\f', are not
        // used exactly because they are the ones which will most likely give problems
        // on some systems (for instance '\f' could actually be written as a sequence
        // of new-lines, and we could never be able to read it back)
        //
        // Note how the bitset object is not initially empty. That helps checking
        // that it isn't erroneously clear()ed by operator>>.

        std::ios::iostate masks[] = {
            std::ios::goodbit,
            std::ios::eofbit,
            std::ios::failbit,
            std::ios::eofbit | std::ios::failbit
        };

        const std::string        spaces      = "\t\n "; //"\t\n\v\f ";

        const std::string        long_string = get_long_string();
        const static std::string strings[]   = {

            // empty string
            std::string( "" ),
            // no bitset
            spaces,

            // no bitset
            std::string( "x" ),
            std::string( "\t  xyz" ),

            // bitset of size 1
            std::string( "0" ),
            std::string( "1" ),

            std::string( "  0  " ),
            std::string( "  1  " ),
            spaces + "1",
            "1" + spaces,
            spaces + "1" + spaces,
            std::string( "  x1x  " ),
            std::string( "  1x  " ),

            // long bitset
            long_string,
            "  " + long_string + " xyz",
            spaces + long_string,
            spaces + long_string + spaces
        };

        //-----------------------------------------------------

        std::stringstream not_good_stream;
        not_good_stream << "test";
        std::string sink;
        not_good_stream >> sink; // now the stream should be in eof state

        const std::size_t num_masks   = sizeof( masks ) / sizeof( masks[ 0 ] );
        const std::size_t num_strings = sizeof( strings ) / sizeof( strings[ 0 ] );

        for ( std::size_t mi = 0; mi < num_masks; ++mi ) {
            for ( std::size_t si = 0; si < num_strings; ++si ) {
                const std::streamsize slen = (std::streamsize)( strings[ si ].length() );
                assert( ( std::numeric_limits< std::streamsize >::max )() >= (std::streamsize)( 1 + slen * 2 ) );

                std::streamsize widths[]   = { -1, 0, slen / 2, slen, 1 + slen * 2 };
                std::size_t     num_widths = sizeof( widths ) / sizeof( widths[ 0 ] );

                for ( std::size_t wi = 0; wi < num_widths; ++wi ) {
                    const std::streamsize w = widths[ wi ];

                    // test 0 - !good() stream
                    {
                        if ( not_good_stream.good() )
                            throw std::logic_error( "Error in operator >> tests"
                                                    " - please, double check" );
                        bitset_type b( 1, 15ul ); // note: b is not empty
                        not_good_stream.width( w );
                        try {
                            not_good_stream.exceptions( masks[ mi ] );
                        } catch ( ... ) {
                        }
                        std::string irrelevant;
                        Tests::stream_extractor( b, not_good_stream, irrelevant );
                    }
                    // test 1a - (narrow) file stream
                    {
                        scoped_temp_file stf;
                        bitset_type      b( 1, 255ul );
                        {
                            std::ofstream f( stf.path().string().c_str() );
                            f << strings[ si ];
                        }

                        std::ifstream f( stf.path().string().c_str() );
                        f.width( w );
                        f.exceptions( masks[ mi ] );
                        Tests::stream_extractor( b, f, strings[ si ] );
                    }
#if ! defined( BOOST_NO_STRINGSTREAM )
                    // test 2a - stringstream
                    {
                        bitset_type        b( 1, 255ul );
                        std::istringstream stream( strings[ si ] );
                        stream.width( w );
                        stream.exceptions( masks[ mi ] );
                        Tests::stream_extractor( b, stream, strings[ si ] );
                    }
#endif

#if ! defined( BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS )
                    // test 1b - wchar_t file stream
                    {
                        scoped_temp_file stf;
                        std::wstring     wstr = widen_string( strings[ si ] );
                        bitset_type      b( 1, 255ul );
                        {
                            std::basic_ofstream< wchar_t > of( stf.path().string().c_str() );
                            of << wstr;
                        }

                        std::basic_ifstream< wchar_t > f( stf.path().string().c_str() );
                        f.width( w );
                        f.exceptions( masks[ mi ] );
                        Tests::stream_extractor( b, f, wstr );
                    }
                    // test 2b - wstringstream
                    {
                        bitset_type         b( 1, 255ul );
                        std::wstring        wstr = widen_string( strings[ si ] );

                        std::wistringstream wstream( wstr );
                        wstream.width( w );
                        wstream.exceptions( masks[ mi ] );
                        Tests::stream_extractor( b, wstream, wstr );
                    }
#endif // BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
                }
            }

        } // for ( mi = 0; ...)
    }
    //=====================================================================
    // << Any other tests go here >>
    //         .....
}

int
main()
{
    run_test_cases< unsigned char >();
    run_test_cases< unsigned char, small_vector< unsigned char > >();
    run_test_cases< unsigned short >();
    run_test_cases< unsigned short, small_vector< unsigned short > >();
    run_test_cases< unsigned int >();
    run_test_cases< unsigned int, small_vector< unsigned int > >();
    run_test_cases< unsigned long >();
    run_test_cases< unsigned long, small_vector< unsigned long > >();
    run_test_cases< unsigned long long >();
    run_test_cases< unsigned long long, small_vector< unsigned long long > >();

    return boost::report_errors();
}