1 // (C) Copyright Gennadiy Rozental 2003-2014.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : unit test for class properties facility
13 // ***************************************************************************
16 #define BOOST_TEST_MODULE Boost.Test algorithms test
17 #include <boost/test/unit_test.hpp>
18 #include <boost/test/utils/class_properties.hpp>
19 #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
20 #include <boost/test/utils/algorithm.hpp>
21 namespace utf
= boost::unit_test
;
22 namespace utu
= boost::unit_test::utils
;
23 using utf::const_string
;
29 # ifdef BOOST_NO_STDC_NAMESPACE
30 namespace std
{ using ::toupper
; }
33 #ifdef BOOST_NO_CXX11_DECLTYPE
34 #define TEST_SURROUND_EXPRESSION(x) (x)
36 #define TEST_SURROUND_EXPRESSION(x) x
39 #ifdef BOOST_NO_CXX98_BINDERS
40 #define REF_FUN(x) std::function<bool(char,char)>(x)
42 #define REF_FUN(x) std::ptr_fun(x)
45 //____________________________________________________________________________//
47 bool predicate( char c1
, char c2
) { return (std::toupper
)( c1
) == (std::toupper
)( c2
); }
49 //____________________________________________________________________________//
51 BOOST_AUTO_TEST_CASE( test_mismatch
)
53 const_string
cs1( "test_string" );
54 const_string
cs2( "test_stream" );
56 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::mismatch( cs1
.begin(), cs1
.end(), cs2
.begin(), cs2
.end() ).first
- cs1
.begin()) == 8 );
59 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::mismatch( cs1
.begin(), cs1
.end(), cs2
.begin(), cs2
.end() ).first
- cs1
.begin()) == 1 );
61 cs2
= "test_string_klmn";
62 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::mismatch( cs1
.begin(), cs1
.end(), cs2
.begin(), cs2
.end() ).first
- cs1
.begin()) == 11 );
66 TEST_SURROUND_EXPRESSION(utu::mismatch( cs1
.begin(), cs1
.end(), cs2
.begin(), cs2
.end(), REF_FUN( predicate
) ).first
- cs1
.begin()) == 5 );
69 //____________________________________________________________________________//
71 BOOST_AUTO_TEST_CASE( test_find_first_not_of
)
73 const_string
cs( "test_string" );
74 const_string
another( "tes" );
76 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_first_not_of( cs
.begin(), cs
.end(), another
.begin(), another
.end() ) - cs
.begin()) == 4 );
80 TEST_SURROUND_EXPRESSION(utu::find_first_not_of( cs
.begin(), cs
.end(), another
.begin(), another
.end(), REF_FUN( predicate
) ) - cs
.begin()) == 7 );
83 BOOST_TEST( utu::find_last_not_of( cs
.begin(), cs
.end(), another
.begin(), another
.end() ) == cs
.end() );
86 //____________________________________________________________________________//
88 BOOST_AUTO_TEST_CASE( test_find_last_of
)
90 const_string
cs( "test_string" );
91 const_string
another( "tes" );
93 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_of( cs
.begin(), cs
.end(), another
.begin(), another
.end() ) - cs
.begin()) == 6 );
96 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_of( cs
.begin(), cs
.end(), another
.begin(), another
.end(), REF_FUN( predicate
) ) - cs
.begin()) == 5 );
99 BOOST_TEST( utu::find_last_of( cs
.begin(), cs
.end(), another
.begin(), another
.end() ) == cs
.end() );
102 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_of( cs
.begin(), cs
.end(), another
.begin(), another
.end() ) - cs
.begin()) == 0 );
105 //____________________________________________________________________________//
107 BOOST_AUTO_TEST_CASE( test_find_last_not_of
)
109 const_string
cs( "test_string" );
110 const_string
another( "string" );
112 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_not_of( cs
.begin(), cs
.end(), another
.begin(), another
.end() ) - cs
.begin()) == 4 );
115 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_not_of( cs
.begin(), cs
.end(), another
.begin(), another
.end(), REF_FUN( predicate
) ) - cs
.begin()) == 9 );
117 another
= "e_string";
118 BOOST_TEST( utu::find_last_not_of( cs
.begin(), cs
.end(), another
.begin(), another
.end() ) == cs
.end() );
121 //____________________________________________________________________________//
123 BOOST_AUTO_TEST_CASE( test_replace_all
)
126 std::string
cs( "cstring: cstring is a const string that can be transformed to an std::string" );
128 const std::string to_look_for
[] = {"cstring", "const"};
129 const std::string to_replace
[] = { "const_string", "constant" };
131 BOOST_TEST( utu::replace_all_occurrences_of(
133 to_look_for
, to_look_for
+ sizeof(to_look_for
)/sizeof(to_look_for
[0]),
134 to_replace
, to_replace
+ sizeof(to_replace
)/sizeof(to_replace
[0]))
136 "constant_string: constant_string is a constant string that can be transformed to an std::string"
141 std::string
cs( "some\\file\\with\\wrong:.path" );
143 const std::string to_look_for
[] = {"\\", ":"};
144 const std::string to_replace
[] = { "/", "_" };
146 BOOST_TEST( utu::replace_all_occurrences_of(
148 to_look_for
, to_look_for
+ sizeof(to_look_for
)/sizeof(to_look_for
[0]),
149 to_replace
, to_replace
+ sizeof(to_replace
)/sizeof(to_replace
[0]))
151 "some/file/with/wrong_.path"
156 //____________________________________________________________________________//
158 BOOST_AUTO_TEST_CASE( test_replace_with_wildcards
)
161 std::string
cs( "this string contains x='374'u elements of size y='24'u and y='75'z" );
163 const std::string to_look_for
[] = {"x='*'u", "y='*'u"};
164 const std::string to_replace
[] = { "x='27'q", "k='*0'p" };
166 BOOST_TEST( utu::replace_all_occurrences_with_wildcards(
168 to_look_for
, to_look_for
+ sizeof(to_look_for
)/sizeof(to_look_for
[0]),
169 to_replace
, to_replace
+ sizeof(to_replace
)/sizeof(to_replace
[0]))
171 "this string contains x='27'q elements of size k='240'p and y='75'z"
176 //____________________________________________________________________________//