1 /******************************************************************************
3 * Copyright (C) 2012-2013 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 <boost/test/unit_test.hpp>
23 #include <boost/smart_ptr/make_shared.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 = boost::make_shared<Dictionary>();
34 BOOST_CHECK(dictionary);
37 BOOST_AUTO_TEST_CASE(get1)
39 Dictionary::Ptr dictionary = boost::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 = boost::make_shared<Dictionary>();
62 Dictionary::Ptr other = boost::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 = boost::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;
87 BOOST_FOREACH(boost::tie(key, value), dictionary) {
88 BOOST_CHECK(key == "test1" || key == "test2");
91 BOOST_CHECK(!seen_test1);
94 BOOST_CHECK(value == 7);
97 } else if (key == "test2") {
98 BOOST_CHECK(!seen_test2);
101 BOOST_CHECK(value == "hello world");
105 BOOST_CHECK(seen_test1);
106 BOOST_CHECK(seen_test2);
109 BOOST_AUTO_TEST_CASE(remove)
111 Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
113 dictionary->Set("test1", 7);
114 dictionary->Set("test2", "hello world");
116 BOOST_CHECK(dictionary->Contains("test1"));
117 BOOST_CHECK(dictionary->GetLength() == 2);
119 dictionary->Set("test1", Empty);
121 BOOST_CHECK(!dictionary->Contains("test1"));
122 BOOST_CHECK(dictionary->GetLength() == 1);
124 dictionary->Remove("test2");
126 BOOST_CHECK(!dictionary->Contains("test2"));
127 BOOST_CHECK(dictionary->GetLength() == 0);
129 dictionary->Set("test1", 7);
130 dictionary->Set("test2", "hello world");
133 ObjectLock olock(dictionary);
135 Dictionary::Iterator it = dictionary->Begin();
136 dictionary->Remove(it);
139 BOOST_CHECK(dictionary->GetLength() == 1);
142 BOOST_AUTO_TEST_CASE(clone)
144 Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
146 dictionary->Set("test1", 7);
147 dictionary->Set("test2", "hello world");
149 Dictionary::Ptr clone = dictionary->ShallowClone();
151 BOOST_CHECK(dictionary != clone);
153 BOOST_CHECK(clone->GetLength() == 2);
154 BOOST_CHECK(clone->Get("test1") == 7);
155 BOOST_CHECK(clone->Get("test2") == "hello world");
157 clone->Set("test3", 5);
158 BOOST_CHECK(!dictionary->Contains("test3"));
159 BOOST_CHECK(dictionary->GetLength() == 2);
161 clone->Set("test2", "test");
162 BOOST_CHECK(dictionary->Get("test2") == "hello world");
165 BOOST_AUTO_TEST_CASE(serialize)
167 Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
169 dictionary->Set("test1", 7);
170 dictionary->Set("test2", "hello world");
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");
180 BOOST_AUTO_TEST_SUITE_END()