1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
20 #include "base/dictionary.h"
21 #include "base/objectlock.h"
22 #include "base/serializer.h"
23 #include <boost/test/unit_test.hpp>
24 #include <boost/foreach.hpp>
25 #include <boost/tuple/tuple.hpp>
27 using namespace icinga;
29 BOOST_AUTO_TEST_SUITE(base_dictionary)
31 BOOST_AUTO_TEST_CASE(construct)
33 Dictionary::Ptr dictionary = make_shared<Dictionary>();
34 BOOST_CHECK(dictionary);
37 BOOST_AUTO_TEST_CASE(get1)
39 Dictionary::Ptr dictionary = make_shared<Dictionary>();
40 dictionary->Set("test1", 7);
41 dictionary->Set("test2", "hello world");
43 BOOST_CHECK(dictionary->GetLength() == 2);
46 test1 = dictionary->Get("test1");
47 BOOST_CHECK(test1 == 7);
50 test2 = dictionary->Get("test2");
51 BOOST_CHECK(test2 == "hello world");
53 String key3 = "test3";
55 test3 = dictionary->Get(key3);
56 BOOST_CHECK(test3.IsEmpty());
59 BOOST_AUTO_TEST_CASE(get2)
61 Dictionary::Ptr dictionary = make_shared<Dictionary>();
62 Dictionary::Ptr other = make_shared<Dictionary>();
64 dictionary->Set("test1", other);
66 BOOST_CHECK(dictionary->GetLength() == 1);
68 Dictionary::Ptr test1 = dictionary->Get("test1");
69 BOOST_CHECK(other == test1);
71 Dictionary::Ptr test2 = dictionary->Get("test2");
75 BOOST_AUTO_TEST_CASE(foreach)
77 Dictionary::Ptr dictionary = make_shared<Dictionary>();
78 dictionary->Set("test1", 7);
79 dictionary->Set("test2", "hello world");
81 ObjectLock olock(dictionary);
83 bool seen_test1 = false, seen_test2 = false;
85 BOOST_FOREACH(const Dictionary::Pair& kv, dictionary) {
86 BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
88 if (kv.first == "test1") {
89 BOOST_CHECK(!seen_test1);
92 BOOST_CHECK(kv.second == 7);
95 } else if (kv.first == "test2") {
96 BOOST_CHECK(!seen_test2);
99 BOOST_CHECK(kv.second == "hello world");
103 BOOST_CHECK(seen_test1);
104 BOOST_CHECK(seen_test2);
107 BOOST_AUTO_TEST_CASE(remove)
109 Dictionary::Ptr dictionary = make_shared<Dictionary>();
111 dictionary->Set("test1", 7);
112 dictionary->Set("test2", "hello world");
114 BOOST_CHECK(dictionary->Contains("test1"));
115 BOOST_CHECK(dictionary->GetLength() == 2);
117 dictionary->Set("test1", Empty);
119 BOOST_CHECK(dictionary->Contains("test1"));
120 BOOST_CHECK(dictionary->GetLength() == 2);
122 dictionary->Remove("test1");
124 BOOST_CHECK(!dictionary->Contains("test1"));
125 BOOST_CHECK(dictionary->GetLength() == 1);
127 dictionary->Remove("test2");
129 BOOST_CHECK(!dictionary->Contains("test2"));
130 BOOST_CHECK(dictionary->GetLength() == 0);
132 dictionary->Set("test1", 7);
133 dictionary->Set("test2", "hello world");
136 ObjectLock olock(dictionary);
138 Dictionary::Iterator it = dictionary->Begin();
139 dictionary->Remove(it);
142 BOOST_CHECK(dictionary->GetLength() == 1);
145 BOOST_AUTO_TEST_CASE(clone)
147 Dictionary::Ptr dictionary = make_shared<Dictionary>();
149 dictionary->Set("test1", 7);
150 dictionary->Set("test2", "hello world");
152 Dictionary::Ptr clone = dictionary->ShallowClone();
154 BOOST_CHECK(dictionary != clone);
156 BOOST_CHECK(clone->GetLength() == 2);
157 BOOST_CHECK(clone->Get("test1") == 7);
158 BOOST_CHECK(clone->Get("test2") == "hello world");
160 clone->Set("test3", 5);
161 BOOST_CHECK(!dictionary->Contains("test3"));
162 BOOST_CHECK(dictionary->GetLength() == 2);
164 clone->Set("test2", "test");
165 BOOST_CHECK(dictionary->Get("test2") == "hello world");
168 BOOST_AUTO_TEST_CASE(json)
170 Dictionary::Ptr dictionary = make_shared<Dictionary>();
172 dictionary->Set("test1", 7);
173 dictionary->Set("test2", "hello world");
175 String json = JsonSerialize(dictionary);
176 BOOST_CHECK(json.GetLength() > 0);
177 Dictionary::Ptr deserialized = JsonDeserialize(json);
178 BOOST_CHECK(deserialized->GetLength() == 2);
179 BOOST_CHECK(deserialized->Get("test1") == 7);
180 BOOST_CHECK(deserialized->Get("test2") == "hello world");
183 BOOST_AUTO_TEST_SUITE_END()