]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/test/utils-ts/algorithm-test.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / test / test / utils-ts / algorithm-test.cpp
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)
5
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // File : $RCSfile$
9 //
10 // Version : $Revision$
11 //
12 // Description : unit test for class properties facility
13 // ***************************************************************************
14
15 // Boost.Test
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;
24
25 // STL
26 #include <cctype>
27 #include <functional>
28
29 # ifdef BOOST_NO_STDC_NAMESPACE
30 namespace std { using ::toupper; }
31 # endif
32
33 #ifdef BOOST_NO_CXX11_DECLTYPE
34 #define TEST_SURROUND_EXPRESSION(x) (x)
35 #else
36 #define TEST_SURROUND_EXPRESSION(x) x
37 #endif
38
39 #ifdef BOOST_NO_CXX98_BINDERS
40 #define REF_FUN(x) std::function<bool(char,char)>(x)
41 #else
42 #define REF_FUN(x) std::ptr_fun(x)
43 #endif
44
45 //____________________________________________________________________________//
46
47 bool predicate( char c1, char c2 ) { return (std::toupper)( c1 ) == (std::toupper)( c2 ); }
48
49 //____________________________________________________________________________//
50
51 BOOST_AUTO_TEST_CASE( test_mismatch )
52 {
53 const_string cs1( "test_string" );
54 const_string cs2( "test_stream" );
55
56 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::mismatch( cs1.begin(), cs1.end(), cs2.begin(), cs2.end() ).first - cs1.begin()) == 8 );
57
58 cs2 = "trest";
59 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::mismatch( cs1.begin(), cs1.end(), cs2.begin(), cs2.end() ).first - cs1.begin()) == 1 );
60
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 );
63
64 cs2 = "TeSt_liNk";
65 BOOST_TEST(
66 TEST_SURROUND_EXPRESSION(utu::mismatch( cs1.begin(), cs1.end(), cs2.begin(), cs2.end(), REF_FUN( predicate ) ).first - cs1.begin()) == 5 );
67 }
68
69 //____________________________________________________________________________//
70
71 BOOST_AUTO_TEST_CASE( test_find_first_not_of )
72 {
73 const_string cs( "test_string" );
74 const_string another( "tes" );
75
76 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_first_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin()) == 4 );
77
78 another = "T_sE";
79 BOOST_TEST(
80 TEST_SURROUND_EXPRESSION(utu::find_first_not_of( cs.begin(), cs.end(), another.begin(), another.end(), REF_FUN( predicate ) ) - cs.begin()) == 7 );
81
82 another = "tes_ring";
83 BOOST_TEST( utu::find_last_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) == cs.end() );
84 }
85
86 //____________________________________________________________________________//
87
88 BOOST_AUTO_TEST_CASE( test_find_last_of )
89 {
90 const_string cs( "test_string" );
91 const_string another( "tes" );
92
93 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin()) == 6 );
94
95 another = "_Se";
96 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_of( cs.begin(), cs.end(), another.begin(), another.end(), REF_FUN( predicate ) ) - cs.begin()) == 5 );
97
98 another = "qw";
99 BOOST_TEST( utu::find_last_of( cs.begin(), cs.end(), another.begin(), another.end() ) == cs.end() );
100
101 cs = "qerty";
102 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin()) == 0 );
103 }
104
105 //____________________________________________________________________________//
106
107 BOOST_AUTO_TEST_CASE( test_find_last_not_of )
108 {
109 const_string cs( "test_string" );
110 const_string another( "string" );
111
112 BOOST_TEST( TEST_SURROUND_EXPRESSION(utu::find_last_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin()) == 4 );
113
114 another = "_SeG";
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 );
116
117 another = "e_string";
118 BOOST_TEST( utu::find_last_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) == cs.end() );
119 }
120
121 //____________________________________________________________________________//
122
123 BOOST_AUTO_TEST_CASE( test_replace_all )
124 {
125 {
126 std::string cs( "cstring: cstring is a const string that can be transformed to an std::string" );
127
128 const std::string to_look_for[] = {"cstring", "const"};
129 const std::string to_replace[] = { "const_string", "constant" };
130
131 BOOST_TEST( utu::replace_all_occurrences_of(
132 cs,
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]))
135 ==
136 "constant_string: constant_string is a constant string that can be transformed to an std::string"
137 );
138 }
139
140 {
141 std::string cs( "some\\file\\with\\wrong:.path" );
142
143 const std::string to_look_for[] = {"\\", ":"};
144 const std::string to_replace[] = { "/", "_" };
145
146 BOOST_TEST( utu::replace_all_occurrences_of(
147 cs,
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]))
150 ==
151 "some/file/with/wrong_.path"
152 );
153 }
154 }
155
156 //____________________________________________________________________________//
157
158 BOOST_AUTO_TEST_CASE( test_replace_with_wildcards )
159 {
160 {
161 std::string cs( "this string contains x='374'u elements of size y='24'u and y='75'z" );
162
163 const std::string to_look_for[] = {"x='*'u", "y='*'u"};
164 const std::string to_replace[] = { "x='27'q", "k='*0'p" };
165
166 BOOST_TEST( utu::replace_all_occurrences_with_wildcards(
167 cs,
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]))
170 ==
171 "this string contains x='27'q elements of size k='240'p and y='75'z"
172 );
173 }
174 }
175
176 //____________________________________________________________________________//
177
178 // EOF