File: formatted.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 (311 lines) | stat: -rw-r--r-- 9,120 bytes parent folder | download | duplicates (18)
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
// Boost.Range library
//
//  Copyright Neil Groves 2014. Use, modification and
//  distribution is subject to 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)
//
// For more information, see http://www.boost.org/libs/range/
//
#include <boost/range/adaptor/formatted.hpp>
#include <boost/cstdint.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

namespace boost_range_test
{
    namespace
    {

    template<typename T>
    std::string make_string(T x)
    {
        std::ostringstream result;
        result << x;
        return result.str();
    }

    template<typename T1, typename T2>
    std::string make_string(T1 x, T2 y)
    {
        std::ostringstream result;
        result << x << y;
        return result.str();
    }

std::string reference_result(const std::vector<boost::int32_t>& v,
                             const std::string& separator,
                             const std::string& prefix,
                             const std::string& postfix)
{
    std::ostringstream out;
    out << prefix;
    if (!v.empty())
    {
        out << v.at(0);
        std::vector<boost::int32_t>::const_iterator it = v.begin();
        for (++it; it != v.end(); ++it)
        {
            out << separator << *it;
        }
    }
    out << postfix;

    return out.str();
}

void test_formatted_0args_impl(const std::vector<boost::int32_t>& v)
{
    std::ostringstream out1;
    out1 << '[' << (v | boost::adaptors::formatted()) << ']';
    BOOST_CHECK_EQUAL(out1.str(), reference_result(v, ",", "[{", "}]"));

    std::ostringstream out2;
    out2 << '[' << boost::adaptors::format(v) << ']';
    BOOST_CHECK_EQUAL(out2.str(), reference_result(v, ",", "[{", "}]"));

    std::ostringstream out3;
    out3 << (v | boost::adaptors::formatted());
    BOOST_CHECK_EQUAL(out3.str(), reference_result(v, ",", "{", "}"));

    std::ostringstream out4;
    out4 << boost::adaptors::format(v);
    BOOST_CHECK_EQUAL(out4.str(), reference_result(v, ",", "{", "}"));
}

template<typename Sep>
void test_formatted_1arg_impl(
        const std::vector<boost::int32_t>& v,
        const Sep& sep)
{
    const std::string ref_sep = make_string(sep);
    std::ostringstream out1;
    out1 << '[' << (v | boost::adaptors::formatted(sep)) << ']';

    BOOST_CHECK_EQUAL(out1.str(), reference_result(v, ref_sep, "[{", "}]"));

    std::ostringstream out2;
    out2 << '[' << boost::adaptors::format(v, sep) << ']';
    BOOST_CHECK_EQUAL(out2.str(), reference_result(v, ref_sep, "[{", "}]"));

    std::ostringstream out3;
    out3 << (v | boost::adaptors::formatted(sep));
    BOOST_CHECK_EQUAL(out3.str(), reference_result(v, ref_sep, "{", "}"));

    std::ostringstream out4;
    out4 << boost::adaptors::format(v, sep);
    BOOST_CHECK_EQUAL(out4.str(), reference_result(v, ref_sep, "{", "}"));
}

void test_formatted_1arg_impl(const std::vector<boost::int32_t>& v)
{
    test_formatted_1arg_impl(v, ',');
    test_formatted_1arg_impl(v, ' ');
    test_formatted_1arg_impl<const char[3]>(v, ":?");
}

template<typename Sep, typename Prefix>
void test_formatted_2args_impl(
        const std::vector<boost::int32_t>& v,
        const Sep& sep,
        const Prefix& prefix
)
{
    const std::string ref_sep = make_string(sep);

    std::ostringstream out1;
    out1 << '[' << (v | boost::adaptors::formatted(sep, prefix)) << ']';
    BOOST_CHECK_EQUAL(
        out1.str(),
        reference_result(v, ref_sep, make_string('[', prefix), "}]"));

    std::ostringstream out2;
    out2 << '[' << boost::adaptors::format(v, sep, prefix) << ']';
    BOOST_CHECK_EQUAL(
        out2.str(),
        reference_result(v, ref_sep, make_string('[', prefix), "}]"));

    std::ostringstream out3;
    out3 << (v | boost::adaptors::formatted(sep, prefix));
    BOOST_CHECK_EQUAL(
        out3.str(),
        reference_result(v, ref_sep, make_string(prefix), "}"));

    std::ostringstream out4;
    out4 << boost::adaptors::format(v, sep, prefix);
    BOOST_CHECK_EQUAL(
        out4.str(),
        reference_result(v, ref_sep, make_string(prefix), "}"));
}

void test_formatted_2args_impl(const std::vector<boost::int32_t>& v)
{
    test_formatted_2args_impl(v, ',', '{');
    test_formatted_2args_impl(v, ':', '(');
    test_formatted_2args_impl<char, const char[3]>(v, ',', "{!");
    test_formatted_2args_impl<const char[3], char>(v, "#$", '{');
    test_formatted_2args_impl<const char[3], const char[3]>(v, "#$", "{!");
}

template<typename Sep, typename Prefix, typename Postfix>
void test_formatted_3args_impl(
        const std::vector<boost::int32_t>& v,
        const Sep& sep,
        const Prefix& prefix,
        const Postfix& postfix
)
{
    const std::string ref_sep = make_string(sep);

    std::ostringstream out1;
    out1 << '[' << (v | boost::adaptors::formatted(sep, prefix, postfix))
         << ']';
    BOOST_CHECK_EQUAL(
        out1.str(),
        reference_result(v, ref_sep, make_string('[', prefix),
                         make_string(postfix, ']')));
}

void test_formatted_3args_impl(const std::vector<boost::int32_t>& v)
{
    test_formatted_3args_impl(v, ',', '{', '}');
    test_formatted_3args_impl(v, ':', '(', ')');
    test_formatted_3args_impl<char, char, const char[3]>(v, ',', '{', "!}");
    test_formatted_3args_impl<char, const char[3], char>(v, ',', "{!", '}');
    test_formatted_3args_impl<const char[3], char, char>(v, "#$", '{', '}');
    test_formatted_3args_impl<
            const char[3], const char[3], const char[3]
    >(v, "#$", "{!", "!}");
}

void test_formatted_impl(const std::vector<boost::int32_t>& v)
{
    test_formatted_0args_impl(v);
    test_formatted_1arg_impl(v);
    test_formatted_2args_impl(v);
    test_formatted_3args_impl(v);
}

void test_formatted1()
{
    std::vector<boost::int32_t> v;
    for (boost::int32_t i = 0; i < 10; ++i)
        v.push_back(i);

    test_formatted_impl(v);
}

void test_formatted2()
{
    std::vector<boost::int32_t> v;
    v.push_back(3);

    test_formatted_impl(v);
}

void test_formatted3()
{
    std::vector<boost::int32_t> v;

    test_formatted_impl(v);
}

void test_formatted4()
{
    std::vector<boost::int32_t> v;
    for (boost::int32_t i = 0; i < 5; ++i)
        v.push_back(i);

    test_formatted_impl(v);
}

struct udt_separator
{
};

template<typename Char, typename Traits>
inline std::basic_ostream<Char,Traits>&
operator<<(std::basic_ostream<Char,Traits>& out, udt_separator)
{
    return out << "[sep]";
}

void test_formatted5()
{
    std::vector<boost::int32_t> v;
    for (boost::int32_t i = 0; i < 5; ++i)
        v.push_back(i);

    std::ostringstream out1;
    out1 << (v | boost::adaptors::formatted(udt_separator()));
    BOOST_CHECK_EQUAL(out1.str(), "{0[sep]1[sep]2[sep]3[sep]4}");

    std::ostringstream out2;
    out2 << boost::adaptors::format(v, udt_separator());
    BOOST_CHECK_EQUAL(out2.str(), "{0[sep]1[sep]2[sep]3[sep]4}");
}

// This test is already covered by the more complex code above. This
// code duplicates coverage to ensure that char literal arrays are handled
// correctly. I was particularly concerned that my test code above may pass
// erroneously by decaying a char literal to a pointer. This function makes
// it very plain that character literal strings work.
void test_formatted_empty()
{
    std::vector<boost::int32_t> v;

    std::ostringstream out1;
    out1 << (v | boost::adaptors::formatted());
    BOOST_CHECK_EQUAL(out1.str(), "{}");

    std::ostringstream out2;
    out2 << boost::adaptors::format(v);
    BOOST_CHECK_EQUAL(out2.str(), "{}");

    std::ostringstream out3;
    out3 << (v | boost::adaptors::formatted(','));
    BOOST_CHECK_EQUAL(out3.str(), "{}");

    std::ostringstream out4;
    out4 << boost::adaptors::format(v, ',');
    BOOST_CHECK_EQUAL(out4.str(), "{}");

    std::ostringstream out5;
    out5 << (v | boost::adaptors::formatted("#$"));
    BOOST_CHECK_EQUAL(out5.str(), "{}");

    std::ostringstream out6;
    out6 << boost::adaptors::format(v, "#$");
    BOOST_CHECK_EQUAL(out6.str(), "{}");

    std::ostringstream out7;
    out7 << (v | boost::adaptors::formatted("", "12", "34"));
    BOOST_CHECK_EQUAL(out7.str(), "1234");

    std::ostringstream out8;
    out8 << boost::adaptors::format(v, "", "12", "34");
    BOOST_CHECK_EQUAL(out8.str(), "1234");
}

    } // anonymous namespace
} // namespace boost_range_test

boost::unit_test::test_suite* init_unit_test_suite(int, char*[] )
{
    boost::unit_test::test_suite* test =
            BOOST_TEST_SUITE( "Boost.Range formatted test suite" );

    test->add(BOOST_TEST_CASE(&boost_range_test::test_formatted1));
    test->add(BOOST_TEST_CASE(&boost_range_test::test_formatted2));
    test->add(BOOST_TEST_CASE(&boost_range_test::test_formatted3));
    test->add(BOOST_TEST_CASE(&boost_range_test::test_formatted4));
    test->add(BOOST_TEST_CASE(&boost_range_test::test_formatted5));

    test->add(BOOST_TEST_CASE(&boost_range_test::test_formatted_empty));

    return test;
}