Added unit test for nice_unit_format()
[libi2ncommon] / test / stringfunc.cpp
1 /***************************************************************************
2  *   Copyright (C) 2006 by Intra2net AG                                    *
3  *   info@intra2net.com                                                    *
4  *                                                                         *
5  ***************************************************************************/
6
7 // #include <iostream>
8 #include <string>
9 // #include <sstream>
10 // #include <stdexcept>
11
12 #define BOOST_TEST_DYN_LINK
13 #include <boost/test/unit_test.hpp>
14
15 #include <stringfunc.hxx>
16 #include <containerfunc.hpp>
17
18 using namespace std;
19 using namespace I2n;
20
21 typedef std::list< std::string > StringList;
22
23 BOOST_AUTO_TEST_SUITE(stringfunc)
24
25 BOOST_AUTO_TEST_CASE(smart_html_entites1)
26 {
27     string output = smart_html_entities("Test");
28
29     BOOST_CHECK_EQUAL(string("Test"), output);
30 }
31
32 BOOST_AUTO_TEST_CASE(smart_html_entites2)
33 {
34     string output = smart_html_entities("Täst");
35
36     BOOST_CHECK_EQUAL(string("T&auml;st"), output);
37 }
38
39 BOOST_AUTO_TEST_CASE(smart_html_entites3)
40 {
41     string output = smart_html_entities("<>");
42
43     BOOST_CHECK_EQUAL(string("<>"), output);
44 }
45
46 BOOST_AUTO_TEST_CASE(smart_html_entites4)
47 {
48     string output = smart_html_entities("<ümlaut>");
49
50     BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
51 }
52
53 BOOST_AUTO_TEST_CASE(smart_html_entites5)
54 {
55     string output = smart_html_entities("Test<ümlaut>Blä");
56
57     BOOST_CHECK_EQUAL(string("Test<ümlaut>Bl&auml;"), output);
58 }
59
60 BOOST_AUTO_TEST_CASE(smart_html_entites6)
61 {
62     string output = smart_html_entities("System > Einstellungen");
63
64     BOOST_CHECK_EQUAL(string("System &gt; Einstellungen"), output);
65 }
66
67 BOOST_AUTO_TEST_CASE(smart_html_entites7)
68 {
69     string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
70
71     BOOST_CHECK_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">&quot;System &gt; Einstellungen&quot;</a>. Oder etwa nicht?"), output);
72 }
73
74 BOOST_AUTO_TEST_CASE(strip_html_tags1)
75 {
76     string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
77
78     BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
79 }
80
81 BOOST_AUTO_TEST_CASE(strip_html_tags2)
82 {
83     string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
84
85     BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
86 }
87
88 BOOST_AUTO_TEST_CASE(html_entities1)
89 {
90     string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
91     BOOST_CHECK_EQUAL(string("&auml;&ouml;&uuml;"), output);
92 }
93
94 BOOST_AUTO_TEST_CASE(html_entities2)
95 {
96     string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
97     BOOST_CHECK_EQUAL(string("&#229; &#245; &#255;"), output);
98 }
99
100 BOOST_AUTO_TEST_CASE(html_entities3)
101 {
102     string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
103     BOOST_CHECK_EQUAL(string("&#270; &#3889; &#32904;"), output);
104 }
105
106 BOOST_AUTO_TEST_CASE(nice_unit_format1)
107 {
108     const int64_t two_bytes = 2;
109     string output = nice_unit_format(two_bytes);
110     BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
111 }
112
113 BOOST_AUTO_TEST_CASE(nice_unit_format2)
114 {
115     const int64_t two_kilobytes = 2000;
116     string output = nice_unit_format(two_kilobytes);
117     BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
118
119     const int64_t two_and_half_kilobytes = 2500;
120     output = nice_unit_format(two_and_half_kilobytes);
121     BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
122 }
123
124 BOOST_AUTO_TEST_CASE(nice_unit_format3)
125 {
126     const int64_t two_megabytes = 2000000;
127     string output = nice_unit_format(two_megabytes);
128     BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
129
130     const int64_t two_and_half_megabytes = 2500000;
131     output = nice_unit_format(two_and_half_megabytes);
132     BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
133 }
134
135 BOOST_AUTO_TEST_CASE(nice_unit_format4)
136 {
137     const int64_t two_gigabytes = 2000000000LL;
138     string output = nice_unit_format(two_gigabytes);
139     BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
140
141     const int64_t two_and_half_gigabytes = 2500000000LL;
142     output = nice_unit_format(two_and_half_gigabytes);
143     BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
144 }
145
146 BOOST_AUTO_TEST_CASE(nice_unit_format5)
147 {
148     const int64_t two_terabytes = 2000000000000LL;
149     string output = nice_unit_format(two_terabytes);
150     BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
151
152     const int64_t two_and_half_terabytes = 2500000000000LL;
153     output = nice_unit_format(two_and_half_terabytes);
154     BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
155 }
156
157 BOOST_AUTO_TEST_CASE(nice_unit_format6)
158 {
159     const int64_t two_petabytes = 2000000000000000LL;
160     string output = nice_unit_format(two_petabytes);
161     BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
162
163     const int64_t two_and_half_petabytes = 2500000000000000LL;
164     output = nice_unit_format(two_and_half_petabytes);
165     BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
166 }
167
168 BOOST_AUTO_TEST_CASE(nice_unit_format7)
169 {
170     const int64_t two_exabytes = 2000000000000000000LL;
171     string output = nice_unit_format(two_exabytes);
172     BOOST_CHECK_EQUAL(string("2.00 EBytes"), output);
173
174     const int64_t two_and_half_exabytes = 2500000000000000000LL;
175     output = nice_unit_format(two_and_half_exabytes);
176     BOOST_CHECK_EQUAL(string("2.50 EBytes"), output);
177 }
178
179 BOOST_AUTO_TEST_CASE(imaputf7_to_utf8)
180 {
181     string output = utf7imap_to_utf8("Sp&AOQ-m");
182     BOOST_CHECK_EQUAL(string("Späm"), output);
183 }
184
185 BOOST_AUTO_TEST_CASE(utf8_to_imaputf7)
186 {
187     string output = utf8_to_utf7imap("Späm");
188     BOOST_CHECK_EQUAL(string("Sp&AOQ-m"), output);
189 }
190
191 /*
192 **
193 */
194
195 BOOST_AUTO_TEST_CASE(TestTrim)
196 {
197     std::string s("s1");
198     trim_mod(s);
199     BOOST_CHECK_EQUAL( std::string("s1"), s );
200
201     s="  s2";
202     trim_mod(s);
203     BOOST_CHECK_EQUAL( std::string("s2"), s );
204
205     s="s3   ";
206     trim_mod(s);
207     BOOST_CHECK_EQUAL( std::string("s3"), s );
208
209     s="::s4:s4::++--aa";
210     trim_mod(s,":+-a");
211     BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
212
213     /* non modifying version */
214
215     s= "s1";
216     BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
217
218     s="  s2";
219     BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
220     BOOST_CHECK_EQUAL( std::string("  s2"), s );
221
222     s="s3   ";
223     BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
224     BOOST_CHECK_EQUAL( std::string("s3   "), s );
225
226     s="::s4:s4::++--aa";
227     BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
228 } // eo TestTrim()
229
230
231
232 BOOST_AUTO_TEST_CASE(TestChomp)
233 {
234     std::string s("s1");
235
236     chomp_mod(s);
237     BOOST_CHECK_EQUAL( std::string("s1"), s );
238
239     s="s2\n";
240     chomp_mod(s);
241     BOOST_CHECK_EQUAL( std::string("s2"), s );
242
243     s="s3:";
244     chomp_mod(s,":");
245     BOOST_CHECK_EQUAL( std::string("s3"), s );
246
247     s=":s4::";
248     chomp_mod(s,"s:");
249     BOOST_CHECK_EQUAL( std::string(":s4:"), s);
250
251     /* non modifiying versions */
252     s= "s1";
253
254     BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
255
256     s="s2\n";
257     BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
258     BOOST_CHECK_EQUAL( std::string("s2\n"), s);
259
260     s="s3:";
261     BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
262     BOOST_CHECK_EQUAL( std::string("s3:"), s);
263
264     s=":s4::";
265     BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
266     BOOST_CHECK_EQUAL( std::string(":s4::"), s);
267 } // eo TestChomp()
268
269
270
271 BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
272 {
273     std::string s1("12.cpp");
274
275     BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
276     BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
277     BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
278     BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
279     BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
280
281     std::string s1c1= remove_suffix(s1,".cpp");
282     BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
283
284     std::string s1c2= remove_suffix(s1,"p");
285     BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
286
287     std::string s1c3= remove_suffix(s1,"cp");
288     BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
289
290     std::string s2(".cpp");
291     BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
292
293     std::string s2c1= remove_suffix(s2,".cpp");
294     BOOST_CHECK_EQUAL( std::string(""), s2c1 );
295
296 } // eo TestSuffixFuncs()
297
298
299
300 BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
301 {
302     std::string s1("12.cpp");
303
304     BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
305     BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
306     BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
307     BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
308
309     std::string s1c1= remove_prefix(s1, "12");
310     BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
311 } // eo TestPrefixFuncs()
312
313
314
315 BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
316 {
317     std::string u1("CASE CONVERSION TEST..");
318     std::string l1("case conversion test..");
319
320     std::string test1(l1);
321
322     to_upper_mod(test1);
323     BOOST_CHECK_EQUAL( u1, test1 );
324
325     to_lower_mod(test1);
326     BOOST_CHECK_EQUAL( l1, test1 );
327
328
329     BOOST_CHECK_EQUAL( u1, to_upper(l1) );
330     BOOST_CHECK_EQUAL( l1, to_lower(u1) );
331 } // eo TestLowerUpper
332
333
334
335 BOOST_AUTO_TEST_CASE(PairSplit1)
336 {
337     StringList str_list;
338     get_push_back_filler(str_list)
339         ("a=11")("a= 11")("a =11 ")("a =  11 ")("  a    =    11   ")
340     ;
341     BOOST_CHECK_EQUAL( 5u, str_list.size() );
342     for(StringList::iterator it= str_list.begin();
343         it != str_list.end();
344         ++it)
345     {
346         std::string key, value;
347         bool res= pair_split( *it, key, value);
348
349         BOOST_CHECK_EQUAL( true , res );
350         BOOST_CHECK_EQUAL( std::string("a"), key );
351         BOOST_CHECK_EQUAL( std::string("11"), value );
352     }
353
354     std::string key, value;
355     bool res;
356
357     res= pair_split(" 1 : 2 ", key, value, ':');
358     BOOST_CHECK_EQUAL( true, res );
359     BOOST_CHECK_EQUAL( std::string("1"), key);
360     BOOST_CHECK_EQUAL( std::string("2"), value);
361 } // eo PairSplit1
362
363
364
365 BOOST_AUTO_TEST_CASE(SplitString1)
366 {
367     std::string block(
368         "Zeile 1\n"
369         "++Zeile-2--\n"
370         "Zeile 3\n"
371         "\n"
372         "Zeile 5\n"
373     );
374
375     StringList list1;
376
377     split_string(block, list1, "\n");
378     // since the blocks ends with \n we should have 6 lines (the last one empty):
379     BOOST_CHECK_EQUAL( 6u, list1.size() );
380     BOOST_CHECK_EQUAL( std::string(), list1.back() );
381     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
382
383     StringList list2;
384
385     split_string(block, list2, "\n", true);
386
387     // now we omitted empty lines, now we should have only 4 lines left:
388     BOOST_CHECK_EQUAL( 4u, list2.size() );
389     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
390     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
391
392     list2= split_string(block, "\n", true, "+-");
393
394     // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
395     BOOST_CHECK_EQUAL( 4u, list2.size() );
396     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
397     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
398     BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
399 } // eo SplitString1
400
401
402
403 BOOST_AUTO_TEST_CASE(SplitString2)
404 {
405     std::string line("172.16.0.0/16 dev eth0  scope link  src 172.16.1.111");
406
407     StringList list1;
408
409     split_string(line, list1, " ", true, Whitespaces);
410
411     BOOST_CHECK_EQUAL( 7u, list1.size() );
412
413 } // eo SplitString2
414
415
416
417 BOOST_AUTO_TEST_CASE(SplitStringEmpty)
418 {
419     std::string line("");
420
421     StringList list1;
422
423     split_string(line, list1, " ", true, Whitespaces);
424
425     BOOST_CHECK_EQUAL( 0u, list1.size() );
426 } // eo SplitStringEmpty
427
428
429 BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
430 {
431     std::string line(" ");
432
433     StringList list1;
434
435     split_string(line, list1, " ", true, Whitespaces);
436
437     BOOST_CHECK_EQUAL( 0u, list1.size() );
438 } // eo SplitStringDelimiterOnly
439
440
441
442 BOOST_AUTO_TEST_CASE(JoinString1)
443 {
444     std::list< std::string > parts;
445     get_push_back_filler(parts)("1")("2")("drei");
446
447     std::string joined_string= join_string(parts,"/");
448     // we should have slashes between the strings:
449     BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
450
451     parts.push_back( std::string() );
452     joined_string= join_string(parts,"/");
453     // now we should have an additional trailing slash:
454     BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
455
456     parts.push_front( std::string() );
457     joined_string= join_string(parts,"/");
458     // now we should have an additional leading slash:
459     BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
460
461 } // eo JoinString1
462
463
464
465 BOOST_AUTO_TEST_CASE(ConversionStringInt)
466 {
467     std::string s1("24");
468     std::string s1x("25x");
469     int i1=0;
470     bool res= false;
471
472     i1= string_to<int>(s1);
473     BOOST_CHECK_EQUAL( 24, i1 );
474     i1= string_to<int>(s1x);
475     BOOST_CHECK_EQUAL( 25, i1 );
476
477     res= string_to<int>(s1,i1);
478     BOOST_CHECK_EQUAL( true, res );
479     BOOST_CHECK_EQUAL( 24, i1 );
480
481     res= string_to<int>(s1x,i1);
482     BOOST_CHECK_EQUAL( false, res );
483
484     std::string ss1= to_string( 24 );
485     BOOST_CHECK_EQUAL( std::string("24"), ss1);
486
487 } // eo ConversionStringInt()
488
489
490
491 BOOST_AUTO_TEST_CASE(HexConversion)
492 {
493     std::string hex1("49324E");
494     std::string bin1("I2N");
495
496     BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
497     BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
498     BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
499
500     std::string hex2("0001");
501     std::string hex2a("00 01");
502     std::string hex2b("00:01");
503     std::string bin2("\0\1",2);
504
505     BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
506     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
507     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
508     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
509
510     BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
511 } // eo HexConversion()
512
513 BOOST_AUTO_TEST_SUITE_END()