Browse Source

Splitting ttail search tests

Yann Weber 7 years ago
parent
commit
22183a4d4b

+ 46
- 0
tests/ttail_check.h View File

@@ -18,6 +18,12 @@ ttail_t *ttail;
18 18
 char *fname[__Fname_sz];
19 19
 /*date formats*/
20 20
 char *fmt[] = TTAIL_DEFAULT_FORMATS;
21
+/* logfiles samples */
22
+char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
23
+	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
24
+off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
25
+FILE *fpl;
26
+
21 27
 
22 28
 void teardown_ttail(void)
23 29
 {
@@ -71,7 +77,47 @@ void teardown_fname(void)
71 77
 	}
72 78
 }
73 79
 
80
+void setup_file_line(void)
81
+{
82
+	fpl = fopen(samples[0], "r");
83
+	if(!fpl)
84
+	{
85
+		perror("Unable to open file for testing :");
86
+		ck_abort();
87
+	}
88
+}
89
+
90
+void teardown_file_line(void)
91
+{
92
+	fclose(fpl);
93
+}
94
+
95
+void teardown_closest(void)
96
+{
97
+
98
+	ttail_free(ttail);
99
+}
100
+
101
+void setup_closest(void)
102
+{
103
+	ttail = ttail_init(1, (char**)&"foo");
104
+}
105
+
106
+void setup_closest_fileinit(void)
107
+{
108
+	size_t i;
109
+	setup_closest();
110
+	for(i=0; i<6; i++)
111
+	{
112
+		ttail_add_logfile(ttail, samples[i]);
113
+	}
114
+}
74 115
 
116
+void teardown_closest_fileinit(void)
117
+{
118
+	_ttail_search_file_free(ttail);
119
+	teardown_closest();
120
+}
75 121
 
76 122
 
77 123
 #define TTAIL_CHECK_MAIN(suite) int main(int argc, char **argv) {\

+ 0
- 480
tests/ttail_search_check.c View File

