summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Figuiere <hub@figuiere.net>2007-12-19 17:39:31 -0500
committerHubert Figuiere <hub@figuiere.net>2007-12-19 17:39:31 -0500
commitb9e2427e7d426f76f8bb6f2203bf487689a80f59 (patch)
tree62d43c3518be35a254c36dfd42eb5f75c5f03730
parenta6912041f54684966dd8328b2e4f4df18f4c21bc (diff)
* exempi/tests/*, configure.ac: detect valgrind.
Add leak detection support.
-rw-r--r--ChangeLog5
-rw-r--r--NEWS1
-rw-r--r--configure.ac21
-rw-r--r--exempi/tests/Makefile.am2
-rw-r--r--exempi/tests/test1.cpp11
-rw-r--r--exempi/tests/test2.cpp6
-rw-r--r--exempi/tests/test3.cpp3
-rw-r--r--exempi/tests/testinit.cpp2
-rw-r--r--exempi/tests/utils.cpp62
-rw-r--r--exempi/tests/utils.h21
10 files changed, 130 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index d84dc23..bef17c6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2007-12-19 Hubert Figuiere <hub@figuiere.net>
+
+ * exempi/tests/*, configure.ac: detect valgrind.
+ Add leak detection support.
+
2007-12-17 Hubert Figuiere <hub@figuiere.net>
* autogen.sh: added for convenience for non-tarball builders.
diff --git a/NEWS b/NEWS
index 454ecb8..178eb5e 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@
- New: xmp_get_property_date() / xmp_set_property_date()
- Test: refactor a the tests preparation
- Test: add a test for multiple initializations.
+- Test: use valgrind is available.
- Bug: fix configure to allow building on MacOS X. Closes bug #13596
- Bug #13707: add autogen.sh
diff --git a/configure.ac b/configure.ac
index 14fe600..565d727 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,4 @@
- AC_PREREQ(2.50)
+AC_PREREQ(2.50)
AC_INIT(exempi/xmp.h)
AM_INIT_AUTOMAKE(exempi, 1.99.6)
AM_MAINTAINER_MODE
@@ -86,7 +86,6 @@ AC_CHECK_HEADER(iconv.h, ,
AX_BOOST_BASE([1.33.0])
AX_BOOST_UNIT_TEST_FRAMEWORK
-
case $build_vendor in
apple)
EXEMPI_PLATFORM_DEF=MAC_ENV
@@ -100,6 +99,24 @@ esac
AC_SUBST(EXEMPI_PLATFORM_DEF)
+AM_CONDITIONAL(WITH_UNIT_TEST, true || test "x$HAVE_BOOST" != "x")
+
+dnl Check for valgrind (optional) for make check.
+AC_CHECK_PROG(VALGRIND, valgrind, valgrind)
+AC_CHECK_HEADERS(valgrind/memcheck.h)
+if test "$VALGRIND" = ""; then
+ AC_MSG_WARN([Valgrind is missing. checks will run without])
+fi
+if test "$VALGRIND" != ""; then
+dnl due to stupid m4 I had to double the [ and ] in the regexp
+ vg_ver=`valgrind --version | sed -e 's/.*-\([[0-9]]\)\.[[0-9]].*/\1/'`
+ VALGRIND="$VALGRIND --tool=memcheck --leak-check=yes --num-callers=10"
+ if test "$vg_ver" != "3" ; then
+ VALGRIND="$VALGRIND --logfile-fd=-1"
+ fi
+fi
+AC_SUBST(VALGRIND)
+
AC_OUTPUT([
Makefile
diff --git a/exempi/tests/Makefile.am b/exempi/tests/Makefile.am
index 5733b0d..b2ba98f 100644
--- a/exempi/tests/Makefile.am
+++ b/exempi/tests/Makefile.am
@@ -49,7 +49,7 @@ EXTRA_DIST = $(check_DATA) $(check_SCRIPTS)
CLEANFILES = test.jpg
TESTS = testcore.sh testinit test1 test2 test3
-TESTS_ENVIRONMENT = TEST_DIR=$(srcdir)
+TESTS_ENVIRONMENT = TEST_DIR=$(srcdir) @VALGRIND@
testinit_SOURCES = testinit.cpp utils.cpp
testinit_LDADD = ../libexempi.la @BOOST_UNIT_TEST_FRAMEWORK_LIB@
diff --git a/exempi/tests/test1.cpp b/exempi/tests/test1.cpp
index 0d66bab..950af8a 100644
--- a/exempi/tests/test1.cpp
+++ b/exempi/tests/test1.cpp
@@ -127,6 +127,9 @@ void test_write_new_property()
fclose(f);
xmp_terminate();
+
+ BOOST_CHECK(!g_lt->check_leaks());
+ BOOST_CHECK(!g_lt->check_errors());
}
@@ -171,6 +174,9 @@ void test_serialize()
fclose(f);
xmp_terminate();
+
+ BOOST_CHECK(!g_lt->check_leaks());
+ BOOST_CHECK(!g_lt->check_errors());
}
@@ -287,6 +293,9 @@ void test_exempi()
fclose(f);
xmp_terminate();
+
+ BOOST_CHECK(!g_lt->check_leaks());
+ BOOST_CHECK(!g_lt->check_errors());
}
@@ -301,7 +310,7 @@ init_unit_test_suite( int argc, char * argv[] )
test->add(BOOST_TEST_CASE(&test_exempi));
test->add(BOOST_TEST_CASE(&test_serialize));
test->add(BOOST_TEST_CASE(&test_write_new_property));
-
+
return test;
}
diff --git a/exempi/tests/test2.cpp b/exempi/tests/test2.cpp
index 395bcbf..0671e2c 100644
--- a/exempi/tests/test2.cpp
+++ b/exempi/tests/test2.cpp
@@ -107,6 +107,9 @@ void test_xmpfiles_write()
// unlink("test.jpg");
xmp_terminate();
+
+ BOOST_CHECK(!g_lt->check_leaks());
+ BOOST_CHECK(!g_lt->check_errors());
}
@@ -138,6 +141,9 @@ void test_xmpfiles()
BOOST_CHECK(xmp_files_free(f));
xmp_terminate();
+
+ BOOST_CHECK(!g_lt->check_leaks());
+ BOOST_CHECK(!g_lt->check_errors());
}
diff --git a/exempi/tests/test3.cpp b/exempi/tests/test3.cpp
index 5529b8f..13b1719 100644
--- a/exempi/tests/test3.cpp
+++ b/exempi/tests/test3.cpp
@@ -109,6 +109,9 @@ void test_exempi_iterate()
free(buffer);
fclose(f);
xmp_terminate();
+
+ BOOST_CHECK(!g_lt->check_leaks());
+ BOOST_CHECK(!g_lt->check_errors());
}
diff --git a/exempi/tests/testinit.cpp b/exempi/tests/testinit.cpp
index 0e4dd1d..2de513f 100644
--- a/exempi/tests/testinit.cpp
+++ b/exempi/tests/testinit.cpp
@@ -88,6 +88,8 @@ void test_exempi_init()
xmp_terminate();
free(buffer);
+ BOOST_CHECK(!g_lt->check_leaks());
+ BOOST_CHECK(!g_lt->check_errors());
}
diff --git a/exempi/tests/utils.cpp b/exempi/tests/utils.cpp
index 3edadf4..e93af00 100644
--- a/exempi/tests/utils.cpp
+++ b/exempi/tests/utils.cpp
@@ -40,6 +40,7 @@
#include "utils.h"
std::string g_testfile;
+boost::scoped_ptr<LeakTracker> g_lt(new LeakTracker) ;
void prepare_test(int argc, char * argv[], const char *filename)
{
@@ -56,3 +57,64 @@ void prepare_test(int argc, char * argv[], const char *filename)
g_testfile = argv[1];
}
}
+
+
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+# include <valgrind/memcheck.h>
+# include <valgrind/valgrind.h>
+# define CC_EXTENSION __extension__
+#else
+# define VALGRIND_COUNT_ERRORS 0
+# define VALGRIND_DO_LEAK_CHECK
+# define VALGRIND_COUNT_LEAKS(a,b,c,d) (a=b=c=d=0)
+# define CC_EXTENSION
+#endif
+
+
+
+LeakTracker::LeakTracker()
+ : m_leaks(0),
+ m_dubious(0),
+ m_reachable(0),
+ m_suppressed(0),
+ m_errors(0)
+{
+
+}
+
+
+LeakTracker::~LeakTracker()
+{
+ printf("LeakTracker: leaked = %d, errors = %d\n", m_leaks, m_errors);
+}
+
+
+int LeakTracker::check_leaks()
+{
+ int leaked = 0;
+ int dubious = 0;
+ int reachable = 0;
+ int suppressed = 0;
+
+ VALGRIND_DO_LEAK_CHECK;
+ VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
+ printf("memleaks: sure:%d dubious:%d reachable:%d suppress:%d\n",
+ leaked, dubious, reachable, suppressed);
+ bool has_leaks = (m_leaks != leaked);
+
+ m_leaks = leaked;
+ m_dubious = dubious;
+ m_reachable = reachable;
+ m_suppressed = suppressed;
+
+ return has_leaks;
+}
+
+int LeakTracker::check_errors()
+{
+ int errors = (int)(CC_EXTENSION VALGRIND_COUNT_ERRORS);
+ bool has_new_errors = (m_errors != errors);
+ m_errors = errors;
+ return has_new_errors;
+}
diff --git a/exempi/tests/utils.h b/exempi/tests/utils.h
index 64da7ea..efe4969 100644
--- a/exempi/tests/utils.h
+++ b/exempi/tests/utils.h
@@ -42,8 +42,29 @@
#include <string>
+#include <boost/scoped_ptr.hpp>
+
extern std::string g_testfile;
void prepare_test(int argc, char * argv[], const char * filename);
+class LeakTracker
+{
+public:
+ LeakTracker();
+ ~LeakTracker();
+/** return false if there is NO leak. Will print on stdout */
+ int check_leaks();
+ int check_errors();
+private:
+ int m_leaks;
+ int m_dubious;
+ int m_reachable;
+ int m_suppressed;
+ int m_errors;
+};
+
+
+extern boost::scoped_ptr<LeakTracker> g_lt;
+
#endif