Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
118e216e | 20 | /*************************************************************************** |
652f47a7 | 21 | * Copyright (C) 2006-2011 by Intra2net AG * |
118e216e TJ |
22 | * * |
23 | ***************************************************************************/ | |
24 | ||
25 | // #include <iostream> | |
26 | #include <string> | |
d435d600 | 27 | #include <cmath> |
118e216e TJ |
28 | // #include <sstream> |
29 | // #include <stdexcept> | |
30 | ||
9fe0853b TJ |
31 | #define BOOST_TEST_DYN_LINK |
32 | #include <boost/test/unit_test.hpp> | |
d435d600 | 33 | #include <boost/numeric/conversion/cast.hpp> |
118e216e TJ |
34 | |
35 | #include <stringfunc.hxx> | |
57e78ccd | 36 | #include <containerfunc.hpp> |
118e216e TJ |
37 | |
38 | using namespace std; | |
57e78ccd | 39 | using namespace I2n; |
118e216e | 40 | |
9fe0853b TJ |
41 | typedef std::list< std::string > StringList; |
42 | ||
43 | BOOST_AUTO_TEST_SUITE(stringfunc) | |
44 | ||
45 | BOOST_AUTO_TEST_CASE(smart_html_entites1) | |
46 | { | |
47 | string output = smart_html_entities("Test"); | |
48 | ||
49 | BOOST_CHECK_EQUAL(string("Test"), output); | |
50 | } | |
51 | ||
52 | BOOST_AUTO_TEST_CASE(smart_html_entites2) | |
53 | { | |
54 | string output = smart_html_entities("Täst"); | |
55 | ||
56 | BOOST_CHECK_EQUAL(string("Täst"), output); | |
57 | } | |
58 | ||
59 | BOOST_AUTO_TEST_CASE(smart_html_entites3) | |
60 | { | |
61 | string output = smart_html_entities("<>"); | |
62 | ||
63 | BOOST_CHECK_EQUAL(string("<>"), output); | |
64 | } | |
65 | ||
66 | BOOST_AUTO_TEST_CASE(smart_html_entites4) | |
67 | { | |
68 | string output = smart_html_entities("<ümlaut>"); | |
69 | ||
70 | BOOST_CHECK_EQUAL(string("<ümlaut>"), output); | |
71 | } | |
72 | ||
73 | BOOST_AUTO_TEST_CASE(smart_html_entites5) | |
74 | { | |
75 | string output = smart_html_entities("Test<ümlaut>Blä"); | |
76 | ||
77 | BOOST_CHECK_EQUAL(string("Test<ümlaut>Blä"), output); | |
78 | } | |
79 | ||
80 | BOOST_AUTO_TEST_CASE(smart_html_entites6) | |
81 | { | |
82 | string output = smart_html_entities("System > Einstellungen"); | |
83 | ||
84 | BOOST_CHECK_EQUAL(string("System > Einstellungen"), output); | |
85 | } | |
86 | ||
87 | BOOST_AUTO_TEST_CASE(smart_html_entites7) | |
88 | { | |
89 | string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?"); | |
90 | ||
91 | BOOST_CHECK_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">"System > Einstellungen"</a>. Oder etwa nicht?"), output); | |
92 | } | |
93 | ||
94 | BOOST_AUTO_TEST_CASE(strip_html_tags1) | |
95 | { | |
96 | string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?"); | |
97 | ||
98 | BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output); | |
99 | } | |
100 | ||
101 | BOOST_AUTO_TEST_CASE(strip_html_tags2) | |
102 | { | |
103 | string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?"); | |
104 | ||
105 | BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output); | |
106 | } | |
107 | ||
8b06a5c9 GMF |
108 | |
109 | ||
9fe0853b TJ |
110 | BOOST_AUTO_TEST_CASE(html_entities1) |
111 | { | |
112 | string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC"); | |
113 | BOOST_CHECK_EQUAL(string("äöü"), output); | |
114 | } | |
115 | ||
116 | BOOST_AUTO_TEST_CASE(html_entities2) | |
117 | { | |
118 | string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF"); | |
119 | BOOST_CHECK_EQUAL(string("å õ ÿ"), output); | |
120 | } | |
121 | ||
122 | BOOST_AUTO_TEST_CASE(html_entities3) | |
123 | { | |
124 | string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88"); | |
125 | BOOST_CHECK_EQUAL(string("Ď ༱ 肈"), output); | |
126 | } | |
127 | ||
8b06a5c9 GMF |
128 | |
129 | ||
388626dc GMF |
130 | BOOST_AUTO_TEST_CASE(nice_unit_format1) |
131 | { | |
132 | const int64_t two_bytes = 2; | |
8b06a5c9 | 133 | |
70fc0674 | 134 | string output = nice_unit_format(two_bytes, LongUnitFormat, UnitBase1000); |
388626dc | 135 | BOOST_CHECK_EQUAL(string("2.00 Bytes"), output); |
8b06a5c9 | 136 | |
4388cc08 | 137 | output = nice_unit_format(two_bytes); |
8b06a5c9 | 138 | BOOST_CHECK_EQUAL(string("2.0 B"), output); |
388626dc GMF |
139 | } |
140 | ||
141 | BOOST_AUTO_TEST_CASE(nice_unit_format2) | |
142 | { | |
143 | const int64_t two_kilobytes = 2000; | |
8b06a5c9 | 144 | |
70fc0674 | 145 | string output = nice_unit_format(two_kilobytes, LongUnitFormat, UnitBase1000); |
388626dc GMF |
146 | BOOST_CHECK_EQUAL(string("2.00 KBytes"), output); |
147 | ||
4388cc08 | 148 | output = nice_unit_format(two_kilobytes); |
8b06a5c9 GMF |
149 | BOOST_CHECK_EQUAL(string("2.0 KB"), output); |
150 | ||
d435d600 CH |
151 | output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes), LongUnitFormat, UnitBase1000); |
152 | BOOST_CHECK_EQUAL(string("2.00 KBytes"), output); | |
153 | ||
154 | output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes)); | |
155 | BOOST_CHECK_EQUAL(string("2.0 KB"), output); | |
156 | ||
388626dc | 157 | const int64_t two_and_half_kilobytes = 2500; |
8b06a5c9 | 158 | |
70fc0674 | 159 | output = nice_unit_format(two_and_half_kilobytes, LongUnitFormat, UnitBase1000); |
388626dc | 160 | BOOST_CHECK_EQUAL(string("2.50 KBytes"), output); |
8b06a5c9 | 161 | |
4388cc08 | 162 | output = nice_unit_format(two_and_half_kilobytes); |
8b06a5c9 | 163 | BOOST_CHECK_EQUAL(string("2.4 KB"), output); |
d435d600 CH |
164 | |
165 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes), LongUnitFormat, UnitBase1000); | |
166 | BOOST_CHECK_EQUAL(string("2.50 KBytes"), output); | |
167 | ||
168 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes)); | |
169 | BOOST_CHECK_EQUAL(string("2.4 KB"), output); | |
388626dc GMF |
170 | } |
171 | ||
172 | BOOST_AUTO_TEST_CASE(nice_unit_format3) | |
173 | { | |
174 | const int64_t two_megabytes = 2000000; | |
8b06a5c9 | 175 | |
70fc0674 | 176 | string output = nice_unit_format(two_megabytes, LongUnitFormat, UnitBase1000); |
388626dc GMF |
177 | BOOST_CHECK_EQUAL(string("2.00 MBytes"), output); |
178 | ||
4388cc08 | 179 | output = nice_unit_format(two_megabytes); |
8b06a5c9 GMF |
180 | BOOST_CHECK_EQUAL(string("1.9 MB"), output); |
181 | ||
d435d600 CH |
182 | output = nice_unit_format(boost::numeric_cast<double>(two_megabytes), LongUnitFormat, UnitBase1000); |
183 | BOOST_CHECK_EQUAL(string("2.00 MBytes"), output); | |
184 | ||
185 | output = nice_unit_format(boost::numeric_cast<double>(two_megabytes)); | |
186 | BOOST_CHECK_EQUAL(string("1.9 MB"), output); | |
187 | ||
388626dc | 188 | const int64_t two_and_half_megabytes = 2500000; |
8b06a5c9 | 189 | |
70fc0674 | 190 | output = nice_unit_format(two_and_half_megabytes, LongUnitFormat, UnitBase1000); |
388626dc | 191 | BOOST_CHECK_EQUAL(string("2.50 MBytes"), output); |
8b06a5c9 | 192 | |
4388cc08 | 193 | output = nice_unit_format(two_and_half_megabytes); |
8b06a5c9 GMF |
194 | BOOST_CHECK_EQUAL(string("2.4 MB"), output); |
195 | ||
d435d600 CH |
196 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes), LongUnitFormat, UnitBase1000); |
197 | BOOST_CHECK_EQUAL(string("2.50 MBytes"), output); | |
198 | ||
199 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes)); | |
200 | BOOST_CHECK_EQUAL(string("2.4 MB"), output); | |
201 | ||
388626dc GMF |
202 | } |
203 | ||
204 | BOOST_AUTO_TEST_CASE(nice_unit_format4) | |
205 | { | |
206 | const int64_t two_gigabytes = 2000000000LL; | |
8b06a5c9 | 207 | |
70fc0674 | 208 | string output = nice_unit_format(two_gigabytes, LongUnitFormat, UnitBase1000); |
388626dc GMF |
209 | BOOST_CHECK_EQUAL(string("2.00 GBytes"), output); |
210 | ||
4388cc08 | 211 | output = nice_unit_format(two_gigabytes); |
8b06a5c9 GMF |
212 | BOOST_CHECK_EQUAL(string("1.9 GB"), output); |
213 | ||
d435d600 CH |
214 | output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes), LongUnitFormat, UnitBase1000); |
215 | BOOST_CHECK_EQUAL(string("2.00 GBytes"), output); | |
216 | ||
217 | output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes)); | |
218 | BOOST_CHECK_EQUAL(string("1.9 GB"), output); | |
219 | ||
388626dc | 220 | const int64_t two_and_half_gigabytes = 2500000000LL; |
8b06a5c9 | 221 | |
70fc0674 | 222 | output = nice_unit_format(two_and_half_gigabytes, LongUnitFormat, UnitBase1000); |
388626dc | 223 | BOOST_CHECK_EQUAL(string("2.50 GBytes"), output); |
8b06a5c9 | 224 | |
4388cc08 | 225 | output = nice_unit_format(two_and_half_gigabytes); |
8b06a5c9 | 226 | BOOST_CHECK_EQUAL(string("2.3 GB"), output); |
d435d600 CH |
227 | |
228 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes), LongUnitFormat, UnitBase1000); | |
229 | BOOST_CHECK_EQUAL(string("2.50 GBytes"), output); | |
230 | ||
231 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes)); | |
232 | BOOST_CHECK_EQUAL(string("2.3 GB"), output); | |
388626dc GMF |
233 | } |
234 | ||
235 | BOOST_AUTO_TEST_CASE(nice_unit_format5) | |
236 | { | |
237 | const int64_t two_terabytes = 2000000000000LL; | |
8b06a5c9 | 238 | |
70fc0674 | 239 | string output = nice_unit_format(two_terabytes, LongUnitFormat, UnitBase1000); |
388626dc GMF |
240 | BOOST_CHECK_EQUAL(string("2.00 TBytes"), output); |
241 | ||
4388cc08 | 242 | output = nice_unit_format(two_terabytes); |
8b06a5c9 GMF |
243 | BOOST_CHECK_EQUAL(string("1.8 TB"), output); |
244 | ||
d435d600 CH |
245 | output = nice_unit_format(boost::numeric_cast<double>(two_terabytes), LongUnitFormat, UnitBase1000); |
246 | BOOST_CHECK_EQUAL(string("2.00 TBytes"), output); | |
247 | ||
248 | output = nice_unit_format(boost::numeric_cast<double>(two_terabytes)); | |
249 | BOOST_CHECK_EQUAL(string("1.8 TB"), output); | |
250 | ||
388626dc | 251 | const int64_t two_and_half_terabytes = 2500000000000LL; |
8b06a5c9 | 252 | |
70fc0674 | 253 | output = nice_unit_format(two_and_half_terabytes, LongUnitFormat, UnitBase1000); |
388626dc | 254 | BOOST_CHECK_EQUAL(string("2.50 TBytes"), output); |
8b06a5c9 | 255 | |
4388cc08 | 256 | output = nice_unit_format(two_and_half_terabytes); |
8b06a5c9 | 257 | BOOST_CHECK_EQUAL(string("2.3 TB"), output); |
d435d600 CH |
258 | |
259 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes), LongUnitFormat, UnitBase1000); | |
260 | BOOST_CHECK_EQUAL(string("2.50 TBytes"), output); | |
261 | ||
262 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes)); | |
263 | BOOST_CHECK_EQUAL(string("2.3 TB"), output); | |
388626dc GMF |
264 | } |
265 | ||
266 | BOOST_AUTO_TEST_CASE(nice_unit_format6) | |
267 | { | |
268 | const int64_t two_petabytes = 2000000000000000LL; | |
8b06a5c9 | 269 | |
70fc0674 | 270 | string output = nice_unit_format(two_petabytes, LongUnitFormat, UnitBase1000); |
388626dc GMF |
271 | BOOST_CHECK_EQUAL(string("2.00 PBytes"), output); |
272 | ||
4388cc08 | 273 | output = nice_unit_format(two_petabytes); |
8b06a5c9 GMF |
274 | BOOST_CHECK_EQUAL(string("1.8 PB"), output); |
275 | ||
d435d600 CH |
276 | output = nice_unit_format(boost::numeric_cast<double>(two_petabytes), LongUnitFormat, UnitBase1000); |
277 | BOOST_CHECK_EQUAL(string("2.00 PBytes"), output); | |
278 | ||
279 | output = nice_unit_format(boost::numeric_cast<double>(two_petabytes)); | |
280 | BOOST_CHECK_EQUAL(string("1.8 PB"), output); | |
281 | ||
388626dc | 282 | const int64_t two_and_half_petabytes = 2500000000000000LL; |
8b06a5c9 | 283 | |
70fc0674 | 284 | output = nice_unit_format(two_and_half_petabytes, LongUnitFormat, UnitBase1000); |
388626dc | 285 | BOOST_CHECK_EQUAL(string("2.50 PBytes"), output); |
8b06a5c9 | 286 | |
4388cc08 | 287 | output = nice_unit_format(two_and_half_petabytes); |
8b06a5c9 | 288 | BOOST_CHECK_EQUAL(string("2.2 PB"), output); |
d435d600 CH |
289 | |
290 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes), LongUnitFormat, UnitBase1000); | |
291 | BOOST_CHECK_EQUAL(string("2.50 PBytes"), output); | |
292 | ||
293 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes)); | |
294 | BOOST_CHECK_EQUAL(string("2.2 PB"), output); | |
388626dc GMF |
295 | } |
296 | ||
297 | BOOST_AUTO_TEST_CASE(nice_unit_format7) | |
298 | { | |
299 | const int64_t two_exabytes = 2000000000000000000LL; | |
8b06a5c9 | 300 | |
70fc0674 | 301 | string output = nice_unit_format(two_exabytes, LongUnitFormat, UnitBase1000); |
83809f5e | 302 | BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output); |
388626dc | 303 | |
4388cc08 | 304 | output = nice_unit_format(two_exabytes); |
83809f5e | 305 | BOOST_CHECK_EQUAL(string("1776.4 PB"), output); |
8b06a5c9 | 306 | |
d435d600 CH |
307 | output = nice_unit_format(boost::numeric_cast<double>(two_exabytes), LongUnitFormat, UnitBase1000); |
308 | BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output); | |
309 | ||
310 | output = nice_unit_format(boost::numeric_cast<double>(two_exabytes)); | |
311 | BOOST_CHECK_EQUAL(string("1776.4 PB"), output); | |
312 | ||
388626dc | 313 | const int64_t two_and_half_exabytes = 2500000000000000000LL; |
8b06a5c9 | 314 | |
70fc0674 | 315 | output = nice_unit_format(two_and_half_exabytes, LongUnitFormat, UnitBase1000); |
83809f5e | 316 | BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output); |
8b06a5c9 | 317 | |
4388cc08 | 318 | output = nice_unit_format(two_and_half_exabytes); |
83809f5e | 319 | BOOST_CHECK_EQUAL(string("2220.4 PB"), output); |
d435d600 CH |
320 | |
321 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes), LongUnitFormat, UnitBase1000); | |
322 | BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output); | |
323 | ||
324 | output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes)); | |
325 | BOOST_CHECK_EQUAL(string("2220.4 PB"), output); | |
83809f5e GMF |
326 | } |
327 | ||
328 | BOOST_AUTO_TEST_CASE(nice_unit_format8) | |
329 | { | |
330 | const int64_t max_representable_64bits_number = 9223372036854775807LL; | |
331 | ||
70fc0674 | 332 | string output = nice_unit_format(max_representable_64bits_number, LongUnitFormat, UnitBase1000); |
83809f5e GMF |
333 | BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output); |
334 | ||
4388cc08 | 335 | output = nice_unit_format(max_representable_64bits_number); |
83809f5e | 336 | BOOST_CHECK_EQUAL(string("8192.0 PB"), output); |
d435d600 CH |
337 | |
338 | /* | |
339 | double val = boost::numeric_cast<double>(max_representable_64bits_number); | |
340 | BOOST_MESSAGE("val as int64 is " << max_representable_64bits_number << " and as double is " << val); | |
341 | BOOST_MESSAGE("val rounded is " << round(val)); | |
342 | BOOST_CHECK_EQUAL(val, round(val)); | |
343 | BOOST_MESSAGE("half as int64 is " << boost::numeric_cast<int64_t>(round(val/2))); // ok | |
344 | BOOST_MESSAGE("cast back is " << boost::numeric_cast<int64_t>(round(val))); // throws a positive overflow | |
345 | output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number), LongUnitFormat, UnitBase1000); | |
346 | BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output); | |
347 | ||
348 | output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number)); | |
349 | BOOST_CHECK_EQUAL(string("8192.0 PB"), output); | |
350 | */ | |
388626dc GMF |
351 | } |
352 | ||
9fe0853b TJ |
353 | BOOST_AUTO_TEST_CASE(TestTrim) |
354 | { | |
355 | std::string s("s1"); | |
356 | trim_mod(s); | |
357 | BOOST_CHECK_EQUAL( std::string("s1"), s ); | |
358 | ||
359 | s=" s2"; | |
360 | trim_mod(s); | |
361 | BOOST_CHECK_EQUAL( std::string("s2"), s ); | |
362 | ||
363 | s="s3 "; | |
364 | trim_mod(s); | |
365 | BOOST_CHECK_EQUAL( std::string("s3"), s ); | |
366 | ||
367 | s="::s4:s4::++--aa"; | |
368 | trim_mod(s,":+-a"); | |
369 | BOOST_CHECK_EQUAL( std::string("s4:s4"), s); | |
370 | ||
371 | /* non modifying version */ | |
372 | ||
373 | s= "s1"; | |
374 | BOOST_CHECK_EQUAL( std::string("s1"), trim(s) ); | |
375 | ||
376 | s=" s2"; | |
377 | BOOST_CHECK_EQUAL( std::string("s2"), trim(s) ); | |
378 | BOOST_CHECK_EQUAL( std::string(" s2"), s ); | |
379 | ||
380 | s="s3 "; | |
381 | BOOST_CHECK_EQUAL( std::string("s3"), trim(s) ); | |
382 | BOOST_CHECK_EQUAL( std::string("s3 "), s ); | |
383 | ||
384 | s="::s4:s4::++--aa"; | |
385 | BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") ); | |
386 | } // eo TestTrim() | |
387 | ||
388 | ||
389 | ||
390 | BOOST_AUTO_TEST_CASE(TestChomp) | |
391 | { | |
392 | std::string s("s1"); | |
393 | ||
394 | chomp_mod(s); | |
395 | BOOST_CHECK_EQUAL( std::string("s1"), s ); | |
396 | ||
397 | s="s2\n"; | |
398 | chomp_mod(s); | |
399 | BOOST_CHECK_EQUAL( std::string("s2"), s ); | |
400 | ||
401 | s="s3:"; | |
402 | chomp_mod(s,":"); | |
403 | BOOST_CHECK_EQUAL( std::string("s3"), s ); | |
404 | ||
405 | s=":s4::"; | |
406 | chomp_mod(s,"s:"); | |
407 | BOOST_CHECK_EQUAL( std::string(":s4:"), s); | |
408 | ||
409 | /* non modifiying versions */ | |
410 | s= "s1"; | |
411 | ||
412 | BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) ); | |
413 | ||
414 | s="s2\n"; | |
415 | BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) ); | |
416 | BOOST_CHECK_EQUAL( std::string("s2\n"), s); | |
417 | ||
418 | s="s3:"; | |
419 | BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") ); | |
420 | BOOST_CHECK_EQUAL( std::string("s3:"), s); | |
421 | ||
422 | s=":s4::"; | |
423 | BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") ); | |
424 | BOOST_CHECK_EQUAL( std::string(":s4::"), s); | |
425 | } // eo TestChomp() | |
426 | ||
427 | ||
428 | ||
429 | BOOST_AUTO_TEST_CASE(TestSuffixFuncs) | |
430 | { | |
431 | std::string s1("12.cpp"); | |
432 | ||
433 | BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") ); | |
434 | BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") ); | |
435 | BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") ); | |
436 | BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") ); | |
437 | BOOST_CHECK_EQUAL( false, has_suffix(s1,"") ); | |
438 | ||
439 | std::string s1c1= remove_suffix(s1,".cpp"); | |
440 | BOOST_CHECK_EQUAL( std::string("12"), s1c1 ); | |
441 | ||
442 | std::string s1c2= remove_suffix(s1,"p"); | |
443 | BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 ); | |
444 | ||
445 | std::string s1c3= remove_suffix(s1,"cp"); | |
446 | BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 ); | |
447 | ||
448 | std::string s2(".cpp"); | |
449 | BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") ); | |
450 | ||
451 | std::string s2c1= remove_suffix(s2,".cpp"); | |
452 | BOOST_CHECK_EQUAL( std::string(""), s2c1 ); | |
453 | ||
454 | } // eo TestSuffixFuncs() | |
455 | ||
456 | ||
457 | ||
458 | BOOST_AUTO_TEST_CASE(TestPrefixFuncs) | |
459 | { | |
460 | std::string s1("12.cpp"); | |
461 | ||
462 | BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") ); | |
463 | BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") ); | |
464 | BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") ); | |
465 | BOOST_CHECK_EQUAL( false, has_prefix(s1, "") ); | |
466 | ||
467 | std::string s1c1= remove_prefix(s1, "12"); | |
468 | BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1); | |
469 | } // eo TestPrefixFuncs() | |
470 | ||
471 | ||
472 | ||
473 | BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs) | |
474 | { | |
475 | std::string u1("CASE CONVERSION TEST.."); | |
476 | std::string l1("case conversion test.."); | |
477 | ||
478 | std::string test1(l1); | |
479 | ||
480 | to_upper_mod(test1); | |
481 | BOOST_CHECK_EQUAL( u1, test1 ); | |
482 | ||
483 | to_lower_mod(test1); | |
484 | BOOST_CHECK_EQUAL( l1, test1 ); | |
485 | ||
486 | ||
487 | BOOST_CHECK_EQUAL( u1, to_upper(l1) ); | |
488 | BOOST_CHECK_EQUAL( l1, to_lower(u1) ); | |
489 | } // eo TestLowerUpper | |
490 | ||
491 | ||
492 | ||
493 | BOOST_AUTO_TEST_CASE(PairSplit1) | |
494 | { | |
495 | StringList str_list; | |
496 | get_push_back_filler(str_list) | |
497 | ("a=11")("a= 11")("a =11 ")("a = 11 ")(" a = 11 ") | |
498 | ; | |
499 | BOOST_CHECK_EQUAL( 5u, str_list.size() ); | |
500 | for(StringList::iterator it= str_list.begin(); | |
501 | it != str_list.end(); | |
502 | ++it) | |
503 | { | |
504 | std::string key, value; | |
505 | bool res= pair_split( *it, key, value); | |
506 | ||
507 | BOOST_CHECK_EQUAL( true , res ); | |
508 | BOOST_CHECK_EQUAL( std::string("a"), key ); | |
509 | BOOST_CHECK_EQUAL( std::string("11"), value ); | |
510 | } | |
511 | ||
512 | std::string key, value; | |
513 | bool res; | |
514 | ||
515 | res= pair_split(" 1 : 2 ", key, value, ':'); | |
516 | BOOST_CHECK_EQUAL( true, res ); | |
517 | BOOST_CHECK_EQUAL( std::string("1"), key); | |
518 | BOOST_CHECK_EQUAL( std::string("2"), value); | |
519 | } // eo PairSplit1 | |
520 | ||
521 | ||
522 | ||
523 | BOOST_AUTO_TEST_CASE(SplitString1) | |
118e216e | 524 | { |
9fe0853b TJ |
525 | std::string block( |
526 | "Zeile 1\n" | |
527 | "++Zeile-2--\n" | |
528 | "Zeile 3\n" | |
529 | "\n" | |
530 | "Zeile 5\n" | |
531 | ); | |
532 | ||
533 | StringList list1; | |
534 | ||
535 | split_string(block, list1, "\n"); | |
536 | // since the blocks ends with \n we should have 6 lines (the last one empty): | |
537 | BOOST_CHECK_EQUAL( 6u, list1.size() ); | |
538 | BOOST_CHECK_EQUAL( std::string(), list1.back() ); | |
539 | BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() ); | |
540 | ||
541 | StringList list2; | |
542 | ||
543 | split_string(block, list2, "\n", true); | |
544 | ||
545 | // now we omitted empty lines, now we should have only 4 lines left: | |
546 | BOOST_CHECK_EQUAL( 4u, list2.size() ); | |
547 | BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() ); | |
548 | BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() ); | |
549 | ||
550 | list2= split_string(block, "\n", true, "+-"); | |
551 | ||
552 | // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-" | |
553 | BOOST_CHECK_EQUAL( 4u, list2.size() ); | |
554 | BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() ); | |
555 | BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() ); | |
556 | BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) ); | |
557 | } // eo SplitString1 | |
558 | ||
559 | ||
560 | ||
561 | BOOST_AUTO_TEST_CASE(SplitString2) | |
562 | { | |
563 | std::string line("172.16.0.0/16 dev eth0 scope link src 172.16.1.111"); | |
564 | ||
565 | StringList list1; | |
566 | ||
567 | split_string(line, list1, " ", true, Whitespaces); | |
568 | ||
569 | BOOST_CHECK_EQUAL( 7u, list1.size() ); | |
570 | ||
571 | } // eo SplitString2 | |
572 | ||
573 | ||
574 | ||
575 | BOOST_AUTO_TEST_CASE(SplitStringEmpty) | |
576 | { | |
577 | std::string line(""); | |
578 | ||
579 | StringList list1; | |
580 | ||
581 | split_string(line, list1, " ", true, Whitespaces); | |
582 | ||
583 | BOOST_CHECK_EQUAL( 0u, list1.size() ); | |
584 | } // eo SplitStringEmpty | |
585 | ||
586 | ||
587 | BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly) | |
588 | { | |
589 | std::string line(" "); | |
590 | ||
591 | StringList list1; | |
592 | ||
593 | split_string(line, list1, " ", true, Whitespaces); | |
594 | ||
595 | BOOST_CHECK_EQUAL( 0u, list1.size() ); | |
596 | } // eo SplitStringDelimiterOnly | |
597 | ||
598 | ||
599 | ||
600 | BOOST_AUTO_TEST_CASE(JoinString1) | |
601 | { | |
602 | std::list< std::string > parts; | |
603 | get_push_back_filler(parts)("1")("2")("drei"); | |
604 | ||
605 | std::string joined_string= join_string(parts,"/"); | |
606 | // we should have slashes between the strings: | |
607 | BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string ); | |
608 | ||
609 | parts.push_back( std::string() ); | |
610 | joined_string= join_string(parts,"/"); | |
611 | // now we should have an additional trailing slash: | |
612 | BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string ); | |
613 | ||
614 | parts.push_front( std::string() ); | |
615 | joined_string= join_string(parts,"/"); | |
616 | // now we should have an additional leading slash: | |
617 | BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string ); | |
618 | ||
619 | } // eo JoinString1 | |
620 | ||
621 | ||
622 | ||
623 | BOOST_AUTO_TEST_CASE(ConversionStringInt) | |
624 | { | |
625 | std::string s1("24"); | |
626 | std::string s1x("25x"); | |
627 | int i1=0; | |
628 | bool res= false; | |
629 | ||
630 | i1= string_to<int>(s1); | |
631 | BOOST_CHECK_EQUAL( 24, i1 ); | |
632 | i1= string_to<int>(s1x); | |
633 | BOOST_CHECK_EQUAL( 25, i1 ); | |
634 | ||
635 | res= string_to<int>(s1,i1); | |
636 | BOOST_CHECK_EQUAL( true, res ); | |
637 | BOOST_CHECK_EQUAL( 24, i1 ); | |
638 | ||
639 | res= string_to<int>(s1x,i1); | |
640 | BOOST_CHECK_EQUAL( false, res ); | |
641 | ||
642 | std::string ss1= to_string( 24 ); | |
643 | BOOST_CHECK_EQUAL( std::string("24"), ss1); | |
644 | ||
645 | } // eo ConversionStringInt() | |
646 | ||
647 | ||
648 | ||
649 | BOOST_AUTO_TEST_CASE(HexConversion) | |
650 | { | |
651 | std::string hex1("49324E"); | |
652 | std::string bin1("I2N"); | |
653 | ||
654 | BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) ); | |
655 | BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) ); | |
656 | BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) ); | |
657 | ||
658 | std::string hex2("0001"); | |
659 | std::string hex2a("00 01"); | |
660 | std::string hex2b("00:01"); | |
661 | std::string bin2("\0\1",2); | |
662 | ||
663 | BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) ); | |
664 | BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) ); | |
665 | BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) ); | |
666 | BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) ); | |
667 | ||
668 | BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error); | |
669 | } // eo HexConversion() | |
670 | ||
b953bf36 GE |
671 | BOOST_AUTO_TEST_CASE(sanitize_for_logging1) |
672 | { | |
673 | string output = sanitize_for_logging("normaler string ohne aerger"); | |
674 | ||
675 | BOOST_CHECK_EQUAL(string("normaler string ohne aerger"), output); | |
676 | } | |
677 | ||
678 | BOOST_AUTO_TEST_CASE(sanitize_for_logging2) | |
679 | { | |
680 | string to_test="fiese"; | |
681 | to_test.push_back(0); | |
682 | to_test+="null"; | |
683 | ||
684 | string output = sanitize_for_logging(to_test); | |
685 | ||
686 | BOOST_CHECK_EQUAL(string("fiese?null"), output); | |
687 | } | |
688 | ||
689 | BOOST_AUTO_TEST_CASE(sanitize_for_logging3) | |
690 | { | |
691 | string output = sanitize_for_logging("läuter ümlaute utf8"); | |
692 | ||
693 | BOOST_CHECK_EQUAL(string("l??uter ??mlaute utf8"), output); | |
694 | } | |
695 | ||
9fe0853b | 696 | BOOST_AUTO_TEST_SUITE_END() |