]> granicus.if.org Git - icinga2/blob - test/base-string.cpp
Cli: Add blacklist/whitelist commands for agent commands
[icinga2] / test / base-string.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/string.hpp"
21 #include <boost/test/unit_test.hpp>
22 #include <boost/foreach.hpp>
23
24 using namespace icinga;
25
26 BOOST_AUTO_TEST_SUITE(base_string)
27
28 BOOST_AUTO_TEST_CASE(construct)
29 {
30         BOOST_CHECK(String() == "");
31         BOOST_CHECK(String(5, 'n') == "nnnnn");
32 }
33
34 BOOST_AUTO_TEST_CASE(equal)
35 {
36         BOOST_CHECK(String("hello") == String("hello"));
37         BOOST_CHECK("hello" == String("hello"));
38         BOOST_CHECK(String("hello") == String("hello"));
39
40         BOOST_CHECK(String("hello") != String("helloworld"));
41         BOOST_CHECK("hello" != String("helloworld"));
42         BOOST_CHECK(String("hello") != "helloworld");
43 }
44
45 BOOST_AUTO_TEST_CASE(clear)
46 {
47         String s = "hello";
48         s.Clear();
49         BOOST_CHECK(s == "");
50         BOOST_CHECK(s.IsEmpty());
51 }
52
53 BOOST_AUTO_TEST_CASE(append)
54 {
55         String s;
56         s += "he";
57         s += String("ll");
58         s += 'o';
59
60         BOOST_CHECK(s == "hello");
61 }
62
63 BOOST_AUTO_TEST_CASE(trim)
64 {
65         String s1 = "hello";
66         s1.Trim();
67         BOOST_CHECK(s1 == "hello");
68
69         String s2 = "  hello";
70         s2.Trim();
71         BOOST_CHECK(s2 == "hello");
72
73         String s3 = "hello ";
74         s3.Trim();
75         BOOST_CHECK(s3 == "hello");
76
77         String s4 = " hello ";
78         s4.Trim();
79         BOOST_CHECK(s4 == "hello");
80 }
81
82 BOOST_AUTO_TEST_CASE(contains)
83 {
84         String s1 = "hello world";
85         String s2 = "hello";
86         BOOST_CHECK(s1.Contains(s2));
87
88         String s3 = "  hello world  ";
89         String s4 = "  hello";
90         BOOST_CHECK(s3.Contains(s4));
91
92         String s5 = "  hello world  ";
93         String s6 = "world  ";
94         BOOST_CHECK(s5.Contains(s6));
95 }
96
97 BOOST_AUTO_TEST_CASE(replace)
98 {
99         String s = "hello";
100
101         s.Replace(0, 2, "x");
102         BOOST_CHECK(s == "xllo");
103 }
104
105 BOOST_AUTO_TEST_CASE(index)
106 {
107         String s = "hello";
108         BOOST_CHECK(s[0] == 'h');
109
110         s[0] = 'x';
111         BOOST_CHECK(s == "xello");
112
113         BOOST_FOREACH(char& ch, s) {
114                 ch = 'y';
115         }
116         BOOST_CHECK(s == "yyyyy");
117 }
118
119 BOOST_AUTO_TEST_CASE(find)
120 {
121         String s = "hello";
122         BOOST_CHECK(s.Find("ll") == 2);
123         BOOST_CHECK(s.FindFirstOf("xl") == 2);
124 }
125
126 BOOST_AUTO_TEST_SUITE_END()