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