1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
20 #include "base/array.hpp"
21 #include "base/objectlock.hpp"
22 #include "base/json.hpp"
23 #include <BoostTestTargetConfig.h>
25 using namespace icinga;
27 BOOST_AUTO_TEST_SUITE(base_array)
29 BOOST_AUTO_TEST_CASE(construct)
31 Array::Ptr array = new Array();
33 BOOST_CHECK(array->GetLength() == 0);
36 BOOST_AUTO_TEST_CASE(getset)
38 Array::Ptr array = new Array();
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);
48 BOOST_CHECK(array->Get(1) == 9);
51 BOOST_CHECK(array->GetLength() == 2);
52 BOOST_CHECK(array->Get(1) == 5);
55 BOOST_AUTO_TEST_CASE(resize)
57 Array::Ptr array = new Array();
59 BOOST_CHECK(array->GetLength() == 2);
60 BOOST_CHECK(array->Get(0) == Empty);
61 BOOST_CHECK(array->Get(1) == Empty);
64 BOOST_AUTO_TEST_CASE(insert)
66 Array::Ptr array = new Array();
70 BOOST_CHECK(array->GetLength() == 2);
71 BOOST_CHECK(array->Get(1) == 22);
74 BOOST_CHECK(array->GetLength() == 3);
75 BOOST_CHECK(array->Get(0) == 33);
76 BOOST_CHECK(array->Get(1) == 11);
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);
85 BOOST_AUTO_TEST_CASE(remove)
87 Array::Ptr array = new Array();
93 ObjectLock olock(array);
94 Array::Iterator it = array->Begin();
98 BOOST_CHECK(array->GetLength() == 2);
99 BOOST_CHECK(array->Get(0) == 2);
102 BOOST_CHECK(array->GetLength() == 0);
105 BOOST_AUTO_TEST_CASE(foreach)
107 Array::Ptr array = new Array();
112 ObjectLock olock(array);
116 for (const Value& item : array) {
117 BOOST_CHECK(n != 0 || item == 7);
118 BOOST_CHECK(n != 1 || item == 2);
119 BOOST_CHECK(n != 2 || item == 5);
125 BOOST_AUTO_TEST_CASE(clone)
127 Array::Ptr array = new Array();
132 Array::Ptr clone = array->ShallowClone();
134 BOOST_CHECK(clone->GetLength() == 3);
135 BOOST_CHECK(clone->Get(0) == 7);
136 BOOST_CHECK(clone->Get(1) == 2);
137 BOOST_CHECK(clone->Get(2) == 5);
140 BOOST_AUTO_TEST_CASE(json)
142 Array::Ptr array = new Array();
147 String json = JsonEncode(array);
148 BOOST_CHECK(json.GetLength() > 0);
150 Array::Ptr deserialized = JsonDecode(json);
151 BOOST_CHECK(deserialized);
152 BOOST_CHECK(deserialized->GetLength() == 3);
153 BOOST_CHECK(deserialized->Get(0) == 7);
154 BOOST_CHECK(deserialized->Get(1) == 2);
155 BOOST_CHECK(deserialized->Get(2) == 5);
158 BOOST_AUTO_TEST_SUITE_END()