]> granicus.if.org Git - icinga2/commitdiff
Add some unit tests.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 18 Apr 2013 14:40:09 +0000 (16:40 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 18 Apr 2013 14:40:09 +0000 (16:40 +0200)
configure.ac
test/Makefile.am
test/base-array.cpp [new file with mode: 0644]
test/base-convert.cpp [new file with mode: 0644]
test/base-dictionary.cpp
test/base-fifo.cpp [new file with mode: 0644]
test/base-object.cpp [new file with mode: 0644]

index f78d1ea128c44332c01a6044e380cbf6fe4fa1cf..723c2a02d52b6f1377689159be8636de15f460b9 100644 (file)
@@ -73,17 +73,28 @@ AC_CHECK_LIB(ws2_32, getsockname)
 AC_CHECK_LIB(shlwapi, PathRemoveFileSpecA)
 AC_CHECK_FUNCS([backtrace_symbols execvpe pipe2])
 
+CFLAGS="$CFLAGS -Wall -Wextra"
+CXXFLAGS="$CXXFLAGS -Wall -Wextra"
+
 AC_MSG_CHECKING(whether to enable debugging)
 AC_ARG_ENABLE(debug, [  --enable-debug=[no/yes]   turn on debugging (default=no)],, enable_debug=no)
 if test "x$enable_debug" = "xyes"; then
-       CFLAGS="$CFLAGS -g -O0 -D_DEBUG -Wall -Wextra"
-       CXXFLAGS="$CXXFLAGS -g -O0 -D_DEBUG -Wall -Wextra"
+       CFLAGS="$CFLAGS -g -O0 -D_DEBUG"
+       CXXFLAGS="$CXXFLAGS -g -O0 -D_DEBUG"
 else
        CFLAGS="$CFLAGS -DNDEBUG"
        CXXFLAGS="$CXXFLAGS -DNDEBUG"
 fi
 AC_MSG_RESULT($enable_debug)
 
+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"
+fi
+AC_MSG_RESULT($enable_coverage)
+
 AX_PYTHON_DEFAULT
 AX_PYTHON_ENABLE
 AX_PYTHON_VERSION_ENSURE([2.5])
index 48e8a8eebc9c0917e977adca0e8c05b7e18afeaa..6a146296fa40e0cc87ae0721d2a14bcf639a5ba7 100644 (file)
@@ -8,7 +8,11 @@ check_PROGRAMS = \
 
 icinga2_test_SOURCES = \
        test.cpp \
+       base-array.cpp \
+       base-convert.cpp \
        base-dictionary.cpp \
+       base-fifo.cpp \
+       base-object.cpp \
        base-shellescape.cpp
 
 icinga2_test_CPPFLAGS = \
diff --git a/test/base-array.cpp b/test/base-array.cpp
new file mode 100644 (file)
index 0000000..2d3515d
--- /dev/null
@@ -0,0 +1,91 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ *                                                                            *
+ * 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"
+#include <boost/test/unit_test.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+#include <boost/foreach.hpp>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(base_array)
+
+BOOST_AUTO_TEST_CASE(construct)
+{
+       Array::Ptr array = boost::make_shared<Array>();
+       BOOST_CHECK(array);
+       BOOST_CHECK(array->GetLength() == 0);
+}
+
+BOOST_AUTO_TEST_CASE(getset)
+{
+       Array::Ptr array = boost::make_shared<Array>();
+       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);
+}
+
+BOOST_AUTO_TEST_CASE(foreach)
+{
+       Array::Ptr array = boost::make_shared<Array>();
+       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)
+{
+       Array::Ptr array = boost::make_shared<Array>();
+       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);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/base-convert.cpp b/test/base-convert.cpp
new file mode 100644 (file)
index 0000000..3bb283a
--- /dev/null
@@ -0,0 +1,35 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ *                                                                            *
+ * 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/convert.h"
+#include <boost/test/unit_test.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(base_convert)
+
+BOOST_AUTO_TEST_CASE(tolong)
+{
+       BOOST_CHECK_THROW(Convert::ToLong(" 7"), boost::exception);
+       BOOST_CHECK(Convert::ToLong("7") == 7);
+       BOOST_CHECK_THROW(Convert::ToLong("7a"), boost::exception);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
index 3196b4dfbee1faa5e153a8aaa58bc54c7184b274..a05142060fd238753560352e1328d4511378409d 100644 (file)
@@ -37,6 +37,8 @@ BOOST_AUTO_TEST_CASE(getproperty)
        dictionary->Set("test1", 7);
        dictionary->Set("test2", "hello world");
 
+       BOOST_CHECK(dictionary->GetLength() == 2);
+
        Value test1;
        test1 = dictionary->Get("test1");
        BOOST_CHECK(test1 == 7);
@@ -45,8 +47,9 @@ BOOST_AUTO_TEST_CASE(getproperty)
        test2 = dictionary->Get("test2");
        BOOST_CHECK(test2 == "hello world");
 
+       String key3 = "test3";
        Value test3;
-       test3 = dictionary->Get("test3");
+       test3 = dictionary->Get(key3);
        BOOST_CHECK(test3.IsEmpty());
 }
 
