1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-2017. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #include <boost/container/string.hpp>
12 #include <boost/utility/string_view.hpp>
13
14 #include <boost/core/lightweight_test.hpp>
15
conversion_test()16 void conversion_test()
17 {
18 #ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN
19 {
20 const boost::container::string s = "some text";
21 boost::string_view sv(s);
22 BOOST_TEST(s.data() == sv.data() && s.size() == sv.size());
23 boost::string_view sv2;
24 sv2 = s;
25 BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size());
26 const boost::string_view csv(s);
27 BOOST_TEST(s.data() == sv.data() && s.size() == csv.size());
28 }
29 #endif
30 }
31
to_view_test()32 void to_view_test()
33 {
34 const boost::container::string s = "some text";
35 boost::string_view sv(s.to_view<boost::string_view>());
36 BOOST_TEST(s.data() == sv.data() && s.size() == sv.size());
37 boost::string_view sv2;
38 sv2 = s.to_view<boost::string_view>();
39 BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size());
40 const boost::string_view csv(s.to_view<boost::string_view>());
41 BOOST_TEST(s.data() == csv.data() && s.size() == csv.size());
42 }
43
equal_test()44 void equal_test()
45 {
46 const boost::string_view sv = "same text";
47 const boost::string_view svd = "different text";
48 const boost::container::string s = "same text";
49 BOOST_TEST(sv == s);
50 BOOST_TEST(s == sv);
51 BOOST_TEST(!(svd == s));
52 BOOST_TEST(!(s == svd));
53 }
54
unequal_test()55 void unequal_test()
56 {
57 const boost::string_view sv = "same text";
58 const boost::string_view svd = "different text";
59 const boost::container::string s = "same text";
60 BOOST_TEST(!(sv != s));
61 BOOST_TEST(!(s != sv));
62 BOOST_TEST(svd != s);
63 BOOST_TEST(s != svd);
64 }
65
less_test()66 void less_test()
67 {
68 boost::string_view sv = "0123456";
69 boost::container::string s = "0123459";
70 BOOST_TEST(sv < s);
71 BOOST_TEST(!(s < sv));
72
73 sv = "0123459";
74 s = "0123456";
75 BOOST_TEST(!(sv < s));
76 BOOST_TEST(s < sv);
77
78 sv = "0123456";
79 BOOST_TEST(!(sv < s));
80 BOOST_TEST(!(s < sv));
81 }
82
greater_test()83 void greater_test()
84 {
85 boost::string_view sv = "0123459";
86 boost::container::string s = "0123456";
87 BOOST_TEST(sv > s);
88 BOOST_TEST(!(s > sv));
89
90 sv = "0123456";
91 s = "0123459";
92 BOOST_TEST(!(sv > s));
93 BOOST_TEST(s > sv);
94
95 sv = "0123459";
96 BOOST_TEST(!(sv > s));
97 BOOST_TEST(!(s > sv));
98 }
99
less_equal_test()100 void less_equal_test()
101 {
102 boost::string_view sv = "0123456";
103 boost::container::string s = "0123459";
104 BOOST_TEST(sv <= s);
105 BOOST_TEST(!(s <= sv));
106
107 sv = "0123459";
108 s = "0123456";
109 BOOST_TEST(!(sv <= s));
110 BOOST_TEST(s <= sv);
111
112 sv = "0123456";
113 BOOST_TEST(sv <= s);
114 BOOST_TEST(s <= sv);
115 }
116
greater_equal_test()117 void greater_equal_test()
118 {
119 boost::string_view sv = "0123459";
120 boost::container::string s = "0123456";
121 BOOST_TEST(sv >= s);
122 BOOST_TEST(!(s >= sv));
123
124 sv = "0123456";
125 s = "0123459";
126 BOOST_TEST(!(sv >= s));
127 BOOST_TEST(s >= sv);
128
129 sv = "0123459";
130 BOOST_TEST(sv >= s);
131 BOOST_TEST(s >= sv);
132 }
133
constructor_test()134 void constructor_test()
135 {
136 boost::string_view sv = "0123459";
137 boost::container::string s(sv);
138 BOOST_TEST(sv == s);
139 boost::container::string s2(sv, s.get_allocator());
140 BOOST_TEST(sv == s);
141 }
142
assignment_test()143 void assignment_test()
144 {
145 boost::string_view sv = "0123459";
146 boost::container::string s;
147 s = sv;
148 BOOST_TEST(sv == s);
149 }
150
assign_test()151 void assign_test()
152 {
153 boost::string_view sv = "0123459";
154 boost::container::string s;
155 s.assign(sv);
156 BOOST_TEST(sv == s);
157 }
158
plus_equal_test()159 void plus_equal_test()
160 {
161 boost::string_view sv = "23459";
162 boost::container::string s("01");
163 s += sv;
164 BOOST_TEST(s == "0123459");
165 }
166
append_test()167 void append_test()
168 {
169 boost::string_view sv = "23459";
170 boost::container::string s("01");
171 s.append(sv);
172 BOOST_TEST(s == "0123459");
173 }
174
insert_test()175 void insert_test()
176 {
177 boost::string_view sv = "34";
178 boost::container::string s("01259");
179 s.insert(3u, sv);
180 BOOST_TEST(s == "0123459");
181 }
182
replace_test()183 void replace_test()
184 {
185 boost::string_view sv = "5678";
186 boost::container::string s("01259");
187 s.replace(2u, 2u, sv);
188 BOOST_TEST(s == "0156789");
189 s.replace(s.begin()+3, s.begin()+6, sv);
190 BOOST_TEST(s == "01556789");
191 s.replace(5u, 3u, sv, 2u, 2u);
192 BOOST_TEST(s == "0155678");
193 }
194
find_test()195 void find_test()
196 {
197 const boost::string_view sv = "25";
198 boost::container::string s("0125925123");
199 BOOST_TEST(s.find(sv,4) == 5);
200 }
201
rfind_test()202 void rfind_test()
203 {
204 const boost::string_view sv = "25";
205 boost::container::string s("0125925123");
206 BOOST_TEST(s.rfind(sv,4) == 2);
207 }
208
find_first_of_test()209 void find_first_of_test()
210 {
211 const boost::string_view sv = "52";
212 boost::container::string s("0125925123");
213 BOOST_TEST(s.find_first_of(sv,4) == 5);
214 }
215
find_last_of_test()216 void find_last_of_test()
217 {
218 const boost::string_view sv = "52";
219 boost::container::string s("520125925123");
220 BOOST_TEST(s.find_last_of(sv,6) == 5);
221 }
222
find_first_not_of_test()223 void find_first_not_of_test()
224 {
225 const boost::string_view sv = "52";
226 boost::container::string s("0125925123");
227 BOOST_TEST(s.find_first_not_of(sv,2) == 4);
228 }
229
find_last_not_of_test()230 void find_last_not_of_test()
231 {
232 const boost::string_view sv = "52";
233 boost::container::string s("0125925123");
234 BOOST_TEST(s.find_last_not_of(sv,6) == 4);
235 }
236
compare_test()237 void compare_test()
238 {
239 const boost::string_view sv = "52";
240 boost::container::string s("0125925123");
241 BOOST_TEST(s.compare(sv) < 0);
242 BOOST_TEST(s.compare(boost::string_view("0125925123")) == 0);
243 BOOST_TEST(s.compare(2u, s.size() - 2u, boost::string_view("25925123")) == 0);
244 boost::string_view sv2("5212592512389");
245 BOOST_TEST(s.compare(2u, s.size() - 2u, sv2, 3, sv2.size()-5u) == 0);
246 }
247
main()248 int main()
249 {
250 conversion_test();
251 to_view_test();
252 equal_test();
253 unequal_test();
254 less_test();
255 greater_test();
256 less_equal_test();
257 greater_equal_test();
258 constructor_test();
259 assignment_test();
260 assign_test();
261 plus_equal_test();
262 append_test();
263 insert_test();
264 replace_test();
265 find_test();
266 rfind_test();
267 find_first_of_test();
268 find_last_of_test();
269 find_first_not_of_test();
270 find_last_not_of_test();
271 compare_test();
272
273 return boost::report_errors();
274 }
275
276