File: util_manip_optional.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (179 lines) | stat: -rw-r--r-- 4,696 bytes parent folder | download | duplicates (8)
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
/*
 *             Copyright Andrey Semashev 2020.
 * 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_optional.cpp
 * \author Andrey Semashev
 * \date   12.05.2020
 *
 * \brief  This header contains tests for the optional manipulator.
 */

#define BOOST_TEST_MODULE util_manip_optional

#include <string>
#include <sstream>
#include <boost/config.hpp>
#include <boost/optional.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/log/utility/manipulators/optional.hpp>
#include "char_definitions.hpp"

namespace logging = boost::log;

struct my_int_wrapper
{
    int n;

    my_int_wrapper(int x = 0) BOOST_NOEXCEPT : n(x) {}
};

template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, my_int_wrapper miw)
{
    if (BOOST_LIKELY(strm.good()))
        strm << '{' << miw.n << '}';

    return strm;
}

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

    boost::optional< int > data;

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data);

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

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

    boost::optional< int > data;

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data, '-');

    ostream_type strm_correct;
    strm_correct << "-";

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

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

    boost::optional< int > data;

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data, "[none]");

    ostream_type strm_correct;
    strm_correct << "[none]";

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

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

    boost::optional< int > data;

    string_type marker;
    marker.push_back('[');
    marker.push_back('n');
    marker.push_back('o');
    marker.push_back('n');
    marker.push_back('e');
    marker.push_back(']');

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data, marker);

    ostream_type strm_correct;
    strm_correct << "[none]";

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

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

    const int* data = 0;

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data, "[none]");

    ostream_type strm_correct;
    strm_correct << "[none]";

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

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

    boost::optional< int > data;
    data = 7;

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data, "[none]");

    ostream_type strm_correct;
    strm_correct << "7";

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

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

    int n = 7;
    int* data = &n;

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data, "[none]");

    ostream_type strm_correct;
    strm_correct << "7";

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

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

    my_int_wrapper n(7);
    const my_int_wrapper* data = &n;

    ostream_type strm_dump;
    strm_dump << logging::optional_manip(data, "[none]");

    ostream_type strm_correct;
    strm_correct << "{7}";

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