Created test cases for format_kb()
[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
89
90 BOOST_AUTO_TEST_CASE(html_entities1)
91 {
92     string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
93     BOOST_CHECK_EQUAL(string("&auml;&ouml;&uuml;"), output);
94 }
95
96 BOOST_AUTO_TEST_CASE(html_entities2)
97 {
98     string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
99     BOOST_CHECK_EQUAL(string("&#229; &#245; &#255;"), output);
100 }
101
102 BOOST_AUTO_TEST_CASE(html_entities3)
103 {
104     string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
105     BOOST_CHECK_EQUAL(string("&#270; &#3889; &#32904;"), output);
106 }
107
108
109
110 BOOST_AUTO_TEST_CASE(nice_unit_format1)
111 {
112     const int64_t two_bytes = 2;
113
114     string output = nice_unit_format(two_bytes);
115     BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
116
117 //    output = nice_unit_format(two_bytes, US_IEC, SHORT);
118     output = format_kb(two_bytes);
119     BOOST_CHECK_EQUAL(string("2.0 B"), output);
120 }
121
122 BOOST_AUTO_TEST_CASE(nice_unit_format2)
123 {
124     const int64_t two_kilobytes = 2000;
125
126     string output = nice_unit_format(two_kilobytes);
127     BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
128
129     //    output = nice_unit_format(two_kilobytes, US_IEC);
130     output = format_kb(two_kilobytes);
131     BOOST_CHECK_EQUAL(string("2.0 KB"), output);
132
133     const int64_t two_and_half_kilobytes = 2500;
134
135     output = nice_unit_format(two_and_half_kilobytes);
136     BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
137
138     //    output = nice_unit_format(two_and_half_kilobytes, US_IEC);
139     output = format_kb(two_and_half_kilobytes);
140     BOOST_CHECK_EQUAL(string("2.4 KB"), output);
141 }
142
143 BOOST_AUTO_TEST_CASE(nice_unit_format3)
144 {
145     const int64_t two_megabytes = 2000000;
146
147     string output = nice_unit_format(two_megabytes);
148     BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
149
150     output = format_kb(two_megabytes);
151     BOOST_CHECK_EQUAL(string("1.9 MB"), output);
152
153     const int64_t two_and_half_megabytes = 2500000;
154
155     output = nice_unit_format(two_and_half_megabytes);
156     BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
157
158     output = format_kb(two_and_half_megabytes);
159     BOOST_CHECK_EQUAL(string("2.4 MB"), output);
160
161 }
162
163 BOOST_AUTO_TEST_CASE(nice_unit_format4)
164 {
165     const int64_t two_gigabytes = 2000000000LL;
166
167     string output = nice_unit_format(two_gigabytes);
168     BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
169
170     output = format_kb(two_gigabytes);
171     BOOST_CHECK_EQUAL(string("1.9 GB"), output);
172
173     const int64_t two_and_half_gigabytes = 2500000000LL;
174
175     output = nice_unit_format(two_and_half_gigabytes);
176     BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
177
178     output = format_kb(two_and_half_gigabytes);
179     BOOST_CHECK_EQUAL(string("2.3 GB"), output);
180 }
181
182 BOOST_AUTO_TEST_CASE(nice_unit_format5)
183 {
184     const int64_t two_terabytes = 2000000000000LL;
185
186     string output = nice_unit_format(two_terabytes);
187     BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
188
189     output = format_kb(two_terabytes);
190     BOOST_CHECK_EQUAL(string("1.8 TB"), output);
191
192     const int64_t two_and_half_terabytes = 2500000000000LL;
193
194     output = nice_unit_format(two_and_half_terabytes);
195     BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
196
197     output = format_kb(two_and_half_terabytes);
198     BOOST_CHECK_EQUAL(string("2.3 TB"), output);
199 }
200
201 BOOST_AUTO_TEST_CASE(nice_unit_format6)
202 {
203     const int64_t two_petabytes = 2000000000000000LL;
204
205     string output = nice_unit_format(two_petabytes);
206     BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
207
208     output = format_kb(two_petabytes);
209     BOOST_CHECK_EQUAL(string("1.8 PB"), output);
210
211     const int64_t two_and_half_petabytes = 2500000000000000LL;
212
213     output = nice_unit_format(two_and_half_petabytes);
214     BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
215
216     output = format_kb(two_and_half_petabytes);
217     BOOST_CHECK_EQUAL(string("2.2 PB"), output);
218 }
219
220 BOOST_AUTO_TEST_CASE(nice_unit_format7)
221 {
222     const int64_t two_exabytes = 2000000000000000000LL;
223
224     string output = nice_unit_format(two_exabytes);
225     BOOST_CHECK_EQUAL(string("2.00 EBytes"), output);
226
227     output = format_kb(two_exabytes);
228     BOOST_CHECK_EQUAL(string("1.7 EB"), output);
229
230     const int64_t two_and_half_exabytes = 2500000000000000000LL;
231
232     output = nice_unit_format(two_and_half_exabytes);
233     BOOST_CHECK_EQUAL(string("2.50 EBytes"), output);
234
235     output = format_kb(two_and_half_exabytes);
236     BOOST_CHECK_EQUAL(string("2.2 EB"), output);
237 }
238
239
240
241 BOOST_AUTO_TEST_CASE(imaputf7_to_utf8)
242 {
243     string output = utf7imap_to_utf8("Sp&AOQ-m");
244     BOOST_CHECK_EQUAL(string("Späm"), output);
245 }
246
247 BOOST_AUTO_TEST_CASE(utf8_to_imaputf7)
248 {
249     string output = utf8_to_utf7imap("Späm");
250     BOOST_CHECK_EQUAL(string("Sp&AOQ-m"), output);
251 }
252
253 /*
254 **
255 */
256
257 BOOST_AUTO_TEST_CASE(TestTrim)
258 {
259     std::string s("s1");
260     trim_mod(s);
261     BOOST_CHECK_EQUAL( std::string("s1"), s );
262
263     s="  s2";
264     trim_mod(s);
265     BOOST_CHECK_EQUAL( std::string("s2"), s );
266
267     s="s3   ";
268     trim_mod(s);
269     BOOST_CHECK_EQUAL( std::string("s3"), s );
270
271     s="::s4:s4::++--aa";
272     trim_mod(s,":+-a");
273     BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
274
275     /* non modifying version */
276
277     s= "s1";
278     BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
279
280     s="  s2";
281     BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
282     BOOST_CHECK_EQUAL( std::string("  s2"), s );
283
284     s="s3   ";
285     BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
286     BOOST_CHECK_EQUAL( std::string("s3   "), s );
287
288     s="::s4:s4::++--aa";
289     BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
290 } // eo TestTrim()
291
292
293
294 BOOST_AUTO_TEST_CASE(TestChomp)
295 {
296     std::string s("s1");
297
298     chomp_mod(s);
299     BOOST_CHECK_EQUAL( std::string("s1"), s );
300
301     s="s2\n";
302     chomp_mod(s);
303     BOOST_CHECK_EQUAL( std::string("s2"), s );
304
305     s="s3:";
306     chomp_mod(s,":");
307     BOOST_CHECK_EQUAL( std::string("s3"), s );
308
309     s=":s4::";
310     chomp_mod(s,"s:");
311     BOOST_CHECK_EQUAL( std::string(":s4:"), s);
312
313     /* non modifiying versions */
314     s= "s1";
315
316     BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
317
318     s="s2\n";
319     BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
320     BOOST_CHECK_EQUAL( std::string("s2\n"), s);
321
322     s="s3:";
323     BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
324     BOOST_CHECK_EQUAL( std::string("s3:"), s);
325
326     s=":s4::";
327     BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
328     BOOST_CHECK_EQUAL( std::string(":s4::"), s);
329 } // eo TestChomp()
330
331
332
333 BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
334 {
335     std::string s1("12.cpp");
336
337     BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
338     BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
339     BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
340     BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
341     BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
342
343     std::string s1c1= remove_suffix(s1,".cpp");
344     BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
345
346     std::string s1c2= remove_suffix(s1,"p");
347     BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
348
349     std::string s1c3= remove_suffix(s1,"cp");
350     BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
351
352     std::string s2(".cpp");
353     BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
354
355     std::string s2c1= remove_suffix(s2,".cpp");
356     BOOST_CHECK_EQUAL( std::string(""), s2c1 );
357
358 } // eo TestSuffixFuncs()
359
360
361
362 BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
363 {
364     std::string s1("12.cpp");
365
366     BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
367     BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
368     BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
369     BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
370
371     std::string s1c1= remove_prefix(s1, "12");
372     BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
373 } // eo TestPrefixFuncs()
374
375
376
377 BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
378 {
379     std::string u1("CASE CONVERSION TEST..");
380     std::string l1("case conversion test..");
381
382     std::string test1(l1);
383
384     to_upper_mod(test1);
385     BOOST_CHECK_EQUAL( u1, test1 );
386
387     to_lower_mod(test1);
388     BOOST_CHECK_EQUAL( l1, test1 );
389
390
391     BOOST_CHECK_EQUAL( u1, to_upper(l1) );
392     BOOST_CHECK_EQUAL( l1, to_lower(u1) );
393 } // eo TestLowerUpper
394
395
396
397 BOOST_AUTO_TEST_CASE(PairSplit1)
398 {
399     StringList str_list;
400     get_push_back_filler(str_list)
401         ("a=11")("a= 11")("a =11 ")("a =  11 ")("  a    =    11   ")
402     ;
403     BOOST_CHECK_EQUAL( 5u, str_list.size() );
404     for(StringList::iterator it= str_list.begin();
405         it != str_list.end();
406         ++it)
407     {
408         std::string key, value;
409         bool res= pair_split( *it, key, value);
410
411         BOOST_CHECK_EQUAL( true , res );
412         BOOST_CHECK_EQUAL( std::string("a"), key );
413         BOOST_CHECK_EQUAL( std::string("11"), value );
414     }
415
416     std::string key, value;
417     bool res;
418
419     res= pair_split(" 1 : 2 ", key, value, ':');
420     BOOST_CHECK_EQUAL( true, res );
421     BOOST_CHECK_EQUAL( std::string("1"), key);
422     BOOST_CHECK_EQUAL( std::string("2"), value);
423 } // eo PairSplit1
424
425
426
427 BOOST_AUTO_TEST_CASE(SplitString1)
428 {
429     std::string block(
430         "Zeile 1\n"
431         "++Zeile-2--\n"
432         "Zeile 3\n"
433         "\n"
434         "Zeile 5\n"
435     );
436
437     StringList list1;
438
439     split_string(block, list1, "\n");
440     // since the blocks ends with \n we should have 6 lines (the last one empty):
441     BOOST_CHECK_EQUAL( 6u, list1.size() );
442     BOOST_CHECK_EQUAL( std::string(), list1.back() );
443     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
444
445     StringList list2;
446
447     split_string(block, list2, "\n", true);
448
449     // now we omitted empty lines, now we should have only 4 lines left:
450     BOOST_CHECK_EQUAL( 4u, list2.size() );
451     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
452     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
453
454     list2= split_string(block, "\n", true, "+-");
455
456     // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
457     BOOST_CHECK_EQUAL( 4u, list2.size() );
458     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
459     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
460     BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
461 } // eo SplitString1
462
463
464
465 BOOST_AUTO_TEST_CASE(SplitString2)
466 {
467     std::string line("172.16.0.0/16 dev eth0  scope link  src 172.16.1.111");
468
469     StringList list1;
470
471     split_string(line, list1, " ", true, Whitespaces);
472
473     BOOST_CHECK_EQUAL( 7u, list1.size() );
474
475 } // eo SplitString2
476
477
478
479 BOOST_AUTO_TEST_CASE(SplitStringEmpty)
480 {
481     std::string line("");
482
483     StringList list1;
484
485     split_string(line, list1, " ", true, Whitespaces);
486
487     BOOST_CHECK_EQUAL( 0u, list1.size() );
488 } // eo SplitStringEmpty
489
490
491 BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
492 {
493     std::string line(" ");
494
495     StringList list1;
496
497     split_string(line, list1, " ", true, Whitespaces);
498
499     BOOST_CHECK_EQUAL( 0u, list1.size() );
500 } // eo SplitStringDelimiterOnly
501
502
503
504 BOOST_AUTO_TEST_CASE(JoinString1)
505 {
506     std::list< std::string > parts;
507     get_push_back_filler(parts)("1")("2")("drei");
508
509     std::string joined_string= join_string(parts,"/");
510     // we should have slashes between the strings:
511     BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
512
513     parts.push_back( std::string() );
514     joined_string= join_string(parts,"/");
515     // now we should have an additional trailing slash:
516     BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
517
518     parts.push_front( std::string() );
519     joined_string= join_string(parts,"/");
520     // now we should have an additional leading slash:
521     BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
522
523 } // eo JoinString1
524
525
526
527 BOOST_AUTO_TEST_CASE(ConversionStringInt)
528 {
529     std::string s1("24");
530     std::string s1x("25x");
531     int i1=0;
532     bool res= false;
533
534     i1= string_to<int>(s1);
535     BOOST_CHECK_EQUAL( 24, i1 );
536     i1= string_to<int>(s1x);
537     BOOST_CHECK_EQUAL( 25, i1 );
538
539     res= string_to<int>(s1,i1);
540     BOOST_CHECK_EQUAL( true, res );
541     BOOST_CHECK_EQUAL( 24, i1 );
542
543     res= string_to<int>(s1x,i1);
544     BOOST_CHECK_EQUAL( false, res );
545
546     std::string ss1= to_string( 24 );
547     BOOST_CHECK_EQUAL( std::string("24"), ss1);
548
549 } // eo ConversionStringInt()
550
551
552
553 BOOST_AUTO_TEST_CASE(HexConversion)
554 {
555     std::string hex1("49324E");
556     std::string bin1("I2N");
557
558     BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
559     BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
560     BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
561
562     std::string hex2("0001");
563     std::string hex2a("00 01");
564     std::string hex2b("00:01");
565     std::string bin2("\0\1",2);
566
567     BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
568     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
569     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
570     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
571
572     BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
573 } // eo HexConversion()
574
575 BOOST_AUTO_TEST_SUITE_END()