summaryrefslogtreecommitdiff
path: root/libs/algorithm/string/test/trim_test.cpp
blob: b254caaa44e120bd1afb1c9b0c8f544c7f7e9ba1 (plain)
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
//  Boost string_algo library trim_test.cpp file  ---------------------------//

//  Copyright Pavol Droba 2002-2003. 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)

//  See http://www.boost.org for updates, documentation, and revision history.

#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/trim_all.hpp>

// Include unit test framework
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

#include <string>
#include <iostream>
#include <boost/test/test_tools.hpp>

using namespace std;
using namespace boost;

void trim_test()
{
    string str1("     1x x x x1     ");
    string str2("     2x x x x2     ");
    string str3("    ");

    // *** value passing tests *** //

    // general string test
    BOOST_CHECK( trim_left_copy( str1 )=="1x x x x1     " ) ;
    BOOST_CHECK( trim_right_copy( str1 )=="     1x x x x1" ) ;
    BOOST_CHECK( trim_copy( str1 )=="1x x x x1" ) ;

    // spaces-only string test
    BOOST_CHECK( trim_left_copy( str3 )=="" );
    BOOST_CHECK( trim_right_copy( str3 )=="" );
    BOOST_CHECK( trim_copy( str3 )=="" );

    // empty string check 
    BOOST_CHECK( trim_left_copy( string("") )=="" );
    BOOST_CHECK( trim_right_copy( string("") )=="" );
    BOOST_CHECK( trim_copy( string("") )=="" );

    // iterator tests
    string str;
    trim_left_copy_if( std::back_inserter(str), str1, is_space() );
    BOOST_CHECK( str=="1x x x x1     " );

    str.clear();
    trim_right_copy_if( std::back_inserter(str), str1, is_space() );
    BOOST_CHECK( str=="     1x x x x1" );

    str.clear();
    trim_copy_if( std::back_inserter(str), str1, is_space() );
    BOOST_CHECK( str=="1x x x x1" );

    str.clear();
    trim_left_copy_if( 
        std::back_inserter(str), 
        "     1x x x x1     ", 
        is_space() );
    BOOST_CHECK( str=="1x x x x1     " );

    str.clear();
    trim_right_copy_if( 
        std::back_inserter(str), 
        "     1x x x x1     ", 
        is_space() );
    BOOST_CHECK( str=="     1x x x x1" );

    str.clear();
    trim_copy_if( 
        std::back_inserter(str), 
        "     1x x x x1     ", 
        is_space() );
    BOOST_CHECK( str=="1x x x x1" );
    // *** inplace tests *** //

    // general string test
    trim_left( str1 );
    BOOST_CHECK( str1=="1x x x x1     " );
    trim_right( str1 );
    BOOST_CHECK( str1=="1x x x x1" );
    trim( str2 );
    BOOST_CHECK( str2=="2x x x x2" );
    
    // spaces-only string test
    str3 = "    "; trim_left( str3 );
    BOOST_CHECK( str3=="" );
    str3 = "    "; trim_right( str3 );
    BOOST_CHECK( str3=="" );
    str3 = "    "; trim( str3 );
    BOOST_CHECK( str3=="" );

    // empty string check 
    str3 = ""; trim_left( str3 );
    BOOST_CHECK( str3=="" );
    str3 = ""; trim_right( str3 );
    BOOST_CHECK( str3=="" );
    str3 = ""; trim( str3 );
    BOOST_CHECK( str3=="" );

    // *** non-standard predicate tests *** //
    BOOST_CHECK( 
        trim_copy_if( 
            string("123abc456"), 
            is_classified(std::ctype_base::digit) )=="abc" );
    BOOST_CHECK( trim_copy_if( string("<>abc<>"), is_any_of( "<<>>" ) )=="abc" );
}

void trim_all_test()
{
    string str1("     1x   x   x   x1     ");
    string str2("+---...2x+--x--+x-+-x2...---+");
    string str3("    ");

    // *** value passing tests *** //

    // general string test
    BOOST_CHECK( trim_all_copy( str1 )=="1x x x x1" ) ;
    BOOST_CHECK( trim_all_copy_if( str2, is_punct() )=="2x+x-x-x2" ) ;

    // spaces-only string test
    BOOST_CHECK( trim_all_copy( str3 )=="" );

    // empty string check 
    BOOST_CHECK( trim_all_copy( string("") )=="" );

    // general string test
    trim_all( str1 );
    BOOST_CHECK( str1=="1x x x x1" ) ;
    trim_all_if( str2, is_punct() );
    BOOST_CHECK( str2=="2x+x-x-x2" ) ;
    
    // spaces-only string test
    str3 = "    "; trim_all( str3 );
    BOOST_CHECK( str3=="" );

    // empty string check 
    str3 = ""; trim_all( str3 );
    BOOST_CHECK( str3=="" );
    BOOST_CHECK( str3=="" );

    // *** non-standard predicate tests *** //
    BOOST_CHECK( 
        trim_all_copy_if( 
            string("123abc127deb456"), 
            is_classified(std::ctype_base::digit) )=="abc1deb" );
    BOOST_CHECK( trim_all_copy_if( string("<>abc<>def<>"), is_any_of( "<<>>" ) )=="abc<def" );
}

void trim_fill_test()
{
    string str1("     1x   x   x   x1     ");
    string str2("+---...2x+--x--+x-+-x2...---+");
    string str3("    ");

    // *** value passing tests *** //

    // general string test
    BOOST_CHECK( trim_fill_copy( str1, "-" )=="1x-x-x-x1" ) ;
    BOOST_CHECK( trim_fill_copy_if( str2, " ", is_punct() )=="2x x x x2" ) ;

    // spaces-only string test
    BOOST_CHECK( trim_fill_copy( str3, " " )=="" );

    // empty string check 
    BOOST_CHECK( trim_fill_copy( string(""), " " )=="" );

    // general string test
    trim_fill( str1, "-" );
    BOOST_CHECK( str1=="1x-x-x-x1" ) ;
    trim_fill_if( str2, "", is_punct() );
    BOOST_CHECK( str2=="2xxxx2" ) ;

    // spaces-only string test
    str3 = "    "; trim_fill( str3, "" );
    BOOST_CHECK( str3=="" );

    // empty string check 
    str3 = ""; trim_fill( str3, "" );
    BOOST_CHECK( str3=="" );
    BOOST_CHECK( str3=="" );

    // *** non-standard predicate tests *** //
    BOOST_CHECK( 
        trim_fill_copy_if( 
        string("123abc127deb456"), 
        "+",
        is_classified(std::ctype_base::digit) )=="abc+deb" );
    BOOST_CHECK( trim_fill_copy_if( string("<>abc<>def<>"), "-", is_any_of( "<<>>" ) )=="abc-def" );
}

BOOST_AUTO_TEST_CASE( test_main )
{
    trim_test();
    trim_all_test();
    trim_fill_test();
}