File: string_view_compat_test.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (336 lines) | stat: -rw-r--r-- 8,183 bytes parent folder | download | duplicates (5)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2007-2017. 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)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/container/string.hpp>
#include <boost/utility/string_view.hpp>

#include <boost/core/lightweight_test.hpp>

#if defined(__cpp_lib_string_view) && (__cpp_lib_string_view >= 201606L)
#define BOOST_CONTAINER_TEST_HAS_STD_STRING_VIEW
#include <string_view>
#endif

template<class StringViewType>
void conversion_test()
{
   #ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN
   {
      const boost::container::string s = "some text";
      StringViewType sv(s);
      BOOST_TEST(s.data() == sv.data() && s.size() == sv.size());
      StringViewType sv2;
      sv2 = s;
      BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size());
      const StringViewType csv(s);
      BOOST_TEST(s.data() == sv.data() && s.size() == csv.size());
   }
   #endif
}

template<class StringViewType>
void to_view_test()
{
   const boost::container::string s = "some text";
   StringViewType sv(s.to_view<StringViewType>());
   BOOST_TEST(s.data() == sv.data() && s.size() == sv.size());
   StringViewType sv2;
   sv2 = s.to_view<StringViewType>();
   BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size());
   const StringViewType csv(s.to_view<StringViewType>());
   BOOST_TEST(s.data() == csv.data() && s.size() == csv.size());
}

template<class StringViewType>
void equal_test()
{
   const StringViewType sv = "same text";
   const StringViewType svd = "different text";
   const boost::container::string s = "same text";
   BOOST_TEST(sv == s);
   BOOST_TEST(s == sv);
   BOOST_TEST(!(svd == s));
   BOOST_TEST(!(s == svd));
}

template<class StringViewType>
void unequal_test()
{
   const StringViewType sv = "same text";
   const StringViewType svd = "different text";
   const boost::container::string s = "same text";
   BOOST_TEST(!(sv != s));
   BOOST_TEST(!(s != sv));
   BOOST_TEST(svd != s);
   BOOST_TEST(s != svd);
}

template<class StringViewType>
void less_test()
{
   StringViewType sv  = "0123456";
   boost::container::string s = "0123459";
   BOOST_TEST(sv < s);
   BOOST_TEST(!(s < sv));

   sv = "0123459";
   s  = "0123456";
   BOOST_TEST(!(sv < s));
   BOOST_TEST(s < sv);

   sv = "0123456";
   BOOST_TEST(!(sv < s));
   BOOST_TEST(!(s < sv));
}

template<class StringViewType>
void greater_test()
{
   StringViewType sv  = "0123459";
   boost::container::string s = "0123456";
   BOOST_TEST(sv > s);
   BOOST_TEST(!(s > sv));

   sv = "0123456";
   s  = "0123459";
   BOOST_TEST(!(sv > s));
   BOOST_TEST(s > sv);

   sv = "0123459";
   BOOST_TEST(!(sv > s));
   BOOST_TEST(!(s > sv));
}

template<class StringViewType>
void less_equal_test()
{
   StringViewType sv  = "0123456";
   boost::container::string s = "0123459";
   BOOST_TEST(sv <= s);
   BOOST_TEST(!(s <= sv));

   sv = "0123459";
   s  = "0123456";
   BOOST_TEST(!(sv <= s));
   BOOST_TEST(s <= sv);

   sv = "0123456";
   BOOST_TEST(sv <= s);
   BOOST_TEST(s <= sv);
}

template<class StringViewType>
void greater_equal_test()
{
   StringViewType sv  = "0123459";
   boost::container::string s = "0123456";
   BOOST_TEST(sv >= s);
   BOOST_TEST(!(s >= sv));

   sv = "0123456";
   s  = "0123459";
   BOOST_TEST(!(sv >= s));
   BOOST_TEST(s >= sv);

   sv = "0123459";
   BOOST_TEST(sv >= s);
   BOOST_TEST(s >= sv);
}

template<class StringViewType>
void constructor_test()
{
   StringViewType sv  = "0123459";
   boost::container::string s(sv);
   BOOST_TEST(sv == s);
   boost::container::string s2(sv, s.get_allocator());
   BOOST_TEST(sv == s);
}

template<class StringViewType>
void assignment_test()
{
   StringViewType sv  = "0123459";
   boost::container::string s;
   s = sv;
   BOOST_TEST(sv == s);
}

template<class StringViewType>
void assign_test()
{
   StringViewType sv  = "0123459";
   boost::container::string s;
   s.assign(sv);
   BOOST_TEST(sv == s);
}

