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