1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/dictionary.hpp"
4 #include "base/objectlock.hpp"
5 #include "base/json.hpp"
6 #include <BoostTestTargetConfig.h>
8 using namespace icinga;
10 BOOST_AUTO_TEST_SUITE(base_dictionary)
12 BOOST_AUTO_TEST_CASE(construct)
14 Dictionary::Ptr dictionary = new Dictionary();
15 BOOST_CHECK(dictionary);
18 BOOST_AUTO_TEST_CASE(initializer1)
22 dict.emplace_back("test1", "Gin-o-clock");
24 Dictionary::Ptr dictionary = new Dictionary(std::move(dict));
27 test1 = dictionary->Get("test1");
28 BOOST_CHECK(test1 == "Gin-o-clock");
31 BOOST_AUTO_TEST_CASE(initializer2)
33 Dictionary::Ptr dictionary = new Dictionary({ {"test1", "Gin-for-the-win"} });
36 test1 = dictionary->Get("test1");
37 BOOST_CHECK(test1 == "Gin-for-the-win");
40 BOOST_AUTO_TEST_CASE(get1)
42 Dictionary::Ptr dictionary = new Dictionary();
43 dictionary->Set("test1", 7);
44 dictionary->Set("test2", "hello world");
46 BOOST_CHECK(dictionary->GetLength() == 2);
49 test1 = dictionary->Get("test1");
50 BOOST_CHECK(test1 == 7);
53 test2 = dictionary->Get("test2");
54 BOOST_CHECK(test2 == "hello world");
56 String key3 = "test3";
58 test3 = dictionary->Get(key3);
59 BOOST_CHECK(test3.IsEmpty());
62 BOOST_AUTO_TEST_CASE(get2)
64 Dictionary::Ptr dictionary = new Dictionary();
65 Dictionary::Ptr other = new Dictionary();
67 dictionary->Set("test1", other);
69 BOOST_CHECK(dictionary->GetLength() == 1);
71 Dictionary::Ptr test1 = dictionary->Get("test1");
72 BOOST_CHECK(other == test1);
74 Dictionary::Ptr test2 = dictionary->Get("test2");
78 BOOST_AUTO_TEST_CASE(foreach)
80 Dictionary::Ptr dictionary = new Dictionary();
81 dictionary->Set("test1", 7);
82 dictionary->Set("test2", "hello world");
84 ObjectLock olock(dictionary);
86 bool seen_test1 = false, seen_test2 = false;
88 for (const Dictionary::Pair& kv : dictionary) {
89 BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
91 if (kv.first == "test1") {
92 BOOST_CHECK(!seen_test1);
95 BOOST_CHECK(kv.second == 7);
98 } else if (kv.first == "test2") {
99 BOOST_CHECK(!seen_test2);
102 BOOST_CHECK(kv.second == "hello world");
106 BOOST_CHECK(seen_test1);
107 BOOST_CHECK(seen_test2);
110 BOOST_AUTO_TEST_CASE(remove)
112 Dictionary::Ptr dictionary = new Dictionary();
114 dictionary->Set("test1", 7);
115 dictionary->Set("test2", "hello world");
117 BOOST_CHECK(dictionary->Contains("test1"));
118 BOOST_CHECK(dictionary->GetLength() == 2);
120 dictionary->Set("test1", Empty);
122 BOOST_CHECK(dictionary->Contains("test1"));
123 BOOST_CHECK(dictionary->GetLength() == 2);
125 dictionary->Remove("test1");
127 BOOST_CHECK(!dictionary->Contains("test1"));
128 BOOST_CHECK(dictionary->GetLength() == 1);
130 dictionary->Remove("test2");
132 BOOST_CHECK(!dictionary->Contains("test2"));
133 BOOST_CHECK(dictionary->GetLength() == 0);
135 dictionary->Set("test1", 7);
136 dictionary->Set("test2", "hello world");
139 ObjectLock olock(dictionary);
141 auto it = dictionary->Begin();
142 dictionary->Remove(it);
145 BOOST_CHECK(dictionary->GetLength() == 1);
148 BOOST_AUTO_TEST_CASE(clone)
150 Dictionary::Ptr dictionary = new Dictionary();
152 dictionary->Set("test1", 7);
153 dictionary->Set("test2", "hello world");
155 Dictionary::Ptr clone = dictionary->ShallowClone();
157 BOOST_CHECK(dictionary != clone);
159 BOOST_CHECK(clone->GetLength() == 2);
160 BOOST_CHECK(clone->Get("test1") == 7);
161 BOOST_CHECK(clone->Get("test2") == "hello world");
163 clone->Set("test3", 5);
164 BOOST_CHECK(!dictionary->Contains("test3"));
165 BOOST_CHECK(dictionary->GetLength() == 2);
167 clone->Set("test2", "test");
168 BOOST_CHECK(dictionary->Get("test2") == "hello world");
171 BOOST_AUTO_TEST_CASE(json)
173 Dictionary::Ptr dictionary = new Dictionary();
175 dictionary->Set("test1", 7);
176 dictionary->Set("test2", "hello world");
178 String json = JsonEncode(dictionary);
179 BOOST_CHECK(json.GetLength() > 0);
180 Dictionary::Ptr deserialized = JsonDecode(json);
181 BOOST_CHECK(deserialized->GetLength() == 2);
182 BOOST_CHECK(deserialized->Get("test1") == 7);
183 BOOST_CHECK(deserialized->Get("test2") == "hello world");
186 BOOST_AUTO_TEST_SUITE_END()