template<class StringViewType>
void plus_equal_test()
{
   StringViewType sv  = "23459";
   boost::container::string s("01");
   s += sv;
   BOOST_TEST(s == "0123459");
}

template<class StringViewType>
void append_test()
{
   StringViewType sv  = "23459";
   boost::container::string s("01");
   s.append(sv);
   BOOST_TEST(s == "0123459");
}

template<class StringViewType>
void insert_test()
{
   StringViewType sv  = "34";
   boost::container::string s("01259");
   s.insert(3u, sv);
   BOOST_TEST(s == "0123459");
}

template<class StringViewType>
void replace_test()
{
   StringViewType sv  = "5678";
   boost::container::string s("01259");
   s.replace(2u, 2u, sv);
   BOOST_TEST(s == "0156789");
   s.replace(s.begin()+3, s.begin()+6, sv);
   BOOST_TEST(s == "01556789");
   s.replace(5u, 3u, sv, 2u, 2u);
   BOOST_TEST(s == "0155678");
}

template<class StringViewType>
void find_test()
{
   const StringViewType sv  = "25";
   boost::container::string s("0125925123");
   BOOST_TEST(s.find(sv,4) == 5);
}

template<class StringViewType>
void rfind_test()
{
   const StringViewType sv  = "25";
   boost::container::string s("0125925123");
   BOOST_TEST(s.rfind(sv,4) == 2);
}

template<class StringViewType>
void find_first_of_test()
{
   const StringViewType sv  = "52";
   boost::container::string s("0125925123");
   BOOST_TEST(s.find_first_of(sv,4) == 5);
}

template<class StringViewType>
void find_last_of_test()
{
   const StringViewType sv  = "52";
   boost::container::string s("520125925123");
   BOOST_TEST(s.find_last_of(sv,6) == 5);
}

template<class StringViewType>
void find_first_not_of_test()
{
   const StringViewType sv  = "52";
   boost::container::string s("0125925123");
   BOOST_TEST(s.find_first_not_of(sv,2) == 4);
}

template<class StringViewType>
void find_last_not_of_test()
{
   const StringViewType sv  = "52";
   boost::container::string s("0125925123");
   BOOST_TEST(s.find_last_not_of(sv,6) == 4);
}

template<class StringViewType>
void compare_test()
{
   const StringViewType sv  = "52";
   boost::container::string s("0125925123");
   BOOST_TEST(s.compare(sv) < 0);
   BOOST_TEST(s.compare(StringViewType("0125925123")) == 0);
   BOOST_TEST(s.compare(2u, s.size() - 2u, StringViewType("25925123")) == 0);
   StringViewType sv2("5212592512389");
   BOOST_TEST(s.compare(2u, s.size() - 2u, sv2, 3, sv2.size()-5u) == 0);
}

#if BOOST_CXX_VERSION >= 201103L
#include <boost/container_hash/hash.hpp>

template<class StringViewType>
void hash_test()
{

   const boost::container::string s1 = "0125925123";
   const boost::container::string s2 = "25925123";
   typedef boost::hash<boost::container::string> shash_t;
   typedef boost::hash<StringViewType> svhash_t;

   BOOST_TEST(shash_t()(s1) == svhash_t()(StringViewType(s1)));
   BOOST_TEST(shash_t()(s2) == svhash_t()(StringViewType(s2)));
}

#else

template<class StringViewType>
void hash_test()
{}

#endif

template<class StringViewType>
void test_all()
{
   conversion_test<StringViewType>();
   to_view_test<StringViewType>();
   equal_test<StringViewType>();
   unequal_test<StringViewType>();
   less_test<StringViewType>();
   greater_test<StringViewType>();
   less_equal_test<StringViewType>();
   greater_equal_test<StringViewType>();
   constructor_test<StringViewType>();
   assignment_test<StringViewType>();
   assign_test<StringViewType>();
   plus_equal_test<StringViewType>();
   append_test<StringViewType>();
   insert_test<StringViewType>();
   replace_test<StringViewType>();
   find_test<StringViewType>();
   rfind_test<StringViewType>();
   find_first_of_test<StringViewType>();
   find_last_of_test<StringViewType>();
   find_first_not_of_test<StringViewType>();
   find_last_not_of_test<StringViewType>();
   compare_test<StringViewType>();
   hash_test<StringViewType>();
}

int main()
{
   test_all<boost::string_view>();
   #ifdef BOOST_CONTAINER_TEST_HAS_STD_STRING_VIEW
   test_all<std::string_view>();
   #endif

   return boost::report_errors();
}