]> granicus.if.org Git - icinga2/blob - test/base-array.cpp
Merge pull request #6294 from Icinga/feature/unique-groups-api
[icinga2] / test / base-array.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/array.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_array)
28
29 BOOST_AUTO_TEST_CASE(construct)
30 {
31         Array::Ptr array = new Array();
32         BOOST_CHECK(array);
33         BOOST_CHECK(array->GetLength() == 0);
34 }
35
36 BOOST_AUTO_TEST_CASE(getset)
37 {
38         Array::Ptr array = new Array();
39         array->Add(7);
40         array->Add(2);
41         array->Add(5);
42         BOOST_CHECK(array->GetLength() == 3);
43         BOOST_CHECK(array->Get(0) == 7);
44         BOOST_CHECK(array->Get(1) == 2);
45         BOOST_CHECK(array->Get(2) == 5);
46
47         array->Set(1, 9);
48         BOOST_CHECK(array->Get(1) == 9);
49
50         array->Remove(1);
51         BOOST_CHECK(array->GetLength() == 2);
52         BOOST_CHECK(array->Get(1) == 5);
53 }
54
55 BOOST_AUTO_TEST_CASE(resize)
56 {
57         Array::Ptr array = new Array();
58         array->Resize(2);
59         BOOST_CHECK(array->GetLength() == 2);
60         BOOST_CHECK(array->Get(0) == Empty);
61         BOOST_CHECK(array->Get(1) == Empty);
62 }
63
64 BOOST_AUTO_TEST_CASE(insert)
65 {
66         Array::Ptr array = new Array();
67
68         array->Insert(0, 11);
69         array->Insert(1, 22);
70         BOOST_CHECK(array->GetLength() == 2);
71         BOOST_CHECK(array->Get(1) == 22);
72
73         array->Insert(0, 33);
74         BOOST_CHECK(array->GetLength() == 3);
75         BOOST_CHECK(array->Get(0) == 33);
76         BOOST_CHECK(array->Get(1) == 11);
77
78         array->Insert(1, 44);
79         BOOST_CHECK(array->GetLength() == 4);
80         BOOST_CHECK(array->Get(0) == 33);
81         BOOST_CHECK(array->Get(1) == 44);
82         BOOST_CHECK(array->Get(2) == 11);
83 }
84
85 BOOST_AUTO_TEST_CASE(remove)
86 {
87         Array::Ptr array = new Array();
88         array->Add(7);
89         array->Add(2);
90         array->Add(5);
91
92         {
93                 ObjectLock olock(array);
94                 auto it = array->Begin();
95                 array->Remove(it);
96         }
97
98         BOOST_CHECK(array->GetLength() == 2);
99         BOOST_CHECK(array->Get(0) == 2);
100
101         array->Clear();
102         BOOST_CHECK(array->GetLength() == 0);
103 }
104
105 BOOST_AUTO_TEST_CASE(unique)
106 {
107         Array::Ptr array = new Array();
108         array->Add("group1");
109         array->Add("group2");
110         array->Add("group1");
111         array->Add("group2");
112
113         Array::Ptr result;
114
115         {
116                 ObjectLock olock(array);
117                 result = array->Unique();
118         }
119
120         BOOST_CHECK(result->GetLength() == 2);
121         result->Sort();
122
123         BOOST_CHECK(result->Get(0) == "group1");
124         BOOST_CHECK(result->Get(1) == "group2");
125 }
126 BOOST_AUTO_TEST_CASE(foreach)
127 {
128         Array::Ptr array = new Array();
129         array->Add(7);
130         array->Add(2);
131         array->Add(5);
132
133         ObjectLock olock(array);
134
135         int n = 0;
136
137         for (const Value& item : array) {
138                 BOOST_CHECK(n != 0 || item == 7);
139                 BOOST_CHECK(n != 1 || item == 2);
140                 BOOST_CHECK(n != 2 || item == 5);
141
142                 n++;
143         }
144 }
145
146 BOOST_AUTO_TEST_CASE(clone)
147 {
148         Array::Ptr array = new Array();
149         array->Add(7);
150         array->Add(2);
151         array->Add(5);
152
153         Array::Ptr clone = array->ShallowClone();
154
155         BOOST_CHECK(clone->GetLength() == 3);
156         BOOST_CHECK(clone->Get(0) == 7);
157         BOOST_CHECK(clone->Get(1) == 2);
158         BOOST_CHECK(clone->Get(2) == 5);
159 }
160
161 BOOST_AUTO_TEST_CASE(json)
162 {
163         Array::Ptr array = new Array();
164         array->Add(7);
165         array->Add(2);
166         array->Add(5);
167
168         String json = JsonEncode(array);
169         BOOST_CHECK(json.GetLength() > 0);
170
171         Array::Ptr deserialized = JsonDecode(json);
172         BOOST_CHECK(deserialized);
173         BOOST_CHECK(deserialized->GetLength() == 3);
174         BOOST_CHECK(deserialized->Get(0) == 7);
175         BOOST_CHECK(deserialized->Get(1) == 2);
176         BOOST_CHECK(deserialized->Get(2) == 5);
177 }
178
179 BOOST_AUTO_TEST_SUITE_END()