Add unit test
authorDongHun Kwak <dh0128.kwak@samsung.com>
Mon, 20 Apr 2020 06:04:11 +0000 (15:04 +0900)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Wed, 22 Apr 2020 01:19:27 +0000 (10:19 +0900)
Change-Id: Ibaa58bac5f2da5ae0217e8cc294f44e09d347730
Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
CMakeLists.txt
packaging/libslp-db-util.spec
unittest/CMakeLists.txt [new file with mode: 0644]
unittest/db-util_gtest.cc [new file with mode: 0644]
unittest/run_coverage.sh [new file with mode: 0755]

index 015a70c..15f3e94 100644 (file)
@@ -48,3 +48,6 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/db-util-common.h DESTINATION i
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/util-func.h DESTINATION include/db-util)
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/collation.h DESTINATION include/db-util)
 
+IF( DEFINED GCOV )
+ADD_SUBDIRECTORY(unittest)
+ENDIF()
index d44b309..1f2c5ae 100755 (executable)
@@ -14,6 +14,11 @@ BuildRequires:  pkgconfig(icu-i18n)
 BuildRequires:  pkgconfig(sqlite3)
 BuildRequires:  pkgconfig(vconf)
 
+%if 0%{?gcov:1}
+BuildRequires:  gtest-devel
+BuildRequires:  lcov
+%endif
+
 %description
 DB Utility.
 
@@ -23,6 +28,14 @@ Requires:       %{name} = %{version}
 %description devel
 Devel package for libslp-db-util (devel)
 
+%if 0%{?gcov:1}
+%package gcov
+Summary:  DB Utility(gcov)
+Group:    Application Framework/Database
+%description gcov
+libslp-db-util gcov objects
+%endif
+
 %prep
 %setup -q
 cp %{SOURCE1001} %{SOURCE1002} .
@@ -35,12 +48,37 @@ export CXXFLAGS+=" $CXXFLAGS -DDB_UTIL_ARCH_64 "
 export FFLAGS+=" $FFLAGS -DDB_UTIL_ARCH_64 "
 %endif
 
-%cmake .
+%if 0%{?gcov:1}
+export CFLAGS+=" -O0 -fprofile-arcs -ftest-coverage"
+export CXXFLAGS+=" -O0 -fprofile-arcs -ftest-coverage"
+%define CMAKE_GCOV -DGCOV=1
+%else
+%define CMAKE_GCOV %{nil}
+%endif
+
+%cmake . %{CMAKE_GCOV}
 make %{?_smp_mflags}
 
+%if 0%{?gcov:1}
+mkdir -p gcov-obj
+find . -name '*.gcno' -exec cp '{}' gcov-obj ';'
+%endif
+
 %install
 %make_install
 
+%if 0%{?gcov:1}
+mkdir -p %{buildroot}%{_datadir}/gcov/obj
+install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
+%endif
+
+%check
+%if 0%{?gcov:1}
+pushd unittest
+./run_coverage.sh
+popd
+%endif
+
 %post -p /sbin/ldconfig
 
 %postun -p /sbin/ldconfig
@@ -60,3 +98,10 @@ make %{?_smp_mflags}
 %{_libdir}/pkgconfig/db-util.pc
 %{_libdir}/libSLP-db-util.so
 
