2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-04-18 16:40:09 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/array.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2016-09-07 08:20:51 +02:00
|
|
|
#include <BoostTestTargetConfig.h>
|
2013-04-18 16:40:09 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(base_array)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(construct)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr array = new Array();
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_CHECK(array);
|
|
|
|
BOOST_CHECK(array->GetLength() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(getset)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr array = new 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);
|
|
|
|
}
|
|
|
|
|
2014-11-23 09:50:11 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(resize)
|
|
|
|
{
|
|
|
|
Array::Ptr array = new Array();
|
|
|
|
array->Resize(2);
|
|
|
|
BOOST_CHECK(array->GetLength() == 2);
|
|
|
|
BOOST_CHECK(array->Get(0) == Empty);
|
|
|
|
BOOST_CHECK(array->Get(1) == Empty);
|
|
|
|
}
|
|
|
|
|
2013-12-12 13:53:27 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(insert)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr array = new Array();
|
2013-12-12 13:53:27 +01:00
|
|
|
|
|
|
|
array->Insert(0, 11);
|
|
|
|
array->Insert(1, 22);
|
|
|
|
BOOST_CHECK(array->GetLength() == 2);
|
|
|
|
BOOST_CHECK(array->Get(1) == 22);
|
|
|
|
|
|
|
|
array->Insert(0, 33);
|
|
|
|
BOOST_CHECK(array->GetLength() == 3);
|
|
|
|
BOOST_CHECK(array->Get(0) == 33);
|
|
|
|
BOOST_CHECK(array->Get(1) == 11);
|
|
|
|
|
|
|
|
array->Insert(1, 44);
|
|
|
|
BOOST_CHECK(array->GetLength() == 4);
|
|
|
|
BOOST_CHECK(array->Get(0) == 33);
|
|
|
|
BOOST_CHECK(array->Get(1) == 44);
|
|
|
|
BOOST_CHECK(array->Get(2) == 11);
|
|
|
|
}
|
|
|
|
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(remove)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr array = new Array();
|
2013-04-19 11:27:18 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(array);
|
2018-01-04 09:07:03 +01:00
|
|
|
auto it = array->Begin();
|
2013-04-19 11:27:18 +02:00
|
|
|
array->Remove(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(array->GetLength() == 2);
|
|
|
|
BOOST_CHECK(array->Get(0) == 2);
|
2014-11-23 09:50:11 +01:00
|
|
|
|
|
|
|
array->Clear();
|
|
|
|
BOOST_CHECK(array->GetLength() == 0);
|
2013-04-19 11:27:18 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 16:55:14 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(unique)
|
|
|
|
{
|
|
|
|
Array::Ptr array = new Array();
|
|
|
|
array->Add("group1");
|
|
|
|
array->Add("group2");
|
|
|
|
array->Add("group1");
|
|
|
|
array->Add("group2");
|
|
|
|
|
|
|
|
Array::Ptr result;
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(array);
|
|
|
|
result = array->Unique();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(result->GetLength() == 2);
|
|
|
|
result->Sort();
|
|
|
|
|
|
|
|
BOOST_CHECK(result->Get(0) == "group1");
|
|
|
|
BOOST_CHECK(result->Get(1) == "group2");
|
|
|
|
}
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(foreach)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr array = new Array();
|
2013-04-18 16:40:09 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
|
|
|
|
ObjectLock olock(array);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& item : array) {
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_CHECK(n != 0 || item == 7);
|
|
|
|
BOOST_CHECK(n != 1 || item == 2);
|
|
|
|
BOOST_CHECK(n != 2 || item == 5);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(clone)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr array = new 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
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr array = new Array();
|
2013-04-19 11:27:18 +02:00
|
|
|
array->Add(7);
|
|
|
|
array->Add(2);
|
|
|
|
array->Add(5);
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
String json = JsonEncode(array);
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_CHECK(json.GetLength() > 0);
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
Array::Ptr deserialized = JsonDecode(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()
|