12 static struct lparse_test
*lpt
;
14 static struct lparse_test
{
20 { "logs/no-lf-terminator.log", 1, 0, 1, -1 },
21 { "logs/single-read.log", 1, 0, 1, 0 },
22 { "logs/beta.log", 6114, 0, 2, 0 },
23 { "logs/nuls.log", 20002, 8, 2, 0 },
27 void sighandler(int signum
)
29 if (signum
== SIGALRM
) {
30 fprintf(stderr
, "Failed to complete parsing %s in under %d seconds\n",
31 lpt
->path
, lpt
->max_runtime
);
36 static int lines
, empty
;
37 static unsigned long long bytes
= 0;
38 static int check_line(char *str
, uint len
)
48 #define print_expected(a, b, what) \
49 printf("# expected %llu " what ", got %llu. delta %d\n", \
50 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
53 static const char *green
, *cyan
, *red
, *yellow
, *reset
;
54 static int passed
, failed
, use_alarm
= 1;
56 static void test_one(int rev
, struct lparse_test
*t
, struct stat
*st
)
58 lines
= bytes
= empty
= 0;
60 if (use_alarm
&& t
->max_runtime
)
61 alarm(t
->max_runtime
);
62 lparse_path_real(rev
, t
->path
, st
->st_size
, check_line
);
63 if (use_alarm
&& t
->max_runtime
)
66 if (lines
== t
->lines
&& t
->empty
== empty
) {
67 if (bytes
== st
->st_size
|| st
->st_size
- bytes
== t
->byte_delta
) {
69 printf("%sPASS%s %s\n", green
, reset
, t
->path
);
74 if (lines
== t
->lines
)
75 printf("%spass%s %s\n", yellow
, reset
, t
->path
);
78 printf("%sFAIL%s %s\n", red
, reset
, t
->path
);
80 if (lines
!= t
->lines
)
81 print_expected(t
->lines
, lines
, "lines");
82 if (empty
!= t
->empty
)
83 print_expected(t
->empty
, empty
, "empty lines");
84 if (st
->st_size
!= bytes
&& st
->st_size
- bytes
!= t
->byte_delta
)
85 print_expected(st
->st_size
, bytes
, "bytes");
88 static void test_all(int reverse
, const char *msg
)
92 printf("%s%s%s\n", cyan
, msg
, reset
);
93 for (i
= 0; testfiles
[i
].path
; i
++) {
98 if (stat(lpt
->path
, &st
) < 0) {
99 fprintf(stderr
, "Failed to stat '%s': %s\n", lpt
->path
, strerror(errno
));
103 test_one(reverse
, lpt
, &st
);
107 static struct path_cmp_test
{
111 { "nagios-12-01-2002-00.log", 2002120100 },
112 { "nagios-11-30-2006-01.log", 2006113001 },
113 { "nagios-08-01-2009-00.log", 2009080100 },
114 { "nagios-12-30-2147-99.log", 2147123099 },
115 /* nagios.log is a special case, as is all logfiles without a dash */
116 { "nagios.log", 1 << ((8 * (sizeof(int))) - 1) },
120 static void test_path_cmp(void)
124 printf("%stesting path comparison%s\n", cyan
, reset
);
125 for (i
= 0; testpaths
[i
].path
; i
++) {
126 struct path_cmp_test
*t
;
130 cmp
= path_cmp_number(t
->path
);
131 if (cmp
== t
->correct
) {
132 printf("%sPASS%s %s parses to %u\n", green
, reset
, t
->path
, cmp
);
138 printf("%sFAIL%s %s should be %u, got %u\n",
139 red
, reset
, t
->path
, t
->correct
, cmp
);
143 int main(int argc
, char **argv
)
147 if (isatty(fileno(stdout
))) {
154 green
= red
= yellow
= cyan
= reset
= "";
157 signal(SIGALRM
, sighandler
);
159 for (i
= 1; i
< argc
; i
++) {
160 if (!strcmp(argv
[i
], "--no-alarm"))
164 test_all(0, "testing forward parsing");
165 test_all(1, "testing reverse parsing");