+
+%if 0%{?gcov:1}
+%files gcov
+%manifest %{name}-devel.manifest
+%defattr(-,root,root,-)
+%{_datadir}/gcov/obj/*
+%endif
diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5e0daa6
--- /dev/null
@@ -0,0 +1,18 @@
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE")
+
+
+pkg_check_modules(gtest_pkgs REQUIRED sqlite3 dlog glib-2.0 icu-i18n )
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${gtest_pkgs_INCLUDE_DIRS})
+
+LINK_DIRECTORIES(${gtest_pkgs_LIBRARY_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})
+
+#=======================================================================================#
+SET(DB-UTIL_GTEST "db-util_gtest")
+SET(DB-UTIL_GTEST_SRCS db-util_gtest.cc)
+ADD_EXECUTABLE(${DB-UTIL_GTEST} ${DB-UTIL_GTEST_SRCS})
+TARGET_LINK_LIBRARIES(${DB-UTIL_GTEST} ${gtest_pkgs_LIBRARIES} gtest pthread SLP-db-util )
+# INSTALL(TARGETS ${DB-UTIL_GTEST} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
+#=======================================================================================#
diff --git a/unittest/db-util_gtest.cc b/unittest/db-util_gtest.cc
new file mode 100644 (file)
index 0000000..d8d3d80
--- /dev/null
@@ -0,0 +1,104 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "gtest/gtest.h"
+
+
+#include <db-util.h>
+
+#define FILE_LEN 255
+
+//=======================================================================================
+class DbUtil : public ::testing::Test {
+    public:
+        char    pszFilePath[FILE_LEN + 1];
+        sqlite3 *db;
+        int     nOption;
+        int     flags;
+        const char* zVfs;
+
+        DbUtil() {
+            strncpy(pszFilePath, "test.db", FILE_LEN);
+            db = NULL;
+            nOption = 0;
+            flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
+            zVfs = NULL;
+        }
+
+        void SetUp() {
+        }
+
+        void TearDown() {
+            int rc;
+            rc = system("rm -f test.db");
+        }
+};
+
+TEST_F(DbUtil, db_util_open_p) {
+        int rc;
+
+        rc = db_util_open(pszFilePath, &db, nOption);
+        ASSERT_EQ ( DB_UTIL_OK , rc );
+
+        rc = db_util_close(db);
+        ASSERT_EQ ( DB_UTIL_OK , rc );
+}
+
+TEST_F(DbUtil, db_util_open_n1) {
+        int rc;
+
+        // Invalid db name
+        rc = db_util_open(".", &db, nOption);
+        ASSERT_NE ( DB_UTIL_OK , rc );
+}
+
+TEST_F(DbUtil, db_util_open_n2) {
+        int rc;
+
+        // Invalid db pointer
+        rc = db_util_open(pszFilePath, NULL, nOption);
+        ASSERT_NE ( DB_UTIL_OK , rc );
+}
+
+TEST_F(DbUtil, db_util_close_n) {
+        int rc;
+
+        // Invalid db close
+        rc = db_util_close(NULL);
+        ASSERT_EQ ( DB_UTIL_OK , rc );
+}
+
+TEST_F(DbUtil, db_util_open_with_options_p) {
+        int rc;
+
+        rc = db_util_open_with_options(pszFilePath, &db, flags, zVfs);
+        ASSERT_EQ ( DB_UTIL_OK , rc );
+
+        rc = db_util_close(db);
+        ASSERT_EQ ( DB_UTIL_OK , rc );
+}
+
+TEST_F(DbUtil, db_util_open_with_options_n1) {
+        int rc;
+
+        // Invalid db name
+        rc = db_util_open_with_options(".", &db, flags, zVfs);
+        ASSERT_NE ( DB_UTIL_OK , rc );
+}
+
+TEST_F(DbUtil, db_util_open_with_options_n2) {
+        int rc;
+
+        // Invalid db name
+        rc = db_util_open_with_options(pszFilePath, NULL, flags, zVfs);
+        ASSERT_NE ( DB_UTIL_OK , rc );
+}
+//=======================================================================================
+
+
+
+int main(int argc, char** argv) {
+    ::testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
+
+
diff --git a/unittest/run_coverage.sh b/unittest/run_coverage.sh
new file mode 100755 (executable)
index 0000000..693149f
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/bash
+export LD_LIBRARY_PATH=..:.:$LD_LIBRARY_PATH
+pushd ../ 
+RootDir=$PWD
+popd
+
+unitTestFile=db-util_gtest
+if [ ! -x  "./$unitTestFile" ]
+then
+    echo "$unitTestFile file does not exist!"
+    exit -1
+fi
+./$unitTestFile
+
+
+CMakeDir=${RootDir}/CMakeFiles/SLP-db-util.dir
+CoverageDir=${RootDir}/coverage
+
+pushd $CMakeDir 
+    for obj in `ls *.o`
+    do  
+        gcov -b -c $obj
+    done
+
+    if [ -f /usr/bin/lcov ]
+    then
+        lcov -c -d . -o cov.info
+        genhtml cov.info -o ${CoverageDir}
+        echo "Coverage test result created! [${CoverageDir}]" 
+    else
+        echo "lcov does not exist!"
+    fi
+popd
+