1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBPERF_INTERNAL_TESTS_H
3 #define __LIBPERF_INTERNAL_TESTS_H
4 
5 #include <stdio.h>
6 #include <unistd.h>
7 
8 extern int tests_failed;
9 extern int tests_verbose;
10 
get_verbose(char ** argv,int argc)11 static inline int get_verbose(char **argv, int argc)
12 {
13 	int c;
14 	int verbose = 0;
15 
16 	while ((c = getopt(argc, argv, "v")) != -1) {
17 		switch (c)
18 		{
19 		case 'v':
20 			verbose = 1;
21 			break;
22 		default:
23 			break;
24 		}
25 	}
26 	optind = 1;
27 
28 	return verbose;
29 }
30 
31 #define __T_START					\
32 do {							\
33 	tests_verbose = get_verbose(argv, argc);	\
34 	fprintf(stdout, "- running %s...", __FILE__);	\
35 	fflush(NULL);					\
36 	tests_failed = 0;				\
37 } while (0)
38 
39 #define __T_END								\
40 do {									\
41 	if (tests_failed)						\
42 		fprintf(stdout, "  FAILED (%d)\n", tests_failed);	\
43 	else								\
44 		fprintf(stdout, "OK\n");				\
45 } while (0)
46 
47 #define __T(text, cond)                                                          \
48 do {                                                                             \
49 	if (!(cond)) {                                                           \
50 		fprintf(stderr, "FAILED %s:%d %s\n", __FILE__, __LINE__, text);  \
51 		tests_failed++;                                                  \
52 		return -1;                                                       \
53 	}                                                                        \
54 } while (0)
55 
56 #define __T_VERBOSE(...)						\
57 do {									\
58 	if (tests_verbose) {						\
59 		if (tests_verbose == 1) {				\
60 			fputc('\n', stderr);				\
61 			tests_verbose++;				\
62 		}							\
63 		fprintf(stderr, ##__VA_ARGS__);				\
64 	}								\
65 } while (0)
66 
67 #endif /* __LIBPERF_INTERNAL_TESTS_H */
68