mediastreamer2_tester.c 7.56 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
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_tester.h"

Simon Morlat's avatar
Simon Morlat committed
22
#include <mediastreamer2/mediastream.h>
23
#if HAVE_CONFIG_H
Simon Morlat's avatar
Simon Morlat committed
24
#include <mediastreamer-config.h>
25
#endif
Simon Morlat's avatar
Simon Morlat committed
26

Ghislain MARY's avatar
Ghislain MARY committed
27 28 29 30 31
#include <stdio.h>
#include "CUnit/Basic.h"
#if HAVE_CU_CURSES
#include "CUnit/CUCurses.h"
#endif
jehan's avatar
jehan committed
32 33 34
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
35

36

37 38 39 40
static test_suite_t **test_suite = NULL;
static int nb_test_suites = 0;


Ghislain MARY's avatar
Ghislain MARY committed
41 42 43 44
#if HAVE_CU_CURSES
static unsigned char curses = 0;
#endif

45 46 47 48 49 50 51 52 53 54
static void add_test_suite(test_suite_t *suite) {
	if (test_suite == NULL) {
		test_suite = (test_suite_t **)malloc(10 * sizeof(test_suite_t *));
	}
	test_suite[nb_test_suites] = suite;
	nb_test_suites++;
	if ((nb_test_suites % 10) == 0) {
		test_suite = (test_suite_t **)realloc(test_suite, (nb_test_suites + 10) * sizeof(test_suite_t *));
	}
}
Ghislain MARY's avatar
Ghislain MARY committed
55 56

static int run_test_suite(test_suite_t *suite) {
57
	int i;
Ghislain MARY's avatar
Ghislain MARY committed
58

59
	CU_pSuite pSuite = CU_add_suite(suite->name, suite->init_func, suite->cleanup_func);
Ghislain MARY's avatar
Ghislain MARY committed
60 61 62 63 64 65 66 67 68 69

	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;
}

70 71
static int test_suite_index(const char *suite_name) {
	int i;
Ghislain MARY's avatar
Ghislain MARY committed
72 73 74

	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))) {
75
			return i;
Ghislain MARY's avatar
Ghislain MARY committed
76 77 78
		}
	}

79
	return -1;
Ghislain MARY's avatar
Ghislain MARY committed
80
}
81 82

#if HAVE_CU_GET_SUITE
83 84 85 86 87 88 89 90 91 92 93 94 95 96
static void list_suite_tests(const char *suite_name) {
	int j;
	for( j = 0; j < mediastreamer2_tester_nb_tests(suite_name); j++) {
		const char *test_name = mediastreamer2_tester_test_name(suite_name, j);
		fprintf(stdout, "%s\n", test_name);
	}
}
static void list_suites() {
	int j;
	for(j = 0; j < mediastreamer2_tester_nb_test_suites(); j++) {
		const char *suite_name = mediastreamer2_tester_test_suite_name(j);
		fprintf(stdout, "%s\n", suite_name);
	}
}
97
#endif
Ghislain MARY's avatar
Ghislain MARY committed
98

99 100 101 102 103 104 105 106 107 108 109
int mediastreamer2_tester_nb_test_suites(void) {
	return nb_test_suites;
}

int mediastreamer2_tester_nb_tests(const char *suite_name) {
	int i = test_suite_index(suite_name);
	if (i < 0) return 0;
	return test_suite[i]->nb_tests;
}

const char * mediastreamer2_tester_test_suite_name(int suite_index) {
Ghislain MARY's avatar
Ghislain MARY committed
110 111 112 113
	if (suite_index >= mediastreamer2_tester_nb_test_suites()) return NULL;
	return test_suite[suite_index]->name;
}

114 115 116
const char * mediastreamer2_tester_test_name(const char *suite_name, int test_index) {
	int suite_index = test_suite_index(suite_name);
	if ((suite_index < 0) || (suite_index >= mediastreamer2_tester_nb_test_suites())) return NULL;
Ghislain MARY's avatar
Ghislain MARY committed
117 118 119 120
	if (test_index >= test_suite[suite_index]->nb_tests) return NULL;
	return test_suite[suite_index]->tests[test_index].name;
}

121
void mediastreamer2_tester_init(void) {
122 123
	int i;

Ghislain MARY's avatar
Ghislain MARY committed
124 125
	add_test_suite(&basic_audio_test_suite);
	add_test_suite(&sound_card_test_suite);
126
	add_test_suite(&adaptive_test_suite);
jehan's avatar
jehan committed
127
	add_test_suite(&audio_stream_test_suite);
128
#ifdef VIDEO_ENABLED
129
	add_test_suite(&video_stream_test_suite);
130
#endif
131
	add_test_suite(&framework_test_suite);
François Grisez's avatar
François Grisez committed
132
	add_test_suite(&player_test_suite);
133 134 135
#ifdef __ARM_NEON__
    add_test_suite(&neon_test_suite);
#endif
136 137 138 139
	for (i = 0; i < mediastreamer2_tester_nb_test_suites(); i++) {
		run_test_suite(test_suite[i]);
	}

140 141 142 143 144 145 146 147 148 149
}

void mediastreamer2_tester_uninit(void) {
	if (test_suite != NULL) {
		free(test_suite);
		test_suite = NULL;
		nb_test_suites = 0;
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
150
int mediastreamer2_tester_run_tests(const char *suite_name, const char *test_name) {
151
	int ret;
Ghislain MARY's avatar
Ghislain MARY committed
152 153 154 155 156 157

#if HAVE_CU_GET_SUITE
	if (suite_name){
		CU_pSuite suite;
		CU_basic_set_mode(CU_BRM_VERBOSE);
		suite=CU_get_suite(suite_name);
158
		if (!suite) {
159
			fprintf(stderr, "Could not find suite '%s'. Available suites are:\n", suite_name);
160 161
			list_suites();
		} else if (test_name) {
Ghislain MARY's avatar
Ghislain MARY committed
162
			CU_pTest test=CU_get_test_by_name(test_name, suite);
163
			if (!test) {
164
				fprintf(stderr, "Could not find test '%s' in suite '%s'. Available tests are:\n", test_name, suite_name);
165 166 167 168
				// do not use suite_name here, since this method is case sentisitive
				list_suite_tests(suite->pName);
			} else {
				CU_ErrorCode err= CU_basic_run_test(suite, test);
169
				if (err != CUE_SUCCESS) fprintf(stderr, "CU_basic_run_test error=%d\n", err);
170 171
			}
		} else {
Ghislain MARY's avatar
Ghislain MARY committed
172
			CU_basic_run_suite(suite);
173
		}
Ghislain MARY's avatar
Ghislain MARY committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	} 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();
		}
	}

191
	/* Redisplay list of failed tests on end */
192 193 194 195
	if (CU_get_number_of_failure_records()){
		CU_basic_show_failures(CU_get_failure_list());
		printf("\n");
	}
Ghislain MARY's avatar
Ghislain MARY committed
196

jehan's avatar
jehan committed
197 198
	ret=CU_get_number_of_tests_failed()!=0;
	return ret;
Ghislain MARY's avatar
Ghislain MARY committed
199 200
}

201 202
void helper(const char *name) {
	fprintf(stderr, "%s \t--help\n"
203
#ifndef _WIN32
204
		"\t\t\t--verbose\n"
205
#endif
206
		"\t\t\t--silent\n"
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
#if HAVE_CU_GET_SUITE
		"\t\t\t--list-suites\n"
		"\t\t\t--list-tests <suite>\n"
		"\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
		, name);
}

#define CHECK_ARG(argument, index, argc) \
	if (index >= argc) { \
		fprintf(stderr, "Missing argument for \"%s\"\n", argument); \
		return -1; \
	}
Ghislain MARY's avatar
Ghislain MARY committed
224

225
#ifndef WINAPI_FAMILY_PHONE_APP
226 227
#if TARGET_OS_MAC || TARGET_OS_IPHONE
int apple_main (int argc, char *argv[]) {
jehan's avatar
jehan committed
228
#else
Ghislain MARY's avatar
Ghislain MARY committed
229
int main (int argc, char *argv[]) {
jehan's avatar
jehan committed
230
#endif
Ghislain MARY's avatar
Ghislain MARY committed
231
	int i;
232
	int ret;
233 234
	const char *suite_name = NULL;
	const char *test_name = NULL;
Ghislain MARY's avatar
Ghislain MARY committed
235 236
	unsigned char verbose = FALSE;

237 238 239 240 241 242
	/* initialize the CUnit test registry */
	if (CUE_SUCCESS != CU_initialize_registry())
		return CU_get_error();

	mediastreamer2_tester_init();

Ghislain MARY's avatar
Ghislain MARY committed
243 244
	for(i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "--help") == 0) {
245
			helper(argv[0]);
Ghislain MARY's avatar
Ghislain MARY committed
246 247 248
			return 0;
		} else if (strcmp(argv[i], "--verbose") == 0) {
			verbose = TRUE;
249 250
		} else if (strcmp(argv[i], "--silent") == 0) {
			verbose = FALSE;
Ghislain MARY's avatar
Ghislain MARY committed
251 252 253
		}
#if HAVE_CU_GET_SUITE
		else if (strcmp(argv[i], "--test")==0) {
254
			CHECK_ARG("--test", ++i, argc);
Ghislain MARY's avatar
Ghislain MARY committed
255 256
			test_name = argv[i];
		} else if (strcmp(argv[i], "--suite") == 0) {
257
			CHECK_ARG("--suite", ++i, argc);
Ghislain MARY's avatar
Ghislain MARY committed
258
			suite_name = argv[i];
259
		}  else if (strcmp(argv[i],"--list-suites")==0){
260
			list_suites();
261 262 263 264
			return 0;
		} else if (strcmp(argv[i],"--list-tests")==0){
			CHECK_ARG("--list-tests", ++i, argc);
			suite_name = argv[i];
265 266 267 268 269 270 271 272
			if (CU_get_suite(suite_name)==NULL){
				fprintf(stderr, "Could not find suite '%s'. Available suites are:\n", suite_name);
				list_suites();
				return -1;
			}else{
				list_suite_tests(suite_name);
				return 0;
			}
Ghislain MARY's avatar
Ghislain MARY committed
273 274 275 276 277 278 279
		}
#endif
#if HAVE_CU_CURSES
		else if (strcmp(argv[i], "--curses") == 0) {
			curses = 1;
		}
#endif
280
		else {
281 282
			fprintf(stderr, "Unknown option \"%s\"\n", argv[i]); \

283 284 285
			helper(argv[0]);
			return -1;
		}
Ghislain MARY's avatar
Ghislain MARY committed
286 287
	}

288

Ghislain MARY's avatar
Ghislain MARY committed
289
	if (verbose) {
Simon Morlat's avatar
Simon Morlat committed
290
		putenv("MEDIASTREAMER_DEBUG=1");
Ghislain MARY's avatar
Ghislain MARY committed
291
	} else {
292
		putenv("MEDIASTREAMER_DEBUG=0");
Ghislain MARY's avatar
Ghislain MARY committed
293 294
	}

295
	ret = mediastreamer2_tester_run_tests(suite_name, test_name);
296
	CU_cleanup_registry();
297 298
	mediastreamer2_tester_uninit();
	return ret;
Ghislain MARY's avatar
Ghislain MARY committed
299 300
}
#endif