1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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 <boost/test/unit_test.hpp>
24 #include <boost/foreach.hpp>
26 using namespace icinga;
28 BOOST_AUTO_TEST_SUITE(base_array)
30 BOOST_AUTO_TEST_CASE(construct)
32 Array::Ptr array = make_shared<Array>();
34 BOOST_CHECK(array->GetLength() == 0);
37 BOOST_AUTO_TEST_CASE(getset)
39 Array::Ptr array = make_shared<Array>();
43 BOOST_CHECK(array->GetLength() == 3);
44 BOOST_CHECK(array->Get(0) == 7);
45 BOOST_CHECK(array->Get(1) == 2);
46 BOOST_CHECK(array->Get(2) == 5);
49 BOOST_CHECK(array->Get(1) == 9);
52 BOOST_CHECK(array->GetLength() == 2);
53 BOOST_CHECK(array->Get(1) == 5);
56 BOOST_AUTO_TEST_CASE(insert)
58 Array::Ptr array = make_shared<Array>();
62 BOOST_CHECK(array->GetLength() == 2);
63 BOOST_CHECK(array->Get(1) == 22);
66 BOOST_CHECK(array->GetLength() == 3);
67 BOOST_CHECK(array->Get(0) == 33);
68 BOOST_CHECK(array->Get(1) == 11);
71 BOOST_CHECK(array->GetLength() == 4);
72 BOOST_CHECK(array->Get(0) == 33);
73 BOOST_CHECK(array->Get(1) == 44);
74 BOOST_CHECK(array->Get(2) == 11);
77 BOOST_AUTO_TEST_CASE(remove)
79 Array::Ptr array = make_shared<Array>();
85 ObjectLock olock(array);
86 Array::Iterator it = array->Begin();
90 BOOST_CHECK(array->GetLength() == 2);
91 BOOST_CHECK(array->Get(0) == 2);
94 BOOST_AUTO_TEST_CASE(foreach)
96 Array::Ptr array = make_shared<Array>();
101 ObjectLock olock(array);
105 BOOST_FOREACH(const Value& item, array) {
106 BOOST_CHECK(n != 0 || item == 7);
107 BOOST_CHECK(n != 1 || item == 2);
108 BOOST_CHECK(n != 2 || item == 5);
114 BOOST_AUTO_TEST_CASE(clone)
116 Array::Ptr array = make_shared<Array>();
121 Array::Ptr clone = array->ShallowClone();
123 BOOST_CHECK(clone->GetLength() == 3);
124 BOOST_CHECK(clone->Get(0) == 7);
125 BOOST_CHECK(clone->Get(1) == 2);
126 BOOST_CHECK(clone->Get(2) == 5);
129 BOOST_AUTO_TEST_CASE(json)
131 Array::Ptr array = make_shared<Array>();
136 String json = JsonEncode(array);
137 BOOST_CHECK(json.GetLength() > 0);
139 Array::Ptr deserialized = JsonDecode(json);
140 BOOST_CHECK(deserialized);
141 BOOST_CHECK(deserialized->GetLength() == 3);
142 BOOST_CHECK(deserialized->Get(0) == 7);
143 BOOST_CHECK(deserialized->Get(1) == 2);
144 BOOST_CHECK(deserialized->Get(2) == 5);
147 BOOST_AUTO_TEST_SUITE_END()