@@ -57,6 +60,8 @@ BOOST_AUTO_TEST_CASE(getproperty_dict)
 
        dictionary->Set("test1", other);
 
+       BOOST_CHECK(dictionary->GetLength() == 1);
+
        Dictionary::Ptr test1 = dictionary->Get("test1");
        BOOST_CHECK(other == test1);
 
@@ -64,4 +69,25 @@ BOOST_AUTO_TEST_CASE(getproperty_dict)
        BOOST_CHECK(!test2);
 }
 
+BOOST_AUTO_TEST_CASE(remove_dict)
+{
+       Dictionary::Ptr dictionary = boost::make_shared<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() == 1);
+
+       dictionary->Remove("test2");
+
+       BOOST_CHECK(!dictionary->Contains("test2"));
+       BOOST_CHECK(dictionary->GetLength() == 0);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/base-fifo.cpp b/test/base-fifo.cpp
new file mode 100644 (file)
index 0000000..d7e93a1
--- /dev/null
@@ -0,0 +1,55 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ *                                                                            *
+ * 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/fifo.h"
+#include "base/objectlock.h"
+#include <boost/test/unit_test.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+#include <boost/foreach.hpp>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(base_fifo)
+
+BOOST_AUTO_TEST_CASE(construct)
+{
+       FIFO::Ptr fifo = boost::make_shared<FIFO>();
+       BOOST_CHECK(fifo);
+       BOOST_CHECK(fifo->GetAvailableBytes() == 0);
+}
+
+BOOST_AUTO_TEST_CASE(io)
+{
+       FIFO::Ptr fifo = boost::make_shared<FIFO>();
+
+       fifo->Write("hello", 5);
+       BOOST_CHECK(fifo->GetAvailableBytes() == 5);
+
+       char buffer1[2];
+       fifo->Read(buffer1, 2);
+       BOOST_CHECK(memcmp(buffer1, "he", 2) == 0);
+       BOOST_CHECK(fifo->GetAvailableBytes() == 3);
+
+       char buffer2[3];
+       fifo->Read(buffer2, 3);
+       BOOST_CHECK(memcmp(buffer2, "llo", 3) == 0);
+       BOOST_CHECK(fifo->GetAvailableBytes() == 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/base-object.cpp b/test/base-object.cpp
new file mode 100644 (file)
index 0000000..bacead0
--- /dev/null
@@ -0,0 +1,62 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ *                                                                            *
+ * 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/object.h"
+#include <boost/test/unit_test.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+
+using namespace icinga;
+
+class TestObject : public Object
+{
+public:
+       typedef boost::shared_ptr<TestObject> Ptr;
+       typedef boost::weak_ptr<TestObject> WeakPtr;
+
+       TestObject::Ptr GetTestRef(void)
+       {
+               return GetSelf();
+       }
+};
+
+BOOST_AUTO_TEST_SUITE(base_object)
+
+BOOST_AUTO_TEST_CASE(construct)
+{
+       Object::Ptr tobject = boost::make_shared<TestObject>();
+       BOOST_CHECK(tobject);
+}
+
+BOOST_AUTO_TEST_CASE(getself)
+{
+       TestObject::Ptr tobject = boost::make_shared<TestObject>();
+       TestObject::Ptr tobject_self = tobject->GetTestRef();
+       BOOST_CHECK(tobject == tobject_self);
+}
+
+BOOST_AUTO_TEST_CASE(weak)
+{
+       TestObject::Ptr tobject = boost::make_shared<TestObject>();
+       TestObject::WeakPtr wtobject = tobject;
+       tobject.reset();
+       BOOST_CHECK(!tobject);
+       BOOST_CHECK(!wtobject.lock());
+}
+
+BOOST_AUTO_TEST_SUITE_END()