1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/dictionary.hpp"
#include "base/objectlock.hpp"
#include "base/json.hpp"
#include "base/string.hpp"
#include "base/utility.hpp"
#include <BoostTestTargetConfig.h>
using namespace icinga;
BOOST_AUTO_TEST_SUITE(base_dictionary)
BOOST_AUTO_TEST_CASE(construct)
{
Dictionary::Ptr dictionary = new Dictionary();
BOOST_CHECK(dictionary);
}
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");
}
BOOST_AUTO_TEST_CASE(get1)
{
Dictionary::Ptr dictionary = new Dictionary();
dictionary->Set("test1", 7);
dictionary->Set("test2", "hello world");
BOOST_CHECK(dictionary->GetLength() == 2);
Value test1;
test1 = dictionary->Get("test1");
BOOST_CHECK(test1 == 7);
Value test2;
test2 = dictionary->Get("test2");
BOOST_CHECK(test2 == "hello world");
String key3 = "test3";
Value test3;
test3 = dictionary->Get(key3);
BOOST_CHECK(test3.IsEmpty());
}
BOOST_AUTO_TEST_CASE(get2)
{
Dictionary::Ptr dictionary = new Dictionary();
Dictionary::Ptr other = new Dictionary();
dictionary->Set("test1", other);
BOOST_CHECK(dictionary->GetLength() == 1);
Dictionary::Ptr test1 = dictionary->Get("test1");
BOOST_CHECK(other == test1);
Dictionary::Ptr test2 = dictionary->Get("test2");
BOOST_CHECK(!test2);
}
BOOST_AUTO_TEST_CASE(foreach)
{
Dictionary::Ptr dictionary = new Dictionary();
dictionary->Set("test1", 7);
dictionary->Set("test2", "hello world");
ObjectLock olock(dictionary);
bool seen_test1 = false, seen_test2 = false;
for (const Dictionary::Pair& kv : dictionary) {
BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
if (kv.first == "test1") {
BOOST_CHECK(!seen_test1);
seen_test1 = true;
BOOST_CHECK(kv.second == 7);
continue;
} else if (kv.first == "test2") {
BOOST_CHECK(!seen_test2);
seen_test2 = true;
BOOST_CHECK(kv.second == "hello world");
}
}
BOOST_CHECK(seen_test1);
BOOST_CHECK(seen_test2);
}
BOOST_AUTO_TEST_CASE(remove)
{
Dictionary::Ptr dictionary = new Dictionary();
dictionary->Set("test1", 7);
dictionary->Set("test2", "hello world");
BOOST_CHECK(dictionary->Contains("test1"));
BOOST_CHECK(dictionary->GetLength() == 2);
dictionary->Set("test1", Empty);
BOOST_CHECK(dictionary->Contains("test1"));
BOOST_CHECK(dictionary->GetLength() == 2);
dictionary->Remove("test1");
BOOST_CHECK(!dictionary->Contains("test1"));
BOOST_CHECK(dictionary->GetLength() == 1);
dictionary->Remove("test2");
BOOST_CHECK(!dictionary->Contains("test2"));
BOOST_CHECK(dictionary->GetLength() == 0);
dictionary->Set("test1", 7);
dictionary->Set("test2", "hello world");
{
ObjectLock olock(dictionary);
auto it = dictionary->Begin();
dictionary->Remove(it);
}
BOOST_CHECK(dictionary->GetLength() == 1);
}
BOOST_AUTO_TEST_CASE(clone)
{
Dictionary::Ptr dictionary = new Dictionary();
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");
}
BOOST_AUTO_TEST_CASE(json)
{
Dictionary::Ptr dictionary = new Dictionary();
dictionary->Set("test1", 7);
dictionary->Set("test2", "hello world");
String json = JsonEncode(dictionary);
BOOST_CHECK(json.GetLength() > 0);
Dictionary::Ptr deserialized = JsonDecode(json);
BOOST_CHECK(deserialized->GetLength() == 2);
BOOST_CHECK(deserialized->Get("test1") == 7);
BOOST_CHECK(deserialized->Get("test2") == "hello world");
}
BOOST_AUTO_TEST_CASE(keys_ordered)
{
Dictionary::Ptr dictionary = new Dictionary();
for (int i = 0; i < 100; i++) {
dictionary->Set(std::to_string(Utility::Random()), Utility::Random());
}
std::vector<String> keys = dictionary->GetKeys();
BOOST_CHECK(std::is_sorted(keys.begin(), keys.end()));
}
BOOST_AUTO_TEST_SUITE_END()
|