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 
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 	return verbose;
27 }
28 
29 #define __T_START					\
30 do {							\
31 	tests_verbose = get_verbose(argv, argc);	\
32 	fprintf(stdout, "- running %s...", __FILE__);	\
33 	fflush(NULL);					\
34 	tests_failed = 0;				\
35 } while (0)
36 
37 #define __T_END								\
38 do {									\
39 	if (tests_failed)						\
40 		fprintf(stdout, "  FAILED (%d)\n", tests_failed);	\
41 	else								\
42 		fprintf(stdout, "OK\n");				\
43 } while (0)
44 
45 #define __T(text, cond)                                                          \
46 do {                                                                             \
47 	if (!(cond)) {                                                           \
48 		fprintf(stderr, "FAILED %s:%d %s\n", __FILE__, __LINE__, text);  \
49 		tests_failed++;                                                  \
50 		return -1;                                                       \
51 	}                                                                        \
52 } while (0)
53 
54 #define __T_VERBOSE(...)						\
55 do {									\
56 	if (tests_verbose) {						\
57 		if (tests_verbose == 1) {				\
58 			fputc('\n', stderr);				\
59 			tests_verbose++;				\
60 		}							\
61 		fprintf(stderr, ##__VA_ARGS__);				\
62 	}								\
63 } while (0)
64 
65 #endif /* __LIBPERF_INTERNAL_TESTS_H */
66