12 static struct lparse_test
*lpt
;
14 static struct lparse_test
{
19 { "logs/no-lf-terminator.log", 1, 0, 1 },
20 { "logs/single-read.log", 1, 0, 1 },
21 { "logs/beta.log", 6114, 0, 2 },
22 { "logs/nuls.log", 20002, 8, 2 },
26 void sighandler(int signum
)
28 if (signum
== SIGALRM
) {
29 fprintf(stderr
, "Failed to complete parsing %s in under %d seconds\n",
30 lpt
->path
, lpt
->max_runtime
);
35 static int lines
, empty
;
36 static unsigned long long bytes
= 0;
37 static int check_line(char *str
, uint len
)
47 #define print_expected(a, b, what) \
48 printf("# expected %llu " what ", got %llu. delta %d\n", \
49 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
52 static const char *green
, *cyan
, *red
, *yellow
, *reset
;
53 static int passed
, failed
, use_alarm
= 1;
55 static void test_one(int rev
, struct lparse_test
*t
, struct stat
*st
)
57 if (use_alarm
&& t
->max_runtime
)
58 alarm(t
->max_runtime
);
59 lparse_path_real(rev
, t
->path
, st
->st_size
, check_line
);
60 if (use_alarm
&& t
->max_runtime
)
63 if (lines
== t
->lines
&& t
->empty
== empty
&& bytes
== st
->st_size
) {
65 printf("%sPASS%s %s\n", green
, reset
, t
->path
);
67 if (lines
== t
->lines
)
68 printf("%spass%s %s\n", yellow
, reset
, t
->path
);
71 printf("%sFAIL%s %s\n", red
, reset
, t
->path
);
73 if (lines
!= t
->lines
)
74 print_expected(t
->lines
, lines
, "lines");
75 if (empty
!= t
->empty
)
76 print_expected(t
->empty
, empty
, "empty lines");
77 if (st
->st_size
!= bytes
)
78 print_expected(st
->st_size
, bytes
, "bytes");
81 lines
= bytes
= empty
= 0;
84 static void test_all(int reverse
, const char *msg
)
88 printf("%s%s%s\n", cyan
, msg
, reset
);
89 for (i
= 0; testfiles
[i
].path
; i
++) {
94 if (stat(lpt
->path
, &st
) < 0) {
95 fprintf(stderr
, "Failed to stat '%s': %s\n", lpt
->path
, strerror(errno
));
99 test_one(reverse
, lpt
, &st
);
103 static struct path_cmp_test
{
107 { "nagios-12-01-2002-00.log", 2002120100 },
108 { "nagios-11-30-2006-01.log", 2006113001 },
109 { "nagios-08-01-2009-00.log", 2009080100 },
110 { "nagios-12-30-2147-99.log", 2147123099 },
111 /* nagios.log is a special case */
112 { "nagios.log", 1 << ((8 * (sizeof(int))) - 1) },
115 static void test_path_cmp(void)
119 printf("%stesting path comparison%s\n", cyan
, reset
);
120 for (i
= 0; testpaths
[i
].path
; i
++) {
121 struct path_cmp_test
*t
;
125 cmp
= path_cmp_number(t
->path
);
126 if (cmp
== t
->correct
) {
127 printf("%sPASS%s %s parses to %u\n", green
, reset
, t
->path
, cmp
);
133 printf("%sFAIL%s %s should be %u, got %u\n",
134 red
, reset
, t
->path
, t
->correct
, cmp
);
138 int main(int argc
, char **argv
)
142 if (isatty(fileno(stdout
))) {
149 green
= red
= yellow
= cyan
= reset
= "";
152 signal(SIGALRM
, sighandler
);
154 for (i
= 1; i
< argc
; i
++) {
155 if (!strcmp(argv
[i
], "--no-alarm"))
159 test_all(0, "testing forward parsing");
160 test_all(1, "testing reverse parsing");