]> granicus.if.org Git - icinga2/blob - test/base-dictionary.cpp
Merge branch 'feature/freebsd-tests-4990' into next
[icinga2] / test / base-dictionary.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 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/dictionary.h"
21 #include "base/objectlock.h"
22 #include <boost/test/unit_test.hpp>
23 #include <boost/smart_ptr/make_shared.hpp>
24 #include <boost/foreach.hpp>
25 #include <boost/tuple/tuple.hpp>
26
27 using namespace icinga;
28
29 BOOST_AUTO_TEST_SUITE(base_dictionary)
30
31 BOOST_AUTO_TEST_CASE(construct)
32 {
33         Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
34         BOOST_CHECK(dictionary);
35 }
36
37 BOOST_AUTO_TEST_CASE(get1)
38 {
39         Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
40         dictionary->Set("test1", 7);
41         dictionary->Set("test2", "hello world");
42
43         BOOST_CHECK(dictionary->GetLength() == 2);
44
45         Value test1;
46         test1 = dictionary->Get("test1");
47         BOOST_CHECK(test1 == 7);
48
49         Value test2;
50         test2 = dictionary->Get("test2");
51         BOOST_CHECK(test2 == "hello world");
52
53         String key3 = "test3";
54         Value test3;
55         test3 = dictionary->Get(key3);
56         BOOST_CHECK(test3.IsEmpty());
57 }
58
59 BOOST_AUTO_TEST_CASE(get2)
60 {
61         Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
62         Dictionary::Ptr other = boost::make_shared<Dictionary>();
63
64         dictionary->Set("test1", other);
65
66         BOOST_CHECK(dictionary->GetLength() == 1);
67
68         Dictionary::Ptr test1 = dictionary->Get("test1");
69         BOOST_CHECK(other == test1);
70
71         Dictionary::Ptr test2 = dictionary->Get("test2");
72         BOOST_CHECK(!test2);
73 }
74
75 BOOST_AUTO_TEST_CASE(foreach)
76 {
77         Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
78         dictionary->Set("test1", 7);
79         dictionary->Set("test2", "hello world");
80
81         ObjectLock olock(dictionary);
82
83         bool seen_test1 = false, seen_test2 = false;
84
85         String key;
86         Value value;
87         BOOST_FOREACH(boost::tie(key, value), dictionary) {
88                 BOOST_CHECK(key == "test1" || key == "test2");
89
90                 if (key == "test1") {
91                         BOOST_CHECK(!seen_test1);
92                         seen_test1 = true;
93
94                         BOOST_CHECK(value == 7);
95
96                         continue;
97                 } else if (key == "test2") {
98                         BOOST_CHECK(!seen_test2);
99                         seen_test2 = true;
100
101                         BOOST_CHECK(value == "hello world");
102                 }
103         }
104
105         BOOST_CHECK(seen_test1);
106         BOOST_CHECK(seen_test2);
107 }
108
109 BOOST_AUTO_TEST_CASE(remove)
110 {
111         Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
112
113         dictionary->Set("test1", 7);
114         dictionary->Set("test2", "hello world");
115
116         BOOST_CHECK(dictionary->Contains("test1"));
117         BOOST_CHECK(dictionary->GetLength() == 2);
118
119         dictionary->Set("test1", Empty);
120
121         BOOST_CHECK(!dictionary->Contains("test1"));
122         BOOST_CHECK(dictionary->GetLength() == 1);
123
124         dictionary->Remove("test2");
125
126         BOOST_CHECK(!dictionary->Contains("test2"));
127         BOOST_CHECK(dictionary->GetLength() == 0);
128
129         dictionary->Set("test1", 7);
130         dictionary->Set("test2", "hello world");
131
132         {
133                 ObjectLock olock(dictionary);
134
135                 Dictionary::Iterator it = dictionary->Begin();
136                 dictionary->Remove(it);
137         }
138
139         BOOST_CHECK(dictionary->GetLength() == 1);
140 }
141
142 BOOST_AUTO_TEST_CASE(clone)
143 {
144         Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
145
146         dictionary->Set("test1", 7);
147         dictionary->Set("test2", "hello world");
148
149         Dictionary::Ptr clone = dictionary->ShallowClone();
150
151         BOOST_CHECK(dictionary != clone);
152
153         BOOST_CHECK(clone->GetLength() == 2);
154         BOOST_CHECK(clone->Get("test1") == 7);
155         BOOST_CHECK(clone->Get("test2") == "hello world");
156
157         clone->Set("test3", 5);
158         BOOST_CHECK(!dictionary->Contains("test3"));
159         BOOST_CHECK(dictionary->GetLength() == 2);
160
161         clone->Set("test2", "test");
162         BOOST_CHECK(dictionary->Get("test2") == "hello world");
163 }
164
165 BOOST_AUTO_TEST_CASE(serialize)
166 {
167         Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
168
169         dictionary->Set("test1", 7);
170         dictionary->Set("test2", "hello world");
171
172         String json = Value(dictionary).Serialize();
173         BOOST_CHECK(json.GetLength() > 0);
174         Dictionary::Ptr deserialized = Value::Deserialize(json);
175         BOOST_CHECK(deserialized->GetLength() == 2);
176         BOOST_CHECK(deserialized->Get("test1") == 7);
177         BOOST_CHECK(deserialized->Get("test2") == "hello world");
178 }
179
180 BOOST_AUTO_TEST_SUITE_END()