Commit 4d2b9c2f authored by Ghislain MARY's avatar Ghislain MARY

Introduce unit tests.

parent a8538b58
......@@ -3,13 +3,16 @@ EXTRA_DIST=mediastreamer-config.h.in pkg.list autogen.sh mediastreamer.pc.in med
ACLOCAL_AMFLAGS = -I m4 $(ACLOCAL_MACOS_FLAGS)
SUBDIRS=po src build m4 include tests help
SUBDIRS=po src build m4 include tests help tester
ACLOCAL_FLAGS=-I$(top_srcdir)/m4
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = mediastreamer.pc
test:
cd tester && $(MAKE) test
## oRTP packaging methods:
# `make rpm'
......
......@@ -805,6 +805,47 @@ AC_ARG_ENABLE(tests,
AM_CONDITIONAL(ENABLE_TESTS, test x$tests = xyes)
dnl ##################################################
dnl # Check for CUnit
dnl ##################################################
PKG_CHECK_MODULES(CUNIT, cunit, [found_cunit=yes],[found_cunit=no])
if test "$found_cunit" = "no" ; then
AC_CHECK_HEADERS(CUnit/CUnit.h,
[
AC_CHECK_LIB(cunit,CU_add_suite,[
found_cunit=yes
CUNIT_LIBS+=" -lcunit"
])
])
fi
case "$target_os" in
*darwin*)
#hack for macport
CUNIT_LIBS+=" -lncurses"
;;
*mingw*)
CPPFLAGS="$CPPFLAGS -D_WIN32_WINNT=0x0501"
LIBS="$LIBS -lws2_32 -liphlpapi"
LDFLAGS="$LDFLAGS -Wl,--export-all-symbols"
;;
esac
if test "$found_cunit" = "no" ; then
AC_MSG_WARN([Could not find cunit framework, tests are not compiled.])
else
AC_CHECK_LIB(cunit,CU_get_suite,[
AC_DEFINE(HAVE_CU_GET_SUITE,1,[defined when CU_get_suite is available])
],[foo=bar],[$CUNIT_LIBS])
AC_CHECK_LIB(cunit,CU_curses_run_tests,[
AC_DEFINE(HAVE_CU_CURSES,1,[defined when CU_curses_run_tests is available])
],[foo=bar],[$CUNIT_LIBS])
fi
AM_CONDITIONAL(BUILD_TESTS,test x$found_cunit = xyes)
dnl ##################################################
dnl # Check for doxygen
dnl ##################################################
......@@ -902,6 +943,7 @@ AC_CONFIG_FILES(
mediastreamer2.spec
help/Makefile
help/Doxyfile
tester/Makefile
)
AC_OUTPUT
if BUILD_TESTS
noinst_PROGRAMS=mediastreamer2_tester
TESTS=mediastreamer2_tester
mediastreamer2_tester_SOURCES= \
mediastreamer2_tester.c \
mediastreamer2_dtmfgen_tester.c
mediastreamer2_tester_CFLAGS=$(CUNIT_CFLAGS) $(STRICT_OPTIONS)
mediastreamer2_tester_LDFLAGS=$(CUNIT_LIBS) $(AM_LDFLAGS)
AM_CPPFLAGS=-I$(top_srcdir)/include -I$(top_srcdir)/src
LDADD=$(top_builddir)/src/libmediastreamer_base.la $(top_builddir)/src/libmediastreamer_voip.la
AM_LDFLAGS=-no-undefined -export-dynamic
AM_CFLAGS=$(STRICT_OPTIONS)
test: mediastreamer2_tester
./mediastreamer2_tester $(TEST_OPTIONS)
else
test:
@echo "CUnit must be installed to be able to run the tests!"
endif
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006-2013 Belledonne Communications, Grenoble
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "mediastreamer2/mediastream.h"
#include "mediastreamer2/dtmfgen.h"
#include "mediastreamer2/mstonedetector.h"
#include "mediastreamer2_tester.h"
#include <stdio.h>
#include "CUnit/Basic.h"
typedef struct {
MSDtmfGenCustomTone generated_tone;
MSToneDetectorDef expected_tone;
} tone_test_def_t;
static MSTicker * create_ticker(void) {
MSTickerParams params = {0};
params.name = "Tester MSTicker";
params.prio = MS_TICKER_PRIO_NORMAL;
return ms_ticker_new_with_params(&params);
}
static void tone_detected_cb(void *data, MSFilter *f, unsigned int event_id, MSToneDetectorEvent *ev) {
unsigned char *tone_detected = (unsigned char *)data;
*tone_detected = TRUE;
}
static void dtmfgen_direct(void) {
MSConnectionHelper h;
MSTicker *ticker = NULL;
MSFilter *fileplay = NULL;
MSFilter *dtmfgen = NULL;
MSFilter *tonedet = NULL;
MSFilter *voidsink = NULL;
unsigned int i;
unsigned char tone_detected;
tone_test_def_t tone_definition[] = {
{ { 400, 2000, 0.6, 0 }, { "", 2000, 300, 0.5 } },
{ { 600, 1500, 1.0, 0 }, { "", 1500, 500, 0.9 } },
{ { 500, 941, 0.8, 0 }, { "", 941, 400, 0.7 } }
};
ms_init();
ticker = create_ticker();
CU_ASSERT_PTR_NOT_NULL_FATAL(ticker);
fileplay = ms_filter_new(MS_FILE_PLAYER_ID);
CU_ASSERT_PTR_NOT_NULL_FATAL(fileplay);
dtmfgen = ms_filter_new(MS_DTMF_GEN_ID);
CU_ASSERT_PTR_NOT_NULL_FATAL(dtmfgen);
tonedet = ms_filter_new(MS_TONE_DETECTOR_ID);
CU_ASSERT_PTR_NOT_NULL_FATAL(tonedet);
ms_filter_set_notify_callback(tonedet, (MSFilterNotifyFunc)tone_detected_cb, &tone_detected);
voidsink = ms_filter_new(MS_VOID_SINK_ID);
CU_ASSERT_PTR_NOT_NULL_FATAL(voidsink);
ms_connection_helper_start(&h);
ms_connection_helper_link(&h, fileplay, -1, 0);
ms_connection_helper_link(&h, dtmfgen, 0, 0);
ms_connection_helper_link(&h, tonedet, 0, 0);
ms_connection_helper_link(&h, voidsink, 0, -1);
ms_ticker_attach(ticker, dtmfgen);
for (i = 0; i < (sizeof(tone_definition) / sizeof(tone_definition[0])); i++) {
tone_detected = FALSE;
ms_filter_call_method(tonedet, MS_TONE_DETECTOR_CLEAR_SCANS, NULL);
ms_filter_call_method(tonedet, MS_TONE_DETECTOR_ADD_SCAN, &tone_definition[i].expected_tone);
ms_filter_call_method(dtmfgen, MS_DTMF_GEN_PLAY_CUSTOM, &tone_definition[i].generated_tone);
ms_sleep(1);
CU_ASSERT_EQUAL(tone_detected, TRUE);
}
ms_ticker_detach(ticker, dtmfgen);
ms_connection_helper_start(&h);
ms_connection_helper_unlink(&h, fileplay, -1, 0);
ms_connection_helper_unlink(&h, dtmfgen, 0, 0);
ms_connection_helper_unlink(&h, tonedet, 0, 0);
ms_connection_helper_unlink(&h, voidsink, 0, -1);
ms_filter_destroy(voidsink);
ms_filter_destroy(tonedet);
ms_filter_destroy(dtmfgen);
ms_filter_destroy(fileplay);
ms_ticker_destroy(ticker);
ms_exit();
}
test_t dtmfgen_tests[] = {
{ "dtmfgen-direct", dtmfgen_direct }
};
test_suite_t dtmfgen_test_suite = {
"dtmfgen",
sizeof(dtmfgen_tests) / sizeof(dtmfgen_tests[0]),
dtmfgen_tests
};
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006-2013 Belledonne Communications, Grenoble
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif
#include "mediastreamer2_tester.h"
#include <stdio.h>
#include "CUnit/Basic.h"
#if HAVE_CU_CURSES
#include "CUnit/CUCurses.h"
#endif
#include <mediastreamer2/mediastream.h>
#if HAVE_CU_CURSES
static unsigned char curses = 0;
#endif
static test_suite_t * test_suite[] = {
&dtmfgen_test_suite
};
static int run_test_suite(test_suite_t *suite) {
unsigned int i;
CU_pSuite pSuite = CU_add_suite(suite->name, NULL, NULL);
for (i = 0; i < suite->nb_tests; i++) {
if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) {
return CU_get_error();
}
}
return 0;
}
unsigned int mediastreamer2_tester_nb_test_suites(void) {
return (sizeof(test_suite) / sizeof(test_suite[0]));
}
unsigned int mediastreamer2_tester_nb_tests(const char *suite_name) {
unsigned int i;
for (i = 0; i < mediastreamer2_tester_nb_test_suites(); i++) {
if ((strcmp(suite_name, test_suite[i]->name) == 0) && (strlen(suite_name) == strlen(test_suite[i]->name))) {
return test_suite[i]->nb_tests;
}
}
return 0;
}
const char * mediastreamer2_tester_test_suite_name(unsigned int suite_index) {
if (suite_index >= mediastreamer2_tester_nb_test_suites()) return NULL;
return test_suite[suite_index]->name;
}
const char * mediastreamer2_tester_test_name(unsigned int suite_index, unsigned int test_index) {
if (suite_index >= mediastreamer2_tester_nb_test_suites()) return NULL;
if (test_index >= test_suite[suite_index]->nb_tests) return NULL;
return test_suite[suite_index]->tests[test_index].name;
}
int mediastreamer2_tester_run_tests(const char *suite_name, const char *test_name) {
unsigned int i;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
for (i = 0; i < mediastreamer2_tester_nb_test_suites(); i++) {
run_test_suite(test_suite[i]);
}
#if HAVE_CU_GET_SUITE
if (suite_name){
CU_pSuite suite;
CU_basic_set_mode(CU_BRM_VERBOSE);
suite=CU_get_suite(suite_name);
if (test_name) {
CU_pTest test=CU_get_test_by_name(test_name, suite);
CU_basic_run_test(suite, test);
} else
CU_basic_run_suite(suite);
} else
#endif
{
#if HAVE_CU_CURSES
if (curses) {
/* Run tests using the CUnit curses interface */
CU_curses_run_tests();
}
else
#endif
{
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
}
}
CU_cleanup_registry();
return CU_get_error();
}
#if !WINAPI_FAMILY_APP
int main (int argc, char *argv[]) {
int i;
char *suite_name = NULL;
char *test_name = NULL;
unsigned char verbose = FALSE;
for(i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--help") == 0) {
fprintf(stderr, "%s \t--help\n"
"\t\t\t--verbose\n"
#if HAVE_CU_GET_SUITE
"\t\t\t--suite <suite name>\n"
"\t\t\t--test <test name>\n"
#endif
#if HAVE_CU_CURSES
"\t\t\t--curses\n"
#endif
, argv[0]);
return 0;
} else if (strcmp(argv[i], "--verbose") == 0) {
verbose = TRUE;
}
#if HAVE_CU_GET_SUITE
else if (strcmp(argv[i], "--test")==0) {
i++;
test_name = argv[i];
} else if (strcmp(argv[i], "--suite") == 0) {
i++;
suite_name = argv[i];
}
#endif
#if HAVE_CU_CURSES
else if (strcmp(argv[i], "--curses") == 0) {
i++;
curses = 1;
}
#endif
}
if (verbose) {
setenv("MEDIASTREAMER_DEBUG", "1", 1);
} else {
unsetenv("MEDIASTREAMER_DEBUG");
}
return mediastreamer2_tester_run_tests(suite_name, test_name);
}
#endif
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006-2013 Belledonne Communications, Grenoble
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _MEDIASTREAMER2_TESTER_H
#define _MEDIASTREAMER2_TESTER_H
typedef void (*test_function_t)(void);
typedef int (*test_suite_function_t)(const char *name);
typedef struct {
const char *name;
test_function_t func;
} test_t;
typedef struct {
const char *name;
unsigned int nb_tests;
test_t *tests;
} test_suite_t;
extern test_suite_t dtmfgen_test_suite;
unsigned int mediastreamer2_tester_nb_test_suites(void);
unsigned int mediastreamer2_tester_nb_tests(const char *suite_name);
const char * mediastreamer2_tester_test_suite_name(unsigned int suite_index);
const char * mediastreamer2_tester_test_name(unsigned int suite_index, unsigned int test_index);
int mediastreamer2_tester_run_tests(const char *suite_name, const char *test_name);
#endif /* _MEDIASTREAMER2_TESTER_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment