• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #846

05 Dec 2022 11:16PM UTC coverage: 86.482% (+0.8%) from 85.664%
#846

push

StellarBot
Merge #6093

6093: Replace boost::string_ref with std::string_view r=hkaiser a=hkaiser

Working towards #5497 and #3440

Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

14 of 14 new or added lines in 6 files covered. (100.0%)

172759 of 199764 relevant lines covered (86.48%)

1842530.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.46
/libs/core/datastructures/tests/unit/dynamic_bitset4.cpp
1
//  Copyright (c) 2022 Hartmut Kaiser
2
//
3
//  SPDX-License-Identifier: BSL-1.0 Distributed under the Boost Software
4
//  License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5
//  http://www.boost.org/LICENSE_1_0.txt)
6
//
7
// This code was adapted from boost dynamic_bitset
8
//
9
// Copyright (c) 2001 Jeremy Siek
10
// Copyright (c) 2003-2006 Gennaro Prota
11

12
#include <hpx/config.hpp>
13
#include <hpx/assert.hpp>
14
#include <hpx/datastructures/detail/dynamic_bitset.hpp>
15

16
#include <cstddef>    // for std::size_t
17
#include <fstream>
18
#include <locale>
19
#include <sstream>
20
#include <stdexcept>    // for std::logic_error
21
#include <string>
22

23
#include "bitset_test.hpp"
24

25
std::wstring widen_string(
×
26
    std::string const& str, std::locale const& loc = std::locale())
27
{
28
    std::wstring result;
×
29
    std::string::size_type const len = str.length();
×
30
    if (len != 0)
×
31
    {
32
        typedef std::ctype<wchar_t> ct_type;
33
        typedef std::wstring::traits_type tr_type;
34
        ct_type const& ct = std::use_facet<ct_type>(loc);
×
35

36
        result.resize(len);
×
37
        for (std::size_t i = 0; i < len; ++i)
×
38
            tr_type::assign(result[i], ct.widen(str[i]));
×
39
    }
×
40
    return result;
×
41
}
×
42

43
template <typename Block>
44
void run_test_cases()
5✔
45
{
46
    typedef hpx::detail::dynamic_bitset<Block> bitset_type;
47
    typedef bitset_test<bitset_type> Tests;
48

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

55
        std::ios::iostate masks[] = {std::ios::goodbit, std::ios::eofbit,
5✔
56
            std::ios::failbit, std::ios::eofbit | std::ios::failbit};
57

58
        static std::string strings[] = {std::string(""), std::string("0"),
5✔
59
            std::string("1"), std::string("11100"), get_long_string()};
30✔
60

61
        char fill_chars[] = {'*', 'x', ' '};
5✔
62

63
        std::size_t num_masks = sizeof(masks) / sizeof(masks[0]);
5✔
64
        std::size_t num_strings = sizeof(strings) / sizeof(strings[0]);
5✔
65
        std::size_t num_chars = sizeof(fill_chars) / sizeof(fill_chars[0]);
5✔
66

67
        std::fstream not_good_stream(
5✔
68
            "dynamic_bitset_tests - this file shouldn't exist", std::ios::in);
69

70
        for (std::size_t mi = 0; mi < num_masks; ++mi)
25✔
71
        {
72
            for (std::size_t si = 0; si < num_strings; ++si)
120✔
73
            {
74
                std::streamsize slen = (std::streamsize)(strings[si].length());
100✔
75

76
                HPX_ASSERT((std::numeric_limits<std::streamsize>::max)() >=
100✔
77
                    (std::streamsize)(1 + slen * 2));
78

79
                for (std::size_t ci = 0; ci < num_chars; ++ci)
400✔
80
                {
81
                    // note how "negative widths" are tested too
82
                    std::streamsize const widths[] = {
1,200✔
83
                        -1 - slen / 2, 0, slen / 2, 1 + slen * 2};
900✔
84
                    std::size_t num_widths = sizeof(widths) / sizeof(widths[0]);
300✔
85

86
                    for (std::size_t wi = 0; wi < num_widths; ++wi)
1,500✔
87
                    {
88
                        std::streamsize w = widths[wi];
1,200✔
89
                        {
90
                            // test 0 - stream !good()
91
                            if (not_good_stream.good())
1,200✔
92
                                throw std::logic_error(
×
93
                                    "Error in operator << tests"
94
                                    " - please, double check");
95
                            bitset_type b(strings[si]);
1,200✔
96
                            not_good_stream.width(w);
1,200✔
97
                            not_good_stream.fill(fill_chars[ci]);
1,200✔
98
                            try
99
                            {
100
                                not_good_stream.exceptions(masks[mi]);
1,200✔
101
                            }
1,200✔
102
                            catch (...)
103
                            {
104
                            }
600✔
105

106
                            Tests::stream_inserter(
1,200✔
107
                                b, not_good_stream, "<unused_string>");
108
                        }
1,200✔
109
#if !defined(HPX_GCC_VERSION)
110
                        {
111
                            // test 1a - file stream
112
                            scoped_temp_file stf;
113
                            bitset_type b(strings[si]);
114
                            std::ofstream file(
115
                                stf.path().string().c_str(), std::ios::trunc);
116
                            file.width(w);
117
                            file.fill(fill_chars[ci]);
118
                            file.exceptions(masks[mi]);
119
                            Tests::stream_inserter(
120
                                b, file, stf.path().string().c_str());
121
                        }
122
                        {
123
                            //NOTE: there are NO string stream tests
124
                        } {
125
                            // test 1b - wide file stream
126
                            scoped_temp_file stf;
127
                            bitset_type b(strings[si]);
128
                            std::wofstream file(stf.path().string().c_str());
129
                            file.width(w);
130
                            file.fill(fill_chars[ci]);
131
                            file.exceptions(masks[mi]);
132
                            Tests::stream_inserter(
133
                                b, file, stf.path().string().c_str());
134
                        }
135
#endif
136
                    }
1,200✔
137
                }
300✔
138
            }
100✔
139
        }    // for (; mi..)
20✔
140
    }
5✔
141

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

161
        std::ios::iostate masks[] = {std::ios::goodbit, std::ios::eofbit,
5✔
162
            std::ios::failbit, std::ios::eofbit | std::ios::failbit};
163

164
        std::string const spaces = "\t\n ";    //"\t\n\v\f ";
5✔
165

166
        std::string const long_string = get_long_string();
5✔
167
        static std::string strings[] = {// empty string
20✔
168
            std::string(""),
5✔
169
            // no bitset
170
            spaces,
5✔
171
            // no bitset
172
            std::string("x"), std::string("\t  xyz"),
5✔
173

174
            // bitset of size 1
175
            std::string("0"), std::string("1"),
5✔
176

177
            std::string("  0  "), std::string("  1  "), spaces + "1",
5✔
178
            "1" + spaces, spaces + "1" + spaces, std::string("  x1x  "),
5✔
179
            std::string("  1x  "),
5✔
180

181
            // long bitset
182
            long_string, "  " + long_string + " xyz", spaces + long_string,
5✔
183
            spaces + long_string + spaces};
5✔
184

185
        //-----------------------------------------------------
186

187
        std::stringstream not_good_stream;
5✔
188
        not_good_stream << "test";
5✔
189
        std::string sink;
5✔
190
        not_good_stream >> sink;    // now the stream should be in eof state
5✔
191

192
        std::size_t const num_masks = sizeof(masks) / sizeof(masks[0]);
5✔
193
        std::size_t const num_strings = sizeof(strings) / sizeof(strings[0]);
5✔
194

195
        for (std::size_t mi = 0; mi < num_masks; ++mi)
