11 #define CLR_RESET "\033[m"
12 #define CLR_BOLD "\033[1m"
13 #define CLR_RED "\033[31m"
14 #define CLR_GREEN "\033[32m"
15 #define CLR_BROWN "\033[33m"
16 #define CLR_YELLOW "\033[33m\033[1m"
17 #define CLR_BLUE "\033[34m"
18 #define CLR_MAGENTA "\033[35m"
19 #define CLR_CYAN "\033[36m"
20 #define CLR_BG_RED "\033[41m"
22 static struct lparse_test
{
27 { "logs/no-lf-terminator.log", 1, 0, 1 },
28 { "logs/single-read.log", 1, 0, 1 },
29 { "logs/beta.log", 6114, 0, 2 },
30 { "logs/nuls.log", 20002, 8, 2 },
34 static struct lparse_test
*t
;
36 void sighandler(int signum
)
38 if (signum
== SIGALRM
) {
39 fprintf(stderr
, "Failed to complete parsing %s in under %d seconds\n",
40 t
->path
, t
->max_runtime
);
45 static int lines
, empty
;
46 static unsigned long long bytes
= 0;
47 static int check_line(char *str
, uint len
)
57 #define print_expected(a, b, what) \
58 printf("# expected %llu " what ", got %llu. delta %d\n", \
59 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
62 static const char *green
, *cyan
, *red
, *yellow
, *reset
;
63 static int passed
, failed
, use_alarm
= 1;
65 static void test_one(int rev
, struct lparse_test
*t
, struct stat
*st
)
67 if (use_alarm
&& t
->max_runtime
)
68 alarm(t
->max_runtime
);
69 lparse_path_real(rev
, t
->path
, st
->st_size
, check_line
);
70 if (use_alarm
&& t
->max_runtime
)
73 if (lines
== t
->lines
&& t
->empty
== empty
&& bytes
== st
->st_size
) {
75 printf("%sPASS%s %s\n", green
, reset
, t
->path
);
77 if (lines
== t
->lines
)
78 printf("%spass%s %s\n", yellow
, reset
, t
->path
);
81 printf("%sFAIL%s %s\n", red
, reset
, t
->path
);
83 if (lines
!= t
->lines
)
84 print_expected(t
->lines
, lines
, "lines");
85 if (empty
!= t
->empty
)
86 print_expected(t
->empty
, empty
, "empty lines");
87 if (st
->st_size
!= bytes
)
88 print_expected(st
->st_size
, bytes
, "bytes");
91 lines
= bytes
= empty
= 0;
94 static void test_all(int reverse
, const char *msg
)
98 printf("%s%s%s\n", cyan
, msg
, reset
);
99 for (i
= 0; testfiles
[i
].path
; i
++) {
104 if (stat(testfiles
[i
].path
, &st
) < 0) {
105 fprintf(stderr
, "Failed to stat '%s': %s\n", t
->path
, strerror(errno
));
109 test_one(reverse
, t
, &st
);
113 int main(int argc
, char **argv
)
117 if (isatty(fileno(stdout
))) {
124 green
= red
= yellow
= cyan
= reset
= "";
127 signal(SIGALRM
, sighandler
);
129 for (i
= 1; i
< argc
; i
++) {
130 if (!strcmp(argv
[i
], "--no-alarm"))
134 test_all(0, "testing forward parsing");
135 test_all(1, "testing reverse parsing");