1 /******************************************************************************
3 * Copyright (C) 2012-2017 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>
24 #include <boost/tuple/tuple.hpp>
26 using namespace icinga;
28 BOOST_AUTO_TEST_SUITE(base_dictionary)
30 BOOST_AUTO_TEST_CASE(construct)
32 Dictionary::Ptr dictionary = new Dictionary();
33 BOOST_CHECK(dictionary);
36 BOOST_AUTO_TEST_CASE(get1)
38 Dictionary::Ptr dictionary = new Dictionary();
39 dictionary->Set("test1", 7);
40 dictionary->Set("test2", "hello world");
42 BOOST_CHECK(dictionary->GetLength() == 2);
45 test1 = dictionary->Get("test1");
46 BOOST_CHECK(test1 == 7);
49 test2 = dictionary->Get("test2");
50 BOOST_CHECK(test2 == "hello world");
52 String key3 = "test3";
54 test3 = dictionary->Get(key3);
55 BOOST_CHECK(test3.IsEmpty());
58 BOOST_AUTO_TEST_CASE(get2)
60 Dictionary::Ptr dictionary = new Dictionary();
61 Dictionary::Ptr other = new Dictionary();
63 dictionary->Set("test1", other);
65 BOOST_CHECK(dictionary->GetLength() == 1);
67 Dictionary::Ptr test1 = dictionary->Get("test1");
68 BOOST_CHECK(other == test1);
70 Dictionary::Ptr test2 = dictionary->Get("test2");
74 BOOST_AUTO_TEST_CASE(foreach)
76 Dictionary::Ptr dictionary = new Dictionary();
77 dictionary->Set("test1", 7);
78 dictionary->Set("test2", "hello world");
80 ObjectLock olock(dictionary);
82 bool seen_test1 = false, seen_test2 = false;
84 for (const Dictionary::Pair& kv : dictionary) {
85 BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
87 if (kv.first == "test1") {
88 BOOST_CHECK(!seen_test1);
91 BOOST_CHECK(kv.second == 7);
94 } else if (kv.first == "test2") {
95 BOOST_CHECK(!seen_test2);
98 BOOST_CHECK(kv.second == "hello world");
102 BOOST_CHECK(seen_test1);
103 BOOST_CHECK(seen_test2);
106 BOOST_AUTO_TEST_CASE(remove)
108 Dictionary::Ptr dictionary = new Dictionary();
110 dictionary->Set("test1", 7);
111 dictionary->Set("test2", "hello world");
113 BOOST_CHECK(dictionary->Contains("test1"));
114 BOOST_CHECK(dictionary->GetLength() == 2);
116 dictionary->Set("test1", Empty);
118 BOOST_CHECK(dictionary->Contains("test1"));
119 BOOST_CHECK(dictionary->GetLength() == 2);
121 dictionary->Remove("test1");
123 BOOST_CHECK(!dictionary->Contains("test1"));
124 BOOST_CHECK(dictionary->GetLength() == 1);
126 dictionary->Remove("test2");
128 BOOST_CHECK(!dictionary->Contains("test2"));
129 BOOST_CHECK(dictionary->GetLength() == 0);
131 dictionary->Set("test1", 7);
132 dictionary->Set("test2", "hello world");
135 ObjectLock olock(dictionary);
137 Dictionary::Iterator it = dictionary->Begin();
138 dictionary->Remove(it);
141 BOOST_CHECK(dictionary->GetLength() == 1);
144 BOOST_AUTO_TEST_CASE(clone)
146 Dictionary::Ptr dictionary = new Dictionary();
148 dictionary->Set("test1", 7);
149 dictionary->Set("test2", "hello world");
151 Dictionary::Ptr clone = dictionary->ShallowClone();
153 BOOST_CHECK(dictionary != clone);
155 BOOST_CHECK(clone->GetLength() == 2);
156 BOOST_CHECK(clone->Get("test1") == 7);
157 BOOST_CHECK(clone->Get("test2") == "hello world");
159 clone->Set("test3", 5);
160 BOOST_CHECK(!dictionary->Contains("test3"));
161 BOOST_CHECK(dictionary->GetLength() == 2);
163 clone->Set("test2", "test");
164 BOOST_CHECK(dictionary->Get("test2") == "hello world");
167 BOOST_AUTO_TEST_CASE(json)
169 Dictionary::Ptr dictionary = new Dictionary();
171 dictionary->Set("test1", 7);
172 dictionary->Set("test2", "hello world");
174 String json = JsonEncode(dictionary);
175 BOOST_CHECK(json.GetLength() > 0);
176 Dictionary::Ptr deserialized = JsonDecode(json);
177 BOOST_CHECK(deserialized->GetLength() == 2);
178 BOOST_CHECK(deserialized->Get("test1") == 7);
179 BOOST_CHECK(deserialized->Get("test2") == "hello world");
182 BOOST_AUTO_TEST_SUITE_END()