25✔
196
        {
197
            for (std::size_t si = 0; si < num_strings; ++si)
360✔
198
            {
199
                std::streamsize const slen =
340✔
200
                    (std::streamsize)(strings[si].length());
340✔
201
                HPX_ASSERT((std::numeric_limits<std::streamsize>::max)() >=
340✔
202
                    (std::streamsize)(1 + slen * 2));
203

204
                std::streamsize widths[] = {
1,360✔
205
                    -1, 0, slen / 2, slen, 1 + slen * 2};
1,020✔
206
                std::size_t num_widths = sizeof(widths) / sizeof(widths[0]);
340✔
207

208
                for (std::size_t wi = 0; wi < num_widths; ++wi)
2,040✔
209
                {
210
                    std::streamsize const w = widths[wi];
1,700✔
211

212
                    // test 0 - !good() stream
213
                    {
214
                        if (not_good_stream.good())
1,700✔
215
                            throw std::logic_error("Error in operator >> tests"
×
216
                                                   " - please, double check");
217
                        bitset_type b(1, 15ul);    // note: b is not empty
1,700✔
218
                        not_good_stream.width(w);
1,700✔
219
                        try
220
                        {
221
                            not_good_stream.exceptions(masks[mi]);
1,700✔
222
                        }
1,700✔
223
                        catch (...)
224
                        {
225
                        }
1,275✔
226
                        std::string irrelevant;
1,700✔
227
                        Tests::stream_extractor(b, not_good_stream, irrelevant);
1,700✔
228
                    }
1,700✔
229
#if !defined(HPX_GCC_VERSION)
230
                    // test 1a - (narrow) file stream
231
                    {
232
                        scoped_temp_file stf;
233
                        bitset_type b(1, 255ul);
234
                        {
235
                            std::ofstream f(stf.path().string().c_str());
236
                            f << strings[si];
237
                        }
238

239
                        std::ifstream f(stf.path().string().c_str());
240
                        f.width(w);
241
                        f.exceptions(masks[mi]);
242
                        Tests::stream_extractor(b, f, strings[si]);
243
                    }
244
                    // test 2a - stringstream
245
                    {
246
                        bitset_type b(1, 255ul);
247
                        std::istringstream stream(strings[si]);
248
                        stream.width(w);
249
                        stream.exceptions(masks[mi]);
250
                        Tests::stream_extractor(b, stream, strings[si]);
251
                    }
252

253
                    // test 1b - wchar_t file stream
254
                    {
255
                        scoped_temp_file stf;
256
                        std::wstring wstr = widen_string(strings[si]);
257
                        bitset_type b(1, 255ul);
258
                        {
259
                            std::basic_ofstream<wchar_t> of(
260
                                stf.path().string().c_str());
261
                            of << wstr;
262
                        }
263

264
                        std::basic_ifstream<wchar_t> f(
265
                            stf.path().string().c_str());
266
                        f.width(w);
267
                        f.exceptions(masks[mi]);
268
                        Tests::stream_extractor(b, f, wstr);
269
                    }
270
                    // test 2b - wstringstream
271
                    {
272
                        bitset_type b(1, 255ul);
273
                        std::wstring wstr = widen_string(strings[si]);
274

275
                        std::wistringstream wstream(wstr);
276
                        wstream.width(w);
277
                        wstream.exceptions(masks[mi]);
278
                        Tests::stream_extractor(b, wstream, wstr);
279
                    }
280
#endif
281
                }
1,700✔
282
            }
340✔
283
        }    // for ( mi = 0; ...)
20✔
284
    }
5✔
285
}
1,965✔
286

287
int main()
1✔
288
{
289
    run_test_cases<unsigned char>();
1✔
290
    run_test_cases<unsigned short>();
1✔
291
    run_test_cases<unsigned int>();
1✔
292
    run_test_cases<unsigned long>();
1✔
293
    run_test_cases<unsigned long long>();
1✔
294

295
    return hpx::util::report_errors();
1✔
296
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc