File: util_manip_invoke.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 (152 lines) | stat: -rw-r--r-- 4,429 bytes parent folder | download | duplicates (11)
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
/*
 *             Copyright Andrey Semashev 2022.
 * 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)
 */
/*!
 * \file   util_manip_invoke.cpp
 * \author Andrey Semashev
 * \date   27.02.2022
 *
 * \brief  This header contains tests for the invoke manipulator.
 */

#define BOOST_TEST_MODULE util_manip_invoke

#include <string>
#include <sstream>
#include <boost/config.hpp>
#include <boost/core/ref.hpp>
#include <boost/bind/bind.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/log/utility/manipulators/invoke.hpp>
#include "char_definitions.hpp"

namespace logging = boost::log;

struct my_function0
{
    typedef void result_type;

    template< typename StreamT >
    result_type operator() (StreamT& stream) const
    {
        stream << "my_function0";
    }
};

struct my_function1
{
    typedef void result_type;

    template< typename StreamT >
    result_type operator() (StreamT& stream, int n) const
    {
        stream << "my_function1(" << n << ")";
    }
};

template< typename StreamT >
void free_function(StreamT& stream, int n)
{
    stream << "free_function(" << n << ")";
}

BOOST_AUTO_TEST_CASE_TEMPLATE(invoke_my_function0, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;

    ostream_type strm_dump;
    strm_dump << logging::invoke_manip(my_function0());

    ostream_type strm_correct;
    my_function0()(strm_correct);

    BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
}

BOOST_AUTO_TEST_CASE_TEMPLATE(invoke_boost_bind, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;

    {
        ostream_type strm_dump;
        strm_dump << logging::invoke_manip(boost::bind(my_function1(), boost::placeholders::_1, 10));

        ostream_type strm_correct;
        boost::bind(my_function1(), boost::placeholders::_1, 10)(strm_correct);

        BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
    }
    {
        ostream_type strm_dump;
        strm_dump << logging::invoke_manip(boost::bind(&free_function< ostream_type >, boost::placeholders::_1, 10));

        ostream_type strm_correct;
        boost::bind(&free_function< ostream_type >, boost::placeholders::_1, 10)(strm_correct);

        BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
    }
}

#if !defined(BOOST_NO_CXX11_LAMBDAS)

BOOST_AUTO_TEST_CASE_TEMPLATE(invoke_lambda, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;

    ostream_type strm_dump;
    strm_dump << logging::invoke_manip([](ostream_type& strm) { strm << "lambda"; });

    ostream_type strm_correct;
    strm_correct << "lambda";

    BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
}

#endif // !defined(BOOST_NO_CXX11_LAMBDAS)

// This list of config macros matches the similar list in boost/log/utility/manipulators/invoke.hpp
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
    !defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) && \
    !defined(BOOST_NO_CXX14_RETURN_TYPE_DEDUCTION)

BOOST_AUTO_TEST_CASE_TEMPLATE(invoke_lambda_args, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;

    ostream_type strm_dump;
    strm_dump << logging::invoke_manip([](ostream_type& strm, int x, int y) { strm << "lambda(" << x << ", " << y << ")"; }, 10, 20);

    ostream_type strm_correct;
    strm_correct << "lambda(" << 10 << ", " << 20 << ")";

    BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
}

BOOST_AUTO_TEST_CASE_TEMPLATE(invoke_lambda_args_ref, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_ostringstream< char_type > ostream_type;

    int external_var = 30;
    ostream_type strm_dump;
    strm_dump << logging::invoke_manip([](ostream_type& strm, int x, int y, int& z)
    {
        strm << "lambda(" << x << ", " << y << ", " << z << ")";
        ++z;
    }, 10, 20, boost::ref(external_var));

    ostream_type strm_correct;
    strm_correct << "lambda(" << 10 << ", " << 20 << ", " << 30 << ")";

    BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
    BOOST_CHECK_EQUAL(external_var, 31);
}

#endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) ...