1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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.hpp"
21 #include "base/objectlock.hpp"
22 #include "base/json.hpp"
23 #include <BoostTestTargetConfig.h>
25 using namespace icinga;
27 BOOST_AUTO_TEST_SUITE(base_dictionary)
29 BOOST_AUTO_TEST_CASE(construct)
31 Dictionary::Ptr dictionary = new Dictionary();
32 BOOST_CHECK(dictionary);
35 BOOST_AUTO_TEST_CASE(get1)
37 Dictionary::Ptr dictionary = new Dictionary();
38 dictionary->Set("test1", 7);
39 dictionary->Set("test2", "hello world");
41 BOOST_CHECK(dictionary->GetLength() == 2);
44 test1 = dictionary->Get("test1");
45 BOOST_CHECK(test1 == 7);
48 test2 = dictionary->Get("test2");
49 BOOST_CHECK(test2 == "hello world");
51 String key3 = "test3";
53 test3 = dictionary->Get(key3);
54 BOOST_CHECK(test3.IsEmpty());
57 BOOST_AUTO_TEST_CASE(get2)
59 Dictionary::Ptr dictionary = new Dictionary();
60 Dictionary::Ptr other = new Dictionary();
62 dictionary->Set("test1", other);
64 BOOST_CHECK(dictionary->GetLength() == 1);
66 Dictionary::Ptr test1 = dictionary->Get("test1");
67 BOOST_CHECK(other == test1);
69 Dictionary::Ptr test2 = dictionary->Get("test2");
73 BOOST_AUTO_TEST_CASE(foreach)
75 Dictionary::Ptr dictionary = new Dictionary();
76 dictionary->Set("test1", 7);
77 dictionary->Set("test2", "hello world");
79 ObjectLock olock(dictionary);
81 bool seen_test1 = false, seen_test2 = false;
83 for (const Dictionary::Pair& kv : dictionary) {
84 BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
86 if (kv.first == "test1") {
87 BOOST_CHECK(!seen_test1);
90 BOOST_CHECK(kv.second == 7);
93 } else if (kv.first == "test2") {
94 BOOST_CHECK(!seen_test2);
97 BOOST_CHECK(kv.second == "hello world");
101 BOOST_CHECK(seen_test1);
102 BOOST_CHECK(seen_test2);
105 BOOST_AUTO_TEST_CASE(remove)
107 Dictionary::Ptr dictionary = new Dictionary();
109 dictionary->Set("test1", 7);
110 dictionary->Set("test2", "hello world");
112 BOOST_CHECK(dictionary->Contains("test1"));
113 BOOST_CHECK(dictionary->GetLength() == 2);
115 dictionary->Set("test1", Empty);
117 BOOST_CHECK(dictionary->Contains("test1"));
118 BOOST_CHECK(dictionary->GetLength() == 2);
120 dictionary->Remove("test1");
122 BOOST_CHECK(!dictionary->Contains("test1"));
123 BOOST_CHECK(dictionary->GetLength() == 1);
125 dictionary->Remove("test2");
127 BOOST_CHECK(!dictionary->Contains("test2"));
128 BOOST_CHECK(dictionary->GetLength() == 0);
130 dictionary->Set("test1", 7);
131 dictionary->Set("test2", "hello world");
134 ObjectLock olock(dictionary);
136 Dictionary::Iterator it = dictionary->Begin();
137 dictionary->Remove(it);
140 BOOST_CHECK(dictionary->GetLength() == 1);
143 BOOST_AUTO_TEST_CASE(clone)
145 Dictionary::Ptr dictionary = new Dictionary();
147 dictionary->Set("test1", 7);
148 dictionary->Set("test2", "hello world");
150 Dictionary::Ptr clone = dictionary->ShallowClone();
152 BOOST_CHECK(dictionary != clone);
154 BOOST_CHECK(clone->GetLength() == 2);
155 BOOST_CHECK(clone->Get("test1") == 7);
156 BOOST_CHECK(clone->Get("test2") == "hello world");
158 clone->Set("test3", 5);
159 BOOST_CHECK(!dictionary->Contains("test3"));
160 BOOST_CHECK(dictionary->GetLength() == 2);
162 clone->Set("test2", "test");
163 BOOST_CHECK(dictionary->Get("test2") == "hello world");
166 BOOST_AUTO_TEST_CASE(json)
168 Dictionary::Ptr dictionary = new Dictionary();
170 dictionary->Set("test1", 7);
171 dictionary->Set("test2", "hello world");
173 String json = JsonEncode(dictionary);
174 BOOST_CHECK(json.GetLength() > 0);
175 Dictionary::Ptr deserialized = JsonDecode(json);
176 BOOST_CHECK(deserialized->GetLength() == 2);
177 BOOST_CHECK(deserialized->Get("test1") == 7);
178 BOOST_CHECK(deserialized->Get("test2") == "hello world");
181 BOOST_AUTO_TEST_SUITE_END()