]> granicus.if.org Git - icinga2/blob - test/base-dictionary.cpp
Merge pull request #6999 from Icinga/bugfix/compiler-warnings
[icinga2] / test / base-dictionary.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/dictionary.hpp"
4 #include "base/objectlock.hpp"
5 #include "base/json.hpp"
6 #include <BoostTestTargetConfig.h>
7
8 using namespace icinga;
9
10 BOOST_AUTO_TEST_SUITE(base_dictionary)
11
12 BOOST_AUTO_TEST_CASE(construct)
13 {
14         Dictionary::Ptr dictionary = new Dictionary();
15         BOOST_CHECK(dictionary);
16 }
17
18 BOOST_AUTO_TEST_CASE(initializer1)
19 {
20         DictionaryData dict;
21
22         dict.emplace_back("test1", "Gin-o-clock");
23
24         Dictionary::Ptr dictionary = new Dictionary(std::move(dict));
25
26         Value test1;
27         test1 = dictionary->Get("test1");
28         BOOST_CHECK(test1 == "Gin-o-clock");
29 }
30
31 BOOST_AUTO_TEST_CASE(initializer2)
32 {
33         Dictionary::Ptr dictionary = new Dictionary({ {"test1", "Gin-for-the-win"} });
34
35         Value test1;
36         test1 = dictionary->Get("test1");
37         BOOST_CHECK(test1 == "Gin-for-the-win");
38 }
39
40 BOOST_AUTO_TEST_CASE(get1)
41 {
42         Dictionary::Ptr dictionary = new Dictionary();
43         dictionary->Set("test1", 7);
44         dictionary->Set("test2", "hello world");
45
46         BOOST_CHECK(dictionary->GetLength() == 2);
47
48         Value test1;
49         test1 = dictionary->Get("test1");
50         BOOST_CHECK(test1 == 7);
51
52         Value test2;
53         test2 = dictionary->Get("test2");
54         BOOST_CHECK(test2 == "hello world");
55
56         String key3 = "test3";
57         Value test3;
58         test3 = dictionary->Get(key3);
59         BOOST_CHECK(test3.IsEmpty());
60 }
61
62 BOOST_AUTO_TEST_CASE(get2)
63 {
64         Dictionary::Ptr dictionary = new Dictionary();
65         Dictionary::Ptr other = new Dictionary();
66
67         dictionary->Set("test1", other);
68
69         BOOST_CHECK(dictionary->GetLength() == 1);
70
71         Dictionary::Ptr test1 = dictionary->Get("test1");
72         BOOST_CHECK(other == test1);
73
74         Dictionary::Ptr test2 = dictionary->Get("test2");
75         BOOST_CHECK(!test2);
76 }
77
78 BOOST_AUTO_TEST_CASE(foreach)
79 {
80         Dictionary::Ptr dictionary = new Dictionary();
81         dictionary->Set("test1", 7);
82         dictionary->Set("test2", "hello world");
83
84         ObjectLock olock(dictionary);
85
86         bool seen_test1 = false, seen_test2 = false;
87
88         for (const Dictionary::Pair& kv : dictionary) {
89                 BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
90
91                 if (kv.first == "test1") {
92                         BOOST_CHECK(!seen_test1);
93                         seen_test1 = true;
94
95                         BOOST_CHECK(kv.second == 7);
96
97                         continue;
98                 } else if (kv.first == "test2") {
99                         BOOST_CHECK(!seen_test2);
100                         seen_test2 = true;
101
102                         BOOST_CHECK(kv.second == "hello world");
103                 }
104         }
105
106         BOOST_CHECK(seen_test1);
107         BOOST_CHECK(seen_test2);
108 }
109
110 BOOST_AUTO_TEST_CASE(remove)
111 {
112         Dictionary::Ptr dictionary = new Dictionary();
113
114         dictionary->Set("test1", 7);
115         dictionary->Set("test2", "hello world");
116
117         BOOST_CHECK(dictionary->Contains("test1"));
118         BOOST_CHECK(dictionary->GetLength() == 2);
119
120         dictionary->Set("test1", Empty);
121
122         BOOST_CHECK(dictionary->Contains("test1"));
123         BOOST_CHECK(dictionary->GetLength() == 2);
124
125         dictionary->Remove("test1");
126
127         BOOST_CHECK(!dictionary->Contains("test1"));
128         BOOST_CHECK(dictionary->GetLength() == 1);
129
130         dictionary->Remove("test2");
131
132         BOOST_CHECK(!dictionary->Contains("test2"));
133         BOOST_CHECK(dictionary->GetLength() == 0);
134
135         dictionary->Set("test1", 7);
136         dictionary->Set("test2", "hello world");
137
138         {
139                 ObjectLock olock(dictionary);
140
141                 auto it = dictionary->Begin();
142                 dictionary->Remove(it);
143         }
144
145         BOOST_CHECK(dictionary->GetLength() == 1);
146 }
147
148 BOOST_AUTO_TEST_CASE(clone)
149 {
150         Dictionary::Ptr dictionary = new Dictionary();
151
152         dictionary->Set("test1", 7);
153         dictionary->Set("test2", "hello world");
154
155         Dictionary::Ptr clone = dictionary->ShallowClone();
156
157         BOOST_CHECK(dictionary != clone);
158
159         BOOST_CHECK(clone->GetLength() == 2);
160         BOOST_CHECK(clone->Get("test1") == 7);
161         BOOST_CHECK(clone->Get("test2") == "hello world");
162
163         clone->Set("test3", 5);
164         BOOST_CHECK(!dictionary->Contains("test3"));
165         BOOST_CHECK(dictionary->GetLength() == 2);
166
167         clone->Set("test2", "test");
168         BOOST_CHECK(dictionary->Get("test2") == "hello world");
169 }
170
171 BOOST_AUTO_TEST_CASE(json)
172 {
173         Dictionary::Ptr dictionary = new Dictionary();
174
175         dictionary->Set("test1", 7);
176         dictionary->Set("test2", "hello world");
177
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");
184 }
185
186 BOOST_AUTO_TEST_SUITE_END()