2013-04-18 16:40:09 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2013-04-18 16:40:09 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "base/array.h"
|
|
|
|
#include "base/objectlock.h"
|
2013-11-08 11:17:46 +01:00
|
|
|
#include "base/serializer.h"
|
2013-04-18 16:40:09 +02:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(base_array)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(construct)
|
|
|
|
{
|
2013-11-06 08:51:56 +01:00
|
|
|
Array::Ptr array = make_shared<Array>();
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_CHECK(array);
|
|
|
|
BOOST_CHECK(array->GetLength() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(getset)
|
|
|
|
{
|
2013-11-06 08:51:56 +01:00
|
|
|
Array::Ptr array = make_shared<Array>();
|
2013-04-18 16:40:09 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
BOOST_CHECK(array->GetLength() == 3);
|
|
|
|
BOOST_CHECK(array->Get(0) == 7);
|
|
|
|
BOOST_CHECK(array->Get(1) == 2);
|
|
|
|
BOOST_CHECK(array->Get(2) == 5);
|
|
|
|
|
|
|
|
array->Set(1, 9);
|
|
|
|
BOOST_CHECK(array->Get(1) == 9);
|
|
|
|
|
|
|
|
array->Remove(1);
|
|
|
|
BOOST_CHECK(array->GetLength() == 2);
|
|
|
|
BOOST_CHECK(array->Get(1) == 5);
|
|
|
|
}
|
|
|
|
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(remove)
|
|
|
|
{
|
2013-11-06 08:51:56 +01:00
|
|
|
Array::Ptr array = make_shared<Array>();
|
2013-04-19 11:27:18 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(array);
|
|
|
|
Array::Iterator it = array->Begin();
|
|
|
|
array->Remove(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(array->GetLength() == 2);
|
|
|
|
BOOST_CHECK(array->Get(0) == 2);
|
|
|
|
}
|
|
|
|
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(foreach)
|
|
|
|
{
|
2013-11-06 08:51:56 +01:00
|
|
|
Array::Ptr array = make_shared<Array>();
|
2013-04-18 16:40:09 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
|
|
|
|
ObjectLock olock(array);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Value& item, array) {
|
|
|
|
BOOST_CHECK(n != 0 || item == 7);
|
|
|
|
BOOST_CHECK(n != 1 || item == 2);
|
|
|
|
BOOST_CHECK(n != 2 || item == 5);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(clone)
|
|
|
|
{
|
2013-11-06 08:51:56 +01:00
|
|
|
Array::Ptr array = make_shared<Array>();
|
2013-04-18 16:40:09 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
|
|
|
|
Array::Ptr clone = array->ShallowClone();
|
|
|
|
|
|
|
|
BOOST_CHECK(clone->GetLength() == 3);
|
|
|
|
BOOST_CHECK(clone->Get(0) == 7);
|
|
|
|
BOOST_CHECK(clone->Get(1) == 2);
|
|
|
|
BOOST_CHECK(clone->Get(2) == 5);
|
|
|
|
}
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(json)
|
2013-04-19 11:27:18 +02:00
|
|
|
{
|
2013-11-06 08:51:56 +01:00
|
|
|
Array::Ptr array = make_shared<Array>();
|
2013-04-19 11:27:18 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
String json = JsonSerialize(array);
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_CHECK(json.GetLength() > 0);
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
Array::Ptr deserialized = JsonDeserialize(json);
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_CHECK(deserialized);
|
|
|
|
BOOST_CHECK(deserialized->GetLength() == 3);
|
|
|
|
BOOST_CHECK(deserialized->Get(0) == 7);
|
|
|
|
BOOST_CHECK(deserialized->Get(1) == 2);
|
|
|
|
BOOST_CHECK(deserialized->Get(2) == 5);
|
|
|
|
}
|
|
|
|
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|