2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-02-08 06:49:23 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/dictionary.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-02-08 06:49:23 +01:00
|
|
|
|
2012-05-28 11:53:51 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2013-02-08 06:49:23 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE(base_dictionary)
|
|
|
|
|
2012-05-28 11:53:51 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(construct)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dictionary = new Dictionary();
|
2013-02-07 11:13:00 +01:00
|
|
|
BOOST_CHECK(dictionary);
|
2012-05-28 11:53:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 14:56:36 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(initializer1)
|
|
|
|
{
|
|
|
|
DictionaryData dict;
|
|
|
|
|
|
|
|
dict.emplace_back("test1", "Gin-o-clock");
|
|
|
|
|
|
|
|
Dictionary::Ptr dictionary = new Dictionary(std::move(dict));
|
|
|
|
|
|
|
|
Value test1;
|
|
|
|
test1 = dictionary->Get("test1");
|
|
|
|
BOOST_CHECK(test1 == "Gin-o-clock");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(initializer2)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr dictionary = new Dictionary({ {"test1", "Gin-for-the-win"} });
|
|
|
|
|
|
|
|
Value test1;
|
|
|
|
test1 = dictionary->Get("test1");
|
|
|
|
BOOST_CHECK(test1 == "Gin-for-the-win");
|
|
|
|
}
|
|
|
|
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(get1)
|
2012-05-28 11:53:51 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dictionary = new Dictionary();
|
2012-07-09 16:19:56 +02:00
|
|
|
dictionary->Set("test1", 7);
|
|
|
|
dictionary->Set("test2", "hello world");
|
2012-05-28 11:53:51 +02:00
|
|
|
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_CHECK(dictionary->GetLength() == 2);
|
|
|
|
|
2012-09-14 11:41:15 +02:00
|
|
|
Value test1;
|
|
|
|
test1 = dictionary->Get("test1");
|
2013-02-07 11:13:00 +01:00
|
|
|
BOOST_CHECK(test1 == 7);
|
2012-05-28 11:53:51 +02:00
|
|
|
|
2012-09-14 11:41:15 +02:00
|
|
|
Value test2;
|
|
|
|
test2 = dictionary->Get("test2");
|
2013-02-07 11:13:00 +01:00
|
|
|
BOOST_CHECK(test2 == "hello world");
|
2012-05-29 06:25:01 +02:00
|
|
|
|
2013-04-18 16:40:09 +02:00
|
|
|
String key3 = "test3";
|
2012-09-14 11:41:15 +02:00
|
|
|
Value test3;
|
2013-04-18 16:40:09 +02:00
|
|
|
test3 = dictionary->Get(key3);
|
2013-02-07 11:13:00 +01:00
|
|
|
BOOST_CHECK(test3.IsEmpty());
|
2012-05-29 06:25:01 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(get2)
|
2012-05-29 06:25:01 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dictionary = new Dictionary();
|
|
|
|
Dictionary::Ptr other = new Dictionary();
|
2012-05-29 06:25:01 +02:00
|
|
|
|
2012-07-09 16:19:56 +02:00
|
|
|
dictionary->Set("test1", other);
|
2012-05-29 06:25:01 +02:00
|
|
|
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_CHECK(dictionary->GetLength() == 1);
|
|
|
|
|
2012-09-14 11:41:15 +02:00
|
|
|
Dictionary::Ptr test1 = dictionary->Get("test1");
|
2013-02-07 11:13:00 +01:00
|
|
|
BOOST_CHECK(other == test1);
|
2012-05-29 06:25:01 +02:00
|
|
|
|
2012-09-14 11:41:15 +02:00
|
|
|
Dictionary::Ptr test2 = dictionary->Get("test2");
|
2013-02-07 11:13:00 +01:00
|
|
|
BOOST_CHECK(!test2);
|
2012-05-29 06:25:01 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(foreach)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dictionary = new Dictionary();
|
2013-04-19 11:27:18 +02:00
|
|
|
dictionary->Set("test1", 7);
|
|
|
|
dictionary->Set("test2", "hello world");
|
|
|
|
|
|
|
|
ObjectLock olock(dictionary);
|
|
|
|
|
|
|
|
bool seen_test1 = false, seen_test2 = false;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : dictionary) {
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
|
2013-04-19 11:27:18 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
if (kv.first == "test1") {
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_CHECK(!seen_test1);
|
|
|
|
seen_test1 = true;
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_CHECK(kv.second == 7);
|
2013-04-19 11:27:18 +02:00
|
|
|
|
|
|
|
continue;
|
2013-11-30 17:42:50 +01:00
|
|
|
} else if (kv.first == "test2") {
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_CHECK(!seen_test2);
|
|
|
|
seen_test2 = true;
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_CHECK(kv.second == "hello world");
|
2013-04-19 11:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(seen_test1);
|
|
|
|
BOOST_CHECK(seen_test2);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(remove)
|
2013-04-18 16:40:09 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dictionary = new Dictionary();
|
2013-04-18 16:40:09 +02:00
|
|
|
|
|
|
|
dictionary->Set("test1", 7);
|
|
|
|
dictionary->Set("test2", "hello world");
|
|
|
|
|
|
|
|
BOOST_CHECK(dictionary->Contains("test1"));
|
|
|
|
BOOST_CHECK(dictionary->GetLength() == 2);
|
|
|
|
|
|
|
|
dictionary->Set("test1", Empty);
|
|
|
|
|
2014-04-09 11:40:17 +02:00
|
|
|
BOOST_CHECK(dictionary->Contains("test1"));
|
|
|
|
BOOST_CHECK(dictionary->GetLength() == 2);
|
|
|
|
|
|
|
|
dictionary->Remove("test1");
|
|
|
|
|
2013-04-18 16:40:09 +02:00
|
|
|
BOOST_CHECK(!dictionary->Contains("test1"));
|
|
|
|
BOOST_CHECK(dictionary->GetLength() == 1);
|
|
|
|
|
|
|
|
dictionary->Remove("test2");
|
|
|
|
|
|
|
|
BOOST_CHECK(!dictionary->Contains("test2"));
|
|
|
|
BOOST_CHECK(dictionary->GetLength() == 0);
|
2013-04-19 11:27:18 +02:00
|
|
|
|
|
|
|
dictionary->Set("test1", 7);
|
|
|
|
dictionary->Set("test2", "hello world");
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(dictionary);
|
|
|
|
|
2018-01-04 09:07:03 +01:00
|
|
|
auto it = dictionary->Begin();
|
2013-04-19 11:27:18 +02:00
|
|
|
dictionary->Remove(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(dictionary->GetLength() == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(clone)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dictionary = new Dictionary();
|
2013-04-19 11:27:18 +02:00
|
|
|
|
|
|
|
dictionary->Set("test1", 7);
|
|
|
|
dictionary->Set("test2", "hello world");
|
|
|
|
|
|
|
|
Dictionary::Ptr clone = dictionary->ShallowClone();
|
|
|
|
|
|
|
|
BOOST_CHECK(dictionary != clone);
|
|
|
|
|
|
|
|
BOOST_CHECK(clone->GetLength() == 2);
|
|
|
|
BOOST_CHECK(clone->Get("test1") == 7);
|
|
|
|
BOOST_CHECK(clone->Get("test2") == "hello world");
|
|
|
|
|
|
|
|
clone->Set("test3", 5);
|
|
|
|
BOOST_CHECK(!dictionary->Contains("test3"));
|
|
|
|
BOOST_CHECK(dictionary->GetLength() == 2);
|
|
|
|
|
|
|
|
clone->Set("test2", "test");
|
|
|
|
BOOST_CHECK(dictionary->Get("test2") == "hello world");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
Dictionary::Ptr dictionary = new Dictionary();
|
2013-04-19 11:27:18 +02:00
|
|
|
|
|
|
|
dictionary->Set("test1", 7);
|
|
|
|
dictionary->Set("test2", "hello world");
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
String json = JsonEncode(dictionary);
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_CHECK(json.GetLength() > 0);
|
2014-10-26 19:59:49 +01:00
|
|
|
Dictionary::Ptr deserialized = JsonDecode(json);
|
2013-04-19 11:27:18 +02:00
|
|
|
BOOST_CHECK(deserialized->GetLength() == 2);
|
|
|
|
BOOST_CHECK(deserialized->Get("test1") == 7);
|
|
|
|
BOOST_CHECK(deserialized->Get("test2") == "hello world");
|
2013-04-18 16:40:09 +02:00
|
|
|
}
|
|
|
|
|
2013-02-08 06:49:23 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|