$(SED) -e 's|^#undef GIT_IS_DIST.*|#define GIT_IS_DIST 1|' \
"icinga-version.h" > "$(distdir)/icinga-version.h"; \
fi
+
+check-coverage:
+ lcov -d . -z
+ make check
+ lcov -d . -c -o icinga2.info
+ lcov -e icinga2.info -o icinga2-filtered.info `pwd`\*
+ mkdir -p docs/lcov
+ genhtml -o docs/lcov icinga2-filtered.info
+
AC_MSG_CHECKING(whether to enable coverage)
AC_ARG_ENABLE(coverage, [ --enable-coverage=[no/yes] turn on profiling (default=no)],, enable_coverage=no)
if test "x$enable_coverage" = "xyes"; then
- CFLAGS="$CFLAGS -g -O0 -fprofile-arcs -ftest-coverage -lgcov"
- CXXFLAGS="$CXXFLAGS -g -O0 -fprofile-arcs -ftest-coverage -lgcov"
+ CFLAGS="$CFLAGS -g -O0 -DNDEBUG -fprofile-arcs -ftest-coverage -lgcov"
+ CXXFLAGS="$CXXFLAGS -g -O0 -DNDEBUG -fprofile-arcs -ftest-coverage -lgcov"
fi
AC_MSG_RESULT($enable_coverage)
ASSERT(!OwnsLock());
ObjectLock olock(this);
- ASSERT(!m_Sealed);
+ if (m_Sealed)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be sealed."));
+
m_Data.at(index) = value;
}
ASSERT(!OwnsLock());
ObjectLock olock(this);
- ASSERT(!m_Sealed);
+ if (m_Sealed)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be sealed."));
+
m_Data.push_back(value);
}
ASSERT(!OwnsLock());
ObjectLock olock(this);
- ASSERT(!m_Sealed);
+ if (m_Sealed)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be sealed."));
+
m_Data.erase(m_Data.begin() + index);
}
*/
void Array::Remove(Array::Iterator it)
{
- ASSERT(!OwnsLock());
- ObjectLock olock(this);
+ ASSERT(OwnsLock());
+
+ if (m_Sealed)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be sealed."));
- ASSERT(!m_Sealed);
m_Data.erase(it);
}
{
Array::Ptr array = boost::make_shared<Array>();
- if (json->type != cJSON_Array)
- BOOST_THROW_EXCEPTION(std::invalid_argument("JSON type must be cJSON_Array."));
+ ASSERT(json->type == cJSON_Array);
for (cJSON *i = json->child; i != NULL; i = i->next) {
array->Add(Value::FromJson(i));
return (ToLong(val) != 0);
}
-String Convert::ToString(long val)
-{
- std::ostringstream cs;
- cs << val;
- return cs.str();
-}
-
String Convert::ToString(const Value& val)
{
return static_cast<String>(val);
static long ToLong(const String& val);
static double ToDouble(const String& val);
static bool ToBool(const String& val);
- static String ToString(long val);
static String ToString(const Value& val);
private:
ASSERT(!OwnsLock());
ObjectLock olock(this);
- ASSERT(!m_Sealed);
+ if (m_Sealed)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be sealed."));
std::pair<std::map<String, Value>::iterator, bool> ret;
ret = m_Data.insert(std::make_pair(key, value));
if (it == m_Data.end())
return;
- ASSERT(!m_Sealed);
+ if (m_Sealed)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be sealed."));
+
m_Data.erase(it);
}
*/
void Dictionary::Remove(Dictionary::Iterator it)
{
- ASSERT(!OwnsLock());
- ObjectLock olock(this);
+ ASSERT(OwnsLock());
+
+ if (m_Sealed)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be sealed."));
- ASSERT(!m_Sealed);
m_Data.erase(it);
}
{ }
Value::Value(int value)
- : m_Value(value)
+ : m_Value(double(value))
+{ }
+
+Value::Value(unsigned int value)
+ : m_Value(double(value))
{ }
Value::Value(long value)
: m_Value(double(value))
{ }
+Value::Value(unsigned long value)
+ : m_Value(double(value))
+{ }
+
Value::Value(double value)
: m_Value(value)
{ }
public:
Value(void);
Value(int value);
+ Value(unsigned int value);
Value(long value);
+ Value(unsigned long value);
Value(double value);
Value(const String& value);
Value(const char *value);
BOOST_CHECK(array->Get(1) == 5);
}
+BOOST_AUTO_TEST_CASE(remove)
+{
+ Array::Ptr array = boost::make_shared<Array>();
+ 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);
+}
+
BOOST_AUTO_TEST_CASE(foreach)
{
Array::Ptr array = boost::make_shared<Array>();
BOOST_CHECK(clone->Get(2) == 5);
}
+BOOST_AUTO_TEST_CASE(seal)
+{
+ Array::Ptr array = boost::make_shared<Array>();
+ array->Add(7);
+
+ BOOST_CHECK(!array->IsSealed());
+ array->Seal();
+ BOOST_CHECK(array->IsSealed());
+
+ BOOST_CHECK_THROW(array->Add(2), boost::exception);
+ BOOST_CHECK(array->GetLength() == 1);
+
+ BOOST_CHECK_THROW(array->Set(0, 8), boost::exception);
+ BOOST_CHECK(array->Get(0) == 7);
+
+ BOOST_CHECK_THROW(array->Remove(0), boost::exception);
+ BOOST_CHECK(array->GetLength() == 1);
+ BOOST_CHECK(array->Get(0) == 7);
+
+ {
+ ObjectLock olock(array);
+ Array::Iterator it = array->Begin();
+ BOOST_CHECK_THROW(array->Remove(it), boost::exception);
+ }
+
+ BOOST_CHECK(array->GetLength() == 1);
+ BOOST_CHECK(array->Get(0) == 7);
+}
+
+BOOST_AUTO_TEST_CASE(serialize)
+{
+ Array::Ptr array = boost::make_shared<Array>();
+ array->Add(7);
+ array->Add(2);
+ array->Add(5);
+
+ String json = Value(array).Serialize();
+ BOOST_CHECK(json.GetLength() > 0);
+
+ Array::Ptr deserialized = Value::Deserialize(json);
+ 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);
+}
+
BOOST_AUTO_TEST_SUITE_END()
******************************************************************************/
#include "base/convert.h"
+#include "base/object.h"
#include <boost/test/unit_test.hpp>
#include <boost/smart_ptr/make_shared.hpp>
+#include <iostream>
using namespace icinga;
BOOST_AUTO_TEST_CASE(tolong)
{
BOOST_CHECK_THROW(Convert::ToLong(" 7"), boost::exception);
- BOOST_CHECK(Convert::ToLong("7") == 7);
+ BOOST_CHECK(Convert::ToLong("-7") == -7);
BOOST_CHECK_THROW(Convert::ToLong("7a"), boost::exception);
}
+BOOST_AUTO_TEST_CASE(todouble)
+{
+ BOOST_CHECK_THROW(Convert::ToDouble(" 7.3"), boost::exception);
+ BOOST_CHECK(Convert::ToDouble("-7.3") == -7.3);
+ BOOST_CHECK_THROW(Convert::ToDouble("7.3a"), boost::exception);
+}
+
+BOOST_AUTO_TEST_CASE(tostring)
+{
+ BOOST_CHECK(Convert::ToString(7) == "7");
+ BOOST_CHECK(Convert::ToString(7.3) == "7.3");
+ BOOST_CHECK(Convert::ToString("hello") == "hello");
+
+ Object::Ptr object = boost::make_shared<Object>();
+ BOOST_CHECK(Convert::ToString(object) == "Object of type 'icinga::Object'");
+}
+
+BOOST_AUTO_TEST_CASE(tobool)
+{
+ BOOST_CHECK_THROW(Convert::ToBool("a"), boost::exception);
+ BOOST_CHECK(Convert::ToBool("0") == false);
+ BOOST_CHECK(Convert::ToBool("1") == true);
+ BOOST_CHECK(Convert::ToBool("2") == true);
+}
+
BOOST_AUTO_TEST_SUITE_END()
******************************************************************************/
#include "base/dictionary.h"
+#include "base/objectlock.h"
#include <boost/test/unit_test.hpp>
#include <boost/smart_ptr/make_shared.hpp>
+#include <boost/foreach.hpp>
+#include <boost/tuple/tuple.hpp>
using namespace icinga;
BOOST_CHECK(dictionary);
}
-BOOST_AUTO_TEST_CASE(getproperty)
+BOOST_AUTO_TEST_CASE(get1)
{
Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
dictionary->Set("test1", 7);
BOOST_CHECK(test3.IsEmpty());
}
-BOOST_AUTO_TEST_CASE(getproperty_dict)
+BOOST_AUTO_TEST_CASE(get2)
{
Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
Dictionary::Ptr other = boost::make_shared<Dictionary>();
BOOST_CHECK(!test2);
}
-BOOST_AUTO_TEST_CASE(remove_dict)
+BOOST_AUTO_TEST_CASE(foreach)
+{
+ Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
+ dictionary->Set("test1", 7);
+ dictionary->Set("test2", "hello world");
+
+ ObjectLock olock(dictionary);
+
+ bool seen_test1 = false, seen_test2 = false;
+
+ String key;
+ Value value;
+ BOOST_FOREACH(boost::tie(key, value), dictionary) {
+ BOOST_CHECK(key == "test1" || key == "test2");
+
+ if (key == "test1") {
+ BOOST_CHECK(!seen_test1);
+ seen_test1 = true;
+
+ BOOST_CHECK(value == 7);
+
+ continue;
+ } else if (key == "test2") {
+ BOOST_CHECK(!seen_test2);
+ seen_test2 = true;
+
+ BOOST_CHECK(value == "hello world");
+ }
+ }
+
+ BOOST_CHECK(seen_test1);
+ BOOST_CHECK(seen_test2);
+}
+
+BOOST_AUTO_TEST_CASE(remove)
{
Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
BOOST_CHECK(!dictionary->Contains("test2"));
BOOST_CHECK(dictionary->GetLength() == 0);
+
+ dictionary->Set("test1", 7);
+ dictionary->Set("test2", "hello world");
+
+ {
+ ObjectLock olock(dictionary);
+
+ Dictionary::Iterator it = dictionary->Begin();
+ dictionary->Remove(it);
+ }
+
+ BOOST_CHECK(dictionary->GetLength() == 1);
+}
+
+BOOST_AUTO_TEST_CASE(clone)
+{
+ Dictionary::Ptr dictionary = boost::make_shared<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(seal)
+{
+ Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
+ dictionary->Set("test1", 7);
+
+ BOOST_CHECK(!dictionary->IsSealed());
+ dictionary->Seal();
+ BOOST_CHECK(dictionary->IsSealed());
+
+ BOOST_CHECK_THROW(dictionary->Set("test2", "hello world"), boost::exception);
+ BOOST_CHECK(dictionary->GetLength() == 1);
+
+ BOOST_CHECK_THROW(dictionary->Set("test1", 8), boost::exception);
+ BOOST_CHECK(dictionary->Get("test1") == 7);
+
+ BOOST_CHECK_THROW(dictionary->Remove("test1"), boost::exception);
+ BOOST_CHECK(dictionary->GetLength() == 1);
+ BOOST_CHECK(dictionary->Get("test1") == 7);
+
+ {
+ ObjectLock olock(dictionary);
+ Dictionary::Iterator it = dictionary->Begin();
+ BOOST_CHECK_THROW(dictionary->Remove(it), boost::exception);
+ }
+
+ BOOST_CHECK(dictionary->GetLength() == 1);
+ BOOST_CHECK(dictionary->Get("test1") == 7);
+}
+
+BOOST_AUTO_TEST_CASE(serialize)
+{
+ Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
+
+ dictionary->Set("test1", 7);
+ dictionary->Set("test2", "hello world");
+
+ String json = Value(dictionary).Serialize();
+ BOOST_CHECK(json.GetLength() > 0);
+ Dictionary::Ptr deserialized = Value::Deserialize(json);
+ BOOST_CHECK(deserialized->GetLength() == 2);
+ BOOST_CHECK(deserialized->Get("test1") == 7);
+ BOOST_CHECK(deserialized->Get("test2") == "hello world");
}
BOOST_AUTO_TEST_SUITE_END()