@@ -1,480 +0,0 @@
1
-#include <check.h>
2
-#include <stdio.h>
3
-#include <libgen.h>
4
-
5
-#include "ttail.h"
6
-#include "ttail_init.h"
7
-#include "ttail_search.h"
8
-
9
-ttail_t *ttail;
10
-
11
-char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
12
-	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
13
-off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
14
-FILE *fpl;
15
-
16
-void setup_file_line(void)
17
-{
18
-	fpl = fopen(samples[0], "r");
19
-	if(!fpl)
20
-	{
21
-		perror("Unable to open file for testing :");
22
-		ck_abort();
23
-	}
24
-}
25
-
26
-void teardown_file_line(void)
27
-{
28
-	fclose(fpl);
29
-}
30
-
31
-void teardown_closest(void)
32
-{
33
-
34
-	ttail_free(ttail);
35
-}
36
-
37
-void setup_closest(void)
38
-{
39
-	ttail = ttail_init(1, (char**)&"foo");
40
-}
41
-
42
-void setup_closest_fileinit(void)
43
-{
44
-	size_t i;
45
-	setup_closest();
46
-	for(i=0; i<6; i++)
47
-	{
48
-		ttail_add_logfile(ttail, samples[i]);
49
-	}
50
-}
51
-
52
-void teardown_closest_fileinit(void)
53
-{
54
-	_ttail_search_file_free(ttail);
55
-	teardown_closest();
56
-}
57
-
58
-START_TEST (test_search_closest_init)
59
-{
60
-	int ret;
61
-
62
-	ret = ttail_search_files_init(ttail);
63
-	ck_assert_int_eq(ret,0);
64
-	ck_assert(ttail->session->file.vpos == 0);
65
-	#ifdef HUGEFILE
66
-	ck_assert_int_eq(ttail->session->file.sz_div,0);
67
-	#endif
68
-}
69
-END_TEST
70
-
71
-START_TEST (test_search_closest_init_filesz)
72
-{
73
-	size_t i;
74
-	ttail_search_files_init(ttail);
75
-	for(i=0;i<6;i++)
76
-	{
77
-		ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
78
-	}
79
-
80
-}
81
-END_TEST
82
-
83
-START_TEST (test_search_closest_init_vfile)
84
-{
85
-	size_t i;
86
-	off_t full_sz;
87
-
88
-	ttail_search_files_init(ttail);
89
-
90
-	full_sz = 0;
91
-	for(i=0;i<6;i++)
92
-	{
93
-		ck_assert(full_sz == ttail->session->file.vfile[i]);
94
-		full_sz += samples_sz[i];
95
-	}
96
-	ck_assert(full_sz == ttail->session->file.vsz);
97
-}
98
-END_TEST
99
-
100
-/*
101
- * _ttail_file_next_line() & _ttail_file_line_start() tests
102
- */
103
-START_TEST (test_file_line_next)
104
-{
105
-	long res;
106
-	res = _ttail_file_next_line(fpl);
107
-	ck_assert(res == 77);
108
-	res = _ttail_file_next_line(fpl);
109
-	ck_assert(res == 136);
110
-	res = _ttail_file_next_line(fpl);
111
-	ck_assert(res == 221);
112
-	res = _ttail_file_next_line(fpl);
113
-	ck_assert(res == 298);
114
-	res = _ttail_file_next_line(fpl);
115
-	ck_assert(res == 357);
116
-	res = _ttail_file_next_line(fpl);
117
-	ck_assert(res == 0);
118
-	res = _ttail_file_next_line(fpl);
119
-	ck_assert(res == -1);
120
-}
121
-END_TEST
122
-
123
-START_TEST (test_file_line_start)
124
-{
125
-	long res;
126
-	fseek(fpl, -1, SEEK_END);
127
-	res = _ttail_file_start_line(fpl);
128
-	ck_assert(res == 357);
129
-	res = _ttail_file_start_line(fpl);
130
-	ck_assert(res == 357);
131
-	fseek(fpl, -1, SEEK_CUR);
132
-	res = _ttail_file_start_line(fpl);
133
-	ck_assert(res == 298);
134
-	fseek(fpl, -1, SEEK_CUR);
135
-	res = _ttail_file_start_line(fpl);
136
-	ck_assert(res == 221);
137
-	fseek(fpl, -1, SEEK_CUR);
138
-	res = _ttail_file_start_line(fpl);
139
-	ck_assert(res == 136);
140
-	fseek(fpl, -1, SEEK_CUR);
141
-	res = _ttail_file_start_line(fpl);
142
-	ck_assert(res == 77);
143
-	fseek(fpl, -1, SEEK_CUR);
144
-	res = _ttail_file_start_line(fpl);
145
-	ck_assert(res == 0);
146
-}
147
-END_TEST
148
-
149
-/*
150
- * _ttail_file_minmax() tests
151
- */
152
-START_TEST (test_file_minmax1)
153
-{
154
-	/**@todo complete the testcase */
155
-	int r;
156
-	struct tm tm[2];
157
-	ttail->flag |= TTAIL_FLAG_PREFIX;
158
-	ttail->prefix_sz = 0;
159
-	ttail_set_fmt(ttail, "%b%n%d %H:%M");
160
-	r = ttail_search_files_init(ttail);
161
-	ck_assert_int_eq(r, 0);
162
-	r = _ttail_file_minmax(ttail, 0, tm);
163
-	ck_assert_int_eq(r, 0);
164
-	ck_assert_int_eq(tm[0].tm_mon, 2);
165
-	ck_assert_int_eq(tm[0].tm_mday, 6);
166
-	ck_assert_int_eq(tm[0].tm_hour, 0);
167
-	ck_assert_int_eq(tm[0].tm_min, 1);
168
-	ck_assert_int_eq(tm[1].tm_mon, 2);
169
-	ck_assert_int_eq(tm[1].tm_mday, 6);
170
-	ck_assert_int_eq(tm[1].tm_hour, 0);
171
-	ck_assert_int_eq(tm[1].tm_min, 29);
172
-}
173
-END_TEST
174
-
175
-/*
176
- * ttail_logline_subst() tests
177
- */
178
-START_TEST (test_search_subst_const1)
179
-{
180
-	char expl[] = "Hello world !";
181
-	const char *res;
182
-	ttail->flag |= TTAIL_FLAG_PREFIX;
183
-	ttail->prefix_sz = 4;
184
-	res = ttail_logline_subst(ttail, expl);
185
-	ck_assert(res != NULL);
186
-	ck_assert(res == expl+4);
187
-}
188
-END_TEST
189
-
190
-START_TEST (test_search_subst_const2)
191
-{
192
-	char expl[] = "Hello world !";
193
-	const char *res;
194
-	ttail->flag |= TTAIL_FLAG_PREFIX;
195
-	ttail->prefix_sz = 0;
196
-	res = ttail_logline_subst(ttail, expl);
197
-	ck_assert(res != NULL);
198
-	ck_assert(res == expl);
199
-}
200
-END_TEST
201
-
202
-START_TEST (test_search_subst_re1)
203
-{
204
-	char expl[] = "1337 Foo Bar - Hello world !";
205
-	char re[] = "^1337 Fo* Bar - ";
206
-	const char *res;
207
-	int ret;
208
-	ret = ttail_set_prefix(ttail, re);
209
-	ck_assert_int_eq(ret,0);
210
-	res = ttail_logline_subst(ttail, expl);
211
-	ck_assert(res != NULL);
212
-	ck_assert_str_eq(res, "Hello world !");
213
-}
214
-END_TEST
215
-
216
-START_TEST (test_search_subst_re2)
217
-{
218
-	char expl[] = "1337 Foo Bar - Hello world !";
219
-	char re[] = "^[0-9]+ Fo{2} Bar - ";
220
-	const char *res;
221
-	int ret;
222
-	ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
223
-	ret = ttail_set_prefix(ttail, re);
224
-	ck_assert_int_eq(ret,0);
225
-	res = ttail_logline_subst(ttail, expl);
226
-	ck_assert(res != NULL);
227
-	ck_assert_str_eq(res, "Hello world !");
228
-}
229
-END_TEST
230
-
231
-START_TEST (test_search_subst_re_nomatch)
232
-{
233
-	char expl[] = "1337 Foo Bar - Hello world !";
234
-	char re[] = "Never match m*";
235
-	const char *res;
236
-	int ret;
237
-	ret = ttail_set_prefix(ttail, re);
238
-	ck_assert_int_eq(ret,0);
239
-	res = ttail_logline_subst(ttail, expl);
240
-	ck_assert(res == NULL);
241
-}
242
-END_TEST
243
-
244
-/*
245
- * ttail_logline2date() checks
246
- */
247
-START_TEST (test_search_log2date1)
248
-{
249
-	char re[] = "^[0-9]+ ";
250
-	char fmt[] = "%Y-%m-%d:%H:%M";
251
-	struct tm tm;
252
-	int ret;
253
-	ttail_set_flag_re_ex(ttail);
254
-	ttail_set_prefix(ttail, re);
255
-	ttail_set_fmt(ttail, fmt);
256
-	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
257
-	ck_assert_int_eq(ret, 0);
258
-	ck_assert_int_eq(tm.tm_year, 88);
259
-	ck_assert_int_eq(tm.tm_mon, 9);
260
-	ck_assert_int_eq(tm.tm_mday, 22);
261
-	ck_assert_int_eq(tm.tm_hour, 22);
262
-	ck_assert_int_eq(tm.tm_min, 10);
263
-}
264
-END_TEST
265
-
266
-START_TEST (test_search_log2date_failpref)
267
-{
268
-	char re[] = "^[0-9]+aa ";
269
-	char fmt[] = "%Y-%m-%d:%H:%M";
270
-	struct tm tm;
271
-	int ret;
272
-	ttail_set_flag_re_ex(ttail);
273
-	ttail_set_prefix(ttail, re);
274
-	ttail_set_fmt(ttail, fmt);
275
-	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
276
-	ck_assert_int_eq(ret, 1);
277
-	ck_assert_int_eq(tm.tm_year, 0);
278
-	ck_assert_int_eq(tm.tm_mon, 0);
279
-	ck_assert_int_eq(tm.tm_mday, 0);
280
-	ck_assert_int_eq(tm.tm_hour, 0);
281
-	ck_assert_int_eq(tm.tm_min, 0);
282
-}
283
-END_TEST
284
-
285
-START_TEST (test_search_log2date_faildate)
286
-{
287
-	char re[] = "^[0-9]+ ";
288
-	char fmt[] = "%y-%m-%d:%H:%M";
289
-	struct tm tm;
290
-	int ret;
291
-	ttail_set_flag_re_ex(ttail);
292
-	ttail_set_prefix(ttail, re);
293
-	ttail_set_fmt(ttail, fmt);
294
-	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
295
-	ck_assert_int_eq(ret, 2);
296
-	ck_assert_int_eq(tm.tm_year, 0);
297
-	ck_assert_int_eq(tm.tm_mon, 0);
298
-	ck_assert_int_eq(tm.tm_mday, 0);
299
-	ck_assert_int_eq(tm.tm_hour, 0);
300
-	ck_assert_int_eq(tm.tm_min, 0);
301
-}
302
-END_TEST
303
-
304
-/*
305
- *	tests ttail_search_closest_files()
306
- */
307
-
308
-START_TEST (test_search_files1)
309
-{
310
-	int ret;
311
-	size_t i;
312
-	struct tm tm;
313
-	memset(&tm, 0, sizeof(tm));
314
-	for(i=1;i<ttail->logfile_sz;i++)
315
-	{
316
-		fclose(ttail->logfile[i]);
317
-		ttail->logfile[i] = NULL;
318
-	}
319
-	ttail->flag |= TTAIL_FLAG_PREFIX;
320
-	ttail->prefix_sz = 0;
321
-	ttail_set_fmt(ttail, "%B%n%d %H:%M");
322
-	memcpy(&(ttail->date_min), &tm, sizeof(tm));
323
-	ttail->flag |= TTAIL_FLAG_DATE_MIN;
324
-
325
-	ret = ttail_search_files_init(ttail);
326
-	ck_assert_int_eq(ret,0);
327
-
328
-	ret = _ttail_search_closest_files(ttail);
329
-	ck_assert_int_eq(ret, 0);
330
-	ck_assert(ttail->session->file.off_min.id == 0);
331
-	ck_assert(ttail->session->file.off_min.off == 0);
332
-}
333
-END_TEST
334
-
335
-START_TEST (test_search_files2)
336
-{
337
-	int ret;
338
-	size_t i;
339
-	struct tm tm;
340
-	memset(&tm, 0, sizeof(tm));
341
-	for(i=1;i<ttail->logfile_sz;i++)
342
-	{
343
-		fclose(ttail->logfile[i]);
344
-		ttail->logfile[i] = NULL;
345
-	}
346
-	ttail->flag |= TTAIL_FLAG_PREFIX;
347
-	ttail->prefix_sz = 0;
348
-	ttail_set_fmt(ttail, "%B%n%d %H:%M");
349
-
350
-	tm.tm_year = -1;
351
-	tm.tm_mon = 2;
352
-	tm.tm_mday = 6;
353
-	tm.tm_hour = 0;
354
-	tm.tm_min = 29;
355
-	tm.tm_sec = -1;
356
-	memcpy(&(ttail->date_min), &tm, sizeof(tm));
357
-	ttail->flag |= TTAIL_FLAG_DATE_MIN;
358
-	
359
-	ret = ttail_search_files_init(ttail);
360
-	ck_assert_int_eq(ret,0);
361
-
362
-	ret = _ttail_search_closest_files(ttail);
363
-	ck_assert_int_eq(ret, 0);
364
-	ck_assert(ttail->session->file.off_min.id == 0);
365
-	ck_assert_int_eq(ttail->session->file.off_min.off, 221);
366
-}
367
-END_TEST
368
-
369
-START_TEST (test_search_files3)
370
-{
371
-	int ret;
372
-	size_t i;
373
-	struct tm tm;
374
-	memset(&tm, 0, sizeof(tm));
375
-	for(i=1;i<ttail->logfile_sz-1;i++)
376
-	{
377
-		fclose(ttail->logfile[i]);
378
-		ttail->logfile[i] = NULL;
379
-	}
380
-	ttail->flag |= TTAIL_FLAG_PREFIX;
381
-	ttail->prefix_sz = 0;
382
-	ttail_set_fmt(ttail, "%B%n%d %H:%M");
383
-
384
-	tm.tm_year = -1;
385
-	tm.tm_mon = 2;
386
-	tm.tm_mday = 6;
387
-	tm.tm_hour = 1;
388
-	tm.tm_min = 0;
389
-	tm.tm_sec = -1;
390
-	memcpy(&(ttail->date_min), &tm, sizeof(tm));
391
-	ttail->flag |= TTAIL_FLAG_DATE_MIN;
392
-
393
-	ret = ttail_search_files_init(ttail);
394
-	ck_assert_int_eq(ret,0);
395
-
396
-	ret = _ttail_search_closest_files(ttail);
397
-	ck_assert_int_eq(ret, 0);
398
-	ck_assert_int_eq(ttail->session->file.off_min.id, 5);
399
-	ck_assert_int_eq(ttail->session->file.off_min.off, 0);
400
-}
401
-END_TEST
402
-
403
-Suite * ttail_search_files_suite(void)
404
-{
405
-	Suite *s;
406
-	TCase *tc_search_closest_fileinit, *tc_file_line,
407
-		*tc_file_minmax;
408
-	TCase *tc_search_subst, *tc_search_logline2date;
409
-	TCase *tc_search_files;
410
-
411
-	s = suite_create("ttail search_files checks");
412
-	tc_search_closest_fileinit = tcase_create("\
413
-ttail_logline_closest_files_init() checks");
414
-	tcase_add_checked_fixture(tc_search_closest_fileinit,
415
-		setup_closest_fileinit, teardown_closest_fileinit);
416
-	tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
417
-	tcase_add_test(tc_search_closest_fileinit,
418
-		test_search_closest_init_filesz);
419
-	tcase_add_test(tc_search_closest_fileinit,
420
-		test_search_closest_init_vfile);
421
-
422
-	tc_file_line = tcase_create("ttail_file_*line*() checks");
423
-	tcase_add_checked_fixture(tc_file_line,
424
-		setup_file_line, teardown_file_line);
425
-	tcase_add_test(tc_file_line, test_file_line_next);
426
-	tcase_add_test(tc_file_line, test_file_line_start);
427
-
428
-	tc_file_minmax = tcase_create("ttail_file_minmax() checks");
429
-	tcase_add_checked_fixture(tc_file_minmax,
430
-		setup_closest_fileinit, teardown_closest_fileinit);
431
-	tcase_add_test(tc_file_minmax, test_file_minmax1);
432
-
433
-	tc_search_subst = tcase_create("ttail_logline_subst() checks");
434
-	tcase_add_checked_fixture(tc_search_subst,
435
-		setup_closest_fileinit, teardown_closest_fileinit);
436
-	tcase_add_test(tc_search_subst, test_search_subst_const1);
437
-	tcase_add_test(tc_search_subst, test_search_subst_const2);
438
-	tcase_add_test(tc_search_subst, test_search_subst_re1);
439
-	tcase_add_test(tc_search_subst, test_search_subst_re2);
440
-	tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
441
-
442
-	tc_search_logline2date = tcase_create("ttail_logline2date() checks");
443
-	tcase_add_checked_fixture(tc_search_logline2date,
444
-		setup_closest_fileinit, teardown_closest_fileinit);
445
-	tcase_add_test(tc_search_subst, test_search_log2date1);
446
-	tcase_add_test(tc_search_subst, test_search_log2date_failpref);
447
-	tcase_add_test(tc_search_subst, test_search_log2date_faildate);
448
-
449
-	tc_search_files = tcase_create("ttail_logline2date() checks");
450
-	tcase_add_checked_fixture(tc_search_files,
451
-		setup_closest_fileinit, teardown_closest_fileinit);
452
-	tcase_add_test(tc_search_files, test_search_files1);
453
-	tcase_add_test(tc_search_files, test_search_files2);
454
-	tcase_add_test(tc_search_files, test_search_files3);
455
-	
456
-
457
-	suite_add_tcase(s, tc_search_closest_fileinit);
458
-	suite_add_tcase(s, tc_file_line);
459
-	suite_add_tcase(s, tc_file_minmax);
460
-	suite_add_tcase(s, tc_search_subst);
461
-	suite_add_tcase(s, tc_search_logline2date);
462
-	suite_add_tcase(s, tc_search_files);
463
-	return s;
464
-}
465
-
466
-int main(int argc, char **argv)
467
-{
468
-	int number_failed = 0;
469
-	SRunner *sr;
470
-
471
-	chdir(dirname(argv[0])); /* move in ./tests dir */
472
-
473
-	sr = srunner_create(ttail_search_files_suite());
474
-	srunner_set_fork_status(sr, CK_FORK);
475
-	srunner_run_all(sr,CK_VERBOSE);
476
-	number_failed = srunner_ntests_failed(sr);
477
-	srunner_free(sr);
478
-	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
479
-
480
-}

+ 73
- 0
tests/ttail_search_closest_fileinit.c View File

@@ -0,0 +1,73 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+#include <libgen.h>
4
+
5
+#include "ttail_check.h"
6
+#include "ttail.h"
7
+#include "ttail_init.h"
8
+#include "ttail_search.h"
9
+
10
+START_TEST (test_search_closest_init)
11
+{
12
+	int ret;
13
+
14
+	ret = ttail_search_files_init(ttail);
15
+	ck_assert_int_eq(ret,0);
16
+	ck_assert(ttail->session->file.vpos == 0);
17
+	#ifdef HUGEFILE
18
+	ck_assert_int_eq(ttail->session->file.sz_div,0);
19
+	#endif
20
+}
21
+END_TEST
22
+
23
+START_TEST (test_search_closest_init_filesz)
24
+{
25
+	size_t i;
26
+	ttail_search_files_init(ttail);
27
+	for(i=0;i<6;i++)
28
+	{
29
+		ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
30
+	}
31
+
32
+}
33
+END_TEST
34
+
35
+START_TEST (test_search_closest_init_vfile)
36
+{
37
+	size_t i;
38
+	off_t full_sz;
39
+
40
+	ttail_search_files_init(ttail);
41
+
42
+	full_sz = 0;
43
+	for(i=0;i<6;i++)
44
+	{
45
+		ck_assert(full_sz == ttail->session->file.vfile[i]);
46
+		full_sz += samples_sz[i];
47
+	}
48
+	ck_assert(full_sz == ttail->session->file.vsz);
49
+}
50
+END_TEST
51
+
52
+Suite * ttail_search_files_suite(void)
53
+{
54
+	Suite *s;
55
+	TCase *tc_search_closest_fileinit;
56
+
57
+	s = suite_create("ttail search_files checks");
58
+
59
+	tc_search_closest_fileinit = tcase_create("\
60
+ttail_logline_closest_files_init() checks");
61
+	tcase_add_checked_fixture(tc_search_closest_fileinit,
62
+		setup_closest_fileinit, teardown_closest_fileinit);
63
+	tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
64
+	tcase_add_test(tc_search_closest_fileinit,
65
+		test_search_closest_init_filesz);
66
+	tcase_add_test(tc_search_closest_fileinit,
67
+		test_search_closest_init_vfile);
68
+
69
+	suite_add_tcase(s, tc_search_closest_fileinit);
70
+	return s;
71
+}
72
+
73
+TTAIL_CHECK_MAIN(ttail_search_files_suite)

+ 76
- 0
tests/ttail_search_file_line.c View File

@@ -0,0 +1,76 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+#include <libgen.h>
4
+
5
+#include "ttail_check.h"
6
+#include "ttail.h"
7
+#include "ttail_init.h"
8
+#include "ttail_search.h"
9
+
10
+/*
11
+ * _ttail_file_next_line() & _ttail_file_line_start() tests
12
+ */
13
+START_TEST (test_file_line_next)
14
+{
15
+	long res;
16
+	res = _ttail_file_next_line(fpl);
17
+	ck_assert(res == 77);
18
+	res = _ttail_file_next_line(fpl);
19
+	ck_assert(res == 136);
20
+	res = _ttail_file_next_line(fpl);
21
+	ck_assert(res == 221);
22
+	res = _ttail_file_next_line(fpl);
23
+	ck_assert(res == 298);
24
+	res = _ttail_file_next_line(fpl);
25
+	ck_assert(res == 357);
26
+	res = _ttail_file_next_line(fpl);
27
+	ck_assert(res == 0);
28
+	res = _ttail_file_next_line(fpl);
29
+	ck_assert(res == -1);
30
+}
31
+END_TEST
32
+
33
+START_TEST (test_file_line_start)
34
+{
35
+	long res;
36
+	fseek(fpl, -1, SEEK_END);
37
+	res = _ttail_file_start_line(fpl);
38
+	ck_assert(res == 357);
39
+	res = _ttail_file_start_line(fpl);
40
+	ck_assert(res == 357);
41
+	fseek(fpl, -1, SEEK_CUR);
42
+	res = _ttail_file_start_line(fpl);
43
+	ck_assert(res == 298);
44
+	fseek(fpl, -1, SEEK_CUR);
45
+	res = _ttail_file_start_line(fpl);
46
+	ck_assert(res == 221);
47
+	fseek(fpl, -1, SEEK_CUR);
48
+	res = _ttail_file_start_line(fpl);
49
+	ck_assert(res == 136);
50
+	fseek(fpl, -1, SEEK_CUR);
51
+	res = _ttail_file_start_line(fpl);
52
+	ck_assert(res == 77);
53
+	fseek(fpl, -1, SEEK_CUR);
54
+	res = _ttail_file_start_line(fpl);
55
+	ck_assert(res == 0);
56
+}
57
+END_TEST
58
+
59
+Suite * ttail_search_files_suite(void)
60
+{
61
+	Suite *s;
62
+	TCase *tc_file_line;
63
+
64
+	s = suite_create("ttail search_files checks");
65
+
66
+	tc_file_line = tcase_create("ttail_file_*line*() checks");
67
+	tcase_add_checked_fixture(tc_file_line,
68
+		setup_file_line, teardown_file_line);
69
+	tcase_add_test(tc_file_line, test_file_line_next);
70
+	tcase_add_test(tc_file_line, test_file_line_start);
71
+
72
+	suite_add_tcase(s, tc_file_line);
73
+	return s;
74
+}
75
+
76
+TTAIL_CHECK_MAIN(ttail_search_files_suite)

+ 127
- 0
tests/ttail_search_files.c View File

@@ -0,0 +1,127 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+#include <libgen.h>
4
+
5
+#include "ttail_check.h"
6
+#include "ttail.h"
7
+#include "ttail_init.h"
8
+#include "ttail_search.h"
9
+
10
+/*
11
+ *	tests ttail_search_closest_files()
12
+ */
13
+
14
+START_TEST (test_search_files1)
15
+{
16
+	int ret;
17
+	size_t i;
18
+	struct tm tm;
19
+	memset(&tm, 0, sizeof(tm));
20
+	for(i=1;i<ttail->logfile_sz;i++)
21
+	{
22
+		fclose(ttail->logfile[i]);
23
+		ttail->logfile[i] = NULL;
24
+	}
25
+	ttail->flag |= TTAIL_FLAG_PREFIX;
26
+	ttail->prefix_sz = 0;
27
+	ttail_set_fmt(ttail, "%B%n%d %H:%M");
28
+	memcpy(&(ttail->date_min), &tm, sizeof(tm));
29
+	ttail->flag |= TTAIL_FLAG_DATE_MIN;
30
+
31
+	ret = ttail_search_files_init(ttail);
32
+	ck_assert_int_eq(ret,0);
33
+
34
+	ret = _ttail_search_closest_files(ttail);
35
+	ck_assert_int_eq(ret, 0);
36
+	ck_assert(ttail->session->file.off_min.id == 0);
37
+	ck_assert(ttail->session->file.off_min.off == 0);
38
+}
39
+END_TEST
40
+
41
+START_TEST (test_search_files2)
42
+{
43
+	int ret;
44
+	size_t i;
45
+	struct tm tm;
46
+	memset(&tm, 0, sizeof(tm));
47
+	for(i=1;i<ttail->logfile_sz;i++)
48
+	{
49
+		fclose(ttail->logfile[i]);
50
+		ttail->logfile[i] = NULL;
51
+	}
52
+	ttail->flag |= TTAIL_FLAG_PREFIX;
53
+	ttail->prefix_sz = 0;
54
+	ttail_set_fmt(ttail, "%B%n%d %H:%M");
55
+
56
+	tm.tm_year = -1;
57
+	tm.tm_mon = 2;
58
+	tm.tm_mday = 6;
59
+	tm.tm_hour = 0;
60
+	tm.tm_min = 29;
61
+	tm.tm_sec = -1;
62
+	memcpy(&(ttail->date_min), &tm, sizeof(tm));
63
+	ttail->flag |= TTAIL_FLAG_DATE_MIN;
64
+	
65
+	ret = ttail_search_files_init(ttail);
66
+	ck_assert_int_eq(ret,0);
67
+
68
+	ret = _ttail_search_closest_files(ttail);
69
+	ck_assert_int_eq(ret, 0);
70
+	ck_assert(ttail->session->file.off_min.id == 0);
71
+	ck_assert_int_eq(ttail->session->file.off_min.off, 221);
72
+}
73
+END_TEST
74
+
75
+START_TEST (test_search_files3)
76
+{
77
+	int ret;
78
+	size_t i;
79
+	struct tm tm;
80
+	memset(&tm, 0, sizeof(tm));
81
+	for(i=1;i<ttail->logfile_sz-1;i++)
82
+	{
83
+		fclose(ttail->logfile[i]);
84
+		ttail->logfile[i] = NULL;
85
+	}
86
+	ttail->flag |= TTAIL_FLAG_PREFIX;
87
+	ttail->prefix_sz = 0;
88
+	ttail_set_fmt(ttail, "%B%n%d %H:%M");
89
+
90
+	tm.tm_year = -1;
91
+	tm.tm_mon = 2;
92
+	tm.tm_mday = 6;
93
+	tm.tm_hour = 1;
94
+	tm.tm_min = 0;
95
+	tm.tm_sec = -1;
96
+	memcpy(&(ttail->date_min), &tm, sizeof(tm));
97
+	ttail->flag |= TTAIL_FLAG_DATE_MIN;
98
+
99
+	ret = ttail_search_files_init(ttail);
100
+	ck_assert_int_eq(ret,0);
101
+
102
+	ret = _ttail_search_closest_files(ttail);
103
+	ck_assert_int_eq(ret, 0);
104
+	ck_assert_int_eq(ttail->session->file.off_min.id, 5);
105
+	ck_assert_int_eq(ttail->session->file.off_min.off, 0);
106
+}
107
+END_TEST
108
+
109
+Suite * ttail_search_files_suite(void)
110
+{
111
+	Suite *s;
112
+	TCase *tc_search_files;
113
+
114
+	s = suite_create("ttail search_files checks");
115
+
116
+	tc_search_files = tcase_create("ttail_logline2date() checks");
117
+	tcase_add_checked_fixture(tc_search_files,
118
+		setup_closest_fileinit, teardown_closest_fileinit);
119
+	tcase_add_test(tc_search_files, test_search_files1);
120
+	tcase_add_test(tc_search_files, test_search_files2);
121
+	tcase_add_test(tc_search_files, test_search_files3);
122
+	
123
+	suite_add_tcase(s, tc_search_files);
124
+	return s;
125
+}
126
+
127
+TTAIL_CHECK_MAIN(ttail_search_files_suite)

+ 88
- 0
tests/ttail_search_logline2date.c View File

@@ -0,0 +1,88 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+#include <libgen.h>
4
+
5
+#include "ttail_check.h"
6
+#include "ttail.h"
7
+#include "ttail_init.h"
8
+#include "ttail_search.h"
9
+
10
+/*
11
+ * ttail_logline2date() checks
12
+ */
13
+START_TEST (test_search_log2date1)
14
+{
15
+	char re[] = "^[0-9]+ ";
16
+	char fmt[] = "%Y-%m-%d:%H:%M";
17
+	struct tm tm;
18
+	int ret;
19
+	ttail_set_flag_re_ex(ttail);
20
+	ttail_set_prefix(ttail, re);
21
+	ttail_set_fmt(ttail, fmt);
22
+	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
23
+	ck_assert_int_eq(ret, 0);
24
+	ck_assert_int_eq(tm.tm_year, 88);
25
+	ck_assert_int_eq(tm.tm_mon, 9);
26
+	ck_assert_int_eq(tm.tm_mday, 22);
27
+	ck_assert_int_eq(tm.tm_hour, 22);
28
+	ck_assert_int_eq(tm.tm_min, 10);
29
+}
30
+END_TEST
31
+
32
+START_TEST (test_search_log2date_failpref)
33
+{
34
+	char re[] = "^[0-9]+aa ";
35
+	char fmt[] = "%Y-%m-%d:%H:%M";
36
+	struct tm tm;
37
+	int ret;
38
+	ttail_set_flag_re_ex(ttail);
39
+	ttail_set_prefix(ttail, re);
40
+	ttail_set_fmt(ttail, fmt);
41
+	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
42
+	ck_assert_int_eq(ret, 1);
43
+	ck_assert_int_eq(tm.tm_year, 0);
44
+	ck_assert_int_eq(tm.tm_mon, 0);
45
+	ck_assert_int_eq(tm.tm_mday, 0);
46
+	ck_assert_int_eq(tm.tm_hour, 0);
47
+	ck_assert_int_eq(tm.tm_min, 0);
48
+}
49
+END_TEST
50
+
51
+START_TEST (test_search_log2date_faildate)
52
+{
53
+	char re[] = "^[0-9]+ ";
54
+	char fmt[] = "%y-%m-%d:%H:%M";
55
+	struct tm tm;
56
+	int ret;
57
+	ttail_set_flag_re_ex(ttail);
58
+	ttail_set_prefix(ttail, re);
59
+	ttail_set_fmt(ttail, fmt);
60
+	ret = ttail_logline2date(ttail, "1337 1988-10-22:22:10 foobar", &tm);
61
+	ck_assert_int_eq(ret, 2);
62
+	ck_assert_int_eq(tm.tm_year, 0);
63
+	ck_assert_int_eq(tm.tm_mon, 0);
64
+	ck_assert_int_eq(tm.tm_mday, 0);
65
+	ck_assert_int_eq(tm.tm_hour, 0);
66
+	ck_assert_int_eq(tm.tm_min, 0);
67
+}
68
+END_TEST
69
+
70
+Suite * ttail_search_files_suite(void)
71
+{
72
+	Suite *s;
73
+	TCase *tc_search_logline2date;
74
+
75
+	s = suite_create("ttail search_files checks");
76
+
77
+	tc_search_logline2date = tcase_create("ttail_logline2date() checks");
78
+	tcase_add_checked_fixture(tc_search_logline2date,
79
+		setup_closest_fileinit, teardown_closest_fileinit);
80
+	tcase_add_test(tc_search_logline2date, test_search_log2date1);
81
+	tcase_add_test(tc_search_logline2date, test_search_log2date_failpref);
82
+	tcase_add_test(tc_search_logline2date, test_search_log2date_faildate);
83
+
84
+	suite_add_tcase(s, tc_search_logline2date);
85
+	return s;
86
+}
87
+
88
+TTAIL_CHECK_MAIN(ttail_search_files_suite)

+ 52
- 0
tests/ttail_search_minmax.c View File

@@ -0,0 +1,52 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+#include <libgen.h>
4
+
5
+#include "ttail_check.h"
6
+#include "ttail.h"
7
+#include "ttail_init.h"
8
+#include "ttail_search.h"
9
+
10
+/*
11
+ * _ttail_file_minmax() tests
12
+ */
13
+START_TEST (test_file_minmax1)
14
+{
15
+	/**@todo complete the testcase */
16
+	int r;
17
+	struct tm tm[2];
18
+	ttail->flag |= TTAIL_FLAG_PREFIX;
19
+	ttail->prefix_sz = 0;
20
+	ttail_set_fmt(ttail, "%b%n%d %H:%M");
21
+	r = ttail_search_files_init(ttail);
22
+	ck_assert_int_eq(r, 0);
23
+	r = _ttail_file_minmax(ttail, 0, tm);
24
+	ck_assert_int_eq(r, 0);
25
+	ck_assert_int_eq(tm[0].tm_mon, 2);
26
+	ck_assert_int_eq(tm[0].tm_mday, 6);
27
+	ck_assert_int_eq(tm[0].tm_hour, 0);
28
+	ck_assert_int_eq(tm[0].tm_min, 1);
29
+	ck_assert_int_eq(tm[1].tm_mon, 2);
30
+	ck_assert_int_eq(tm[1].tm_mday, 6);
31
+	ck_assert_int_eq(tm[1].tm_hour, 0);
32
+	ck_assert_int_eq(tm[1].tm_min, 29);
33
+}
34
+END_TEST
35
+
36
+Suite * ttail_search_files_suite(void)
37
+{
38
+	Suite *s;
39
+	TCase *tc_file_minmax;
40
+
41
+	s = suite_create("ttail search_files checks");
42
+
43
+	tc_file_minmax = tcase_create("ttail_file_minmax() checks");
44
+	tcase_add_checked_fixture(tc_file_minmax,
45
+		setup_closest_fileinit, teardown_closest_fileinit);
46
+	tcase_add_test(tc_file_minmax, test_file_minmax1);
47
+
48
+	suite_add_tcase(s, tc_file_minmax);
49
+	return s;
50
+}
51
+
52
+TTAIL_CHECK_MAIN(ttail_search_files_suite)

+ 99
- 0
tests/ttail_search_subst.c View File

@@ -0,0 +1,99 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+#include <libgen.h>
4
+
5
+#include "ttail_check.h"
6
+#include "ttail.h"
7
+#include "ttail_init.h"
8
+#include "ttail_search.h"
9
+
10
+/*
11
+ * ttail_logline_subst() tests
12
+ */
13
+START_TEST (test_search_subst_const1)
14
+{
15
+	char expl[] = "Hello world !";
16
+	const char *res;
17
+	ttail->flag |= TTAIL_FLAG_PREFIX;
18
+	ttail->prefix_sz = 4;
19
+	res = ttail_logline_subst(ttail, expl);
20
+	ck_assert(res != NULL);
21
+	ck_assert(res == expl+4);
22
+}
23
+END_TEST
24
+
25
+START_TEST (test_search_subst_const2)
26
+{
27
+	char expl[] = "Hello world !";
28
+	const char *res;
29
+	ttail->flag |= TTAIL_FLAG_PREFIX;
30
+	ttail->prefix_sz = 0;
31
+	res = ttail_logline_subst(ttail, expl);
32
+	ck_assert(res != NULL);
33
+	ck_assert(res == expl);
34
+}
35
+END_TEST
36
+
37
+START_TEST (test_search_subst_re1)
38
+{
39
+	char expl[] = "1337 Foo Bar - Hello world !";
40
+	char re[] = "^1337 Fo* Bar - ";
41
+	const char *res;
42
+	int ret;
43
+	ret = ttail_set_prefix(ttail, re);
44
+	ck_assert_int_eq(ret,0);
45
+	res = ttail_logline_subst(ttail, expl);
46
+	ck_assert(res != NULL);
47
+	ck_assert_str_eq(res, "Hello world !");
48
+}
49
+END_TEST
50
+
51
+START_TEST (test_search_subst_re2)
52
+{
53
+	char expl[] = "1337 Foo Bar - Hello world !";
54
+	char re[] = "^[0-9]+ Fo{2} Bar - ";
55
+	const char *res;
56
+	int ret;
57
+	ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
58
+	ret = ttail_set_prefix(ttail, re);
59
+	ck_assert_int_eq(ret,0);
60
+	res = ttail_logline_subst(ttail, expl);
61
+	ck_assert(res != NULL);
62
+	ck_assert_str_eq(res, "Hello world !");
63
+}
64
+END_TEST
65
+
66
+START_TEST (test_search_subst_re_nomatch)
67
+{
68
+	char expl[] = "1337 Foo Bar - Hello world !";
69
+	char re[] = "Never match m*";
70
+	const char *res;
71
+	int ret;
72
+	ret = ttail_set_prefix(ttail, re);
73
+	ck_assert_int_eq(ret,0);
74
+	res = ttail_logline_subst(ttail, expl);
75
+	ck_assert(res == NULL);
76
+}
77
+END_TEST
78
+
79
+Suite * ttail_search_files_suite(void)
80
+{
81
+	Suite *s;
82
+	TCase *tc_search_subst;
83
+
84
+	s = suite_create("ttail search_files checks");
85
+
86
+	tc_search_subst = tcase_create("ttail_logline_subst() checks");
87
+	tcase_add_checked_fixture(tc_search_subst,
88
+		setup_closest_fileinit, teardown_closest_fileinit);
89
+	tcase_add_test(tc_search_subst, test_search_subst_const1);
90
+	tcase_add_test(tc_search_subst, test_search_subst_const2);
91
+	tcase_add_test(tc_search_subst, test_search_subst_re1);
92
+	tcase_add_test(tc_search_subst, test_search_subst_re2);
93
+	tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
94
+
95
+	suite_add_tcase(s, tc_search_subst);
96
+	return s;
97
+}
98
+
99
+TTAIL_CHECK_MAIN(ttail_search_files_suite)

Loading…
Cancel
Save