Browse Source

Tests + bugfix

Testing ttail_search_closest_files() on multiple files
Yann Weber 7 years ago
parent
commit
9f5cf21919
2 changed files with 63 additions and 18 deletions
  1. 27
    12
      src/ttail_search_files.c
  2. 36
    6
      tests/ttail_search_check.c

+ 27
- 12
src/ttail_search_files.c View File

@@ -5,6 +5,7 @@ int _ttail_search_closest_files(ttail_t* t, const struct tm *tm)
5 5
 	int ret;
6 6
 	size_t i, prev;
7 7
 	struct tm **ftm; /* t->logfile_sz len of struct tm[2] */
8
+	char prev_found;
8 9
 	ret = _ttail_search_closest_files_init(t);
9 10
 	if(ret)
10 11
 	{
@@ -18,19 +19,22 @@ int _ttail_search_closest_files(ttail_t* t, const struct tm *tm)
18 19
 		perror("Unable to allocate memory");
19 20
 		goto _ttail_search_closest_files_alloc_err;
20 21
 	}
22
+	prev_found = 0;
23
+	prev = 0;
21 24
 	for(i=0; i<t->logfile_sz; i++)
22 25
 	{
26
+		if(!t->logfile[i])
27
+		{
28
+			ftm[i] = NULL;
29
+			continue;
30
+		}
23 31
 		ftm[i] = malloc(sizeof(struct tm)*2);
24 32
 		if(!ftm[i])
25 33
 		{
26 34
 			perror("Unable to allocate memory for time");
27 35
 			goto _ttail_search_closest_files_alloc_loop_err;
28 36
 		}
29
-		if(!t->logfile[i])
30
-		{
31
-			continue;
32
-		}
33
-		ret = _ttail_file_minmax(t, 0, ftm[i]);
37
+		ret = _ttail_file_minmax(t, i, ftm[i]);
34 38
 		if(ret < 0)
35 39
 		{
36 40
 			fprintf(stderr, "Minmax error\n");
@@ -47,9 +51,9 @@ a valid date in '%s'\n", t->logfile_name[i]);
47 51
 			ftm[i] = NULL;
48 52
 			fclose(t->logfile[i]);
49 53
 			t->logfile[i] = NULL;
54
+			continue;
50 55
 		}
51
-		prev = i;
52
-		if(i && prev &&
56
+		if(i && prev_found &&
53 57
 			ttail_tm_cmp(&(ftm[prev][1]), &(ftm[i][0])) > 0)
54 58
 		{
55 59
 			/* files are not sorted */
@@ -57,6 +61,8 @@ a valid date in '%s'\n", t->logfile_name[i]);
57 61
 File sorting not implemented yet\n");
58 62
 			goto _ttail_search_closest_files_loop_err;
59 63
 		}
64
+		prev_found = 1;
65
+		prev = i;
60 66
 	}
61 67
 	/* TODO begining binary search of date_min */
62 68
 	ret = _ttail_search_files_binary_search(t, tm, (const struct tm**)ftm);
@@ -66,8 +72,8 @@ File sorting not implemented yet\n");
66 72
 	}
67 73
 	free(ftm);
68 74
 	return ret;
69
-	goto _ttail_search_closest_files_err;
70 75
 
76
+	goto _ttail_search_closest_files_err;
71 77
 	_ttail_search_closest_files_err:
72 78
 	i = t->logfile_sz;
73 79
 	do
@@ -115,7 +121,7 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
115 121
 			/* found at the begining of the file */
116 122
 			return 0;
117 123
 		}
118
-		else if (cmax <= 0)
124
+		else if (cmax == 0)
119 125
 		{
120 126
 			/* found at EOF */
121 127
 			*off = _ttail_from_search_from_end(t, *id, in);
@@ -128,9 +134,14 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
128 134
 		}
129 135
 		else if(cmin > 0)
130 136
 		{
131
-			/* not found */
132
-			*id=0;
133
-			return 1;
137
+			if(!*id)
138
+			{
139
+				/* not found */
140
+				return 1;
141
+			}
142
+			/* found at start of file */
143
+			off = 0;
144
+			return 0;
134 145
 		}
135 146
 		else if(*id == t->logfile_sz - 1 ||
136 147
 			cmax > 0)
@@ -529,6 +540,10 @@ inline off_t _ttail_from_search_from_end(ttail_t* t , size_t id ,
529 540
 			}
530 541
 			else
531 542
 			{
543
+				if(cmpret < 0 && result < 0)
544
+				{
545
+					result = last;
546
+				}
532 547
 				break;
533 548
 			}
534 549
 		}

+ 36
- 6
tests/ttail_search_check.c View File

@@ -8,9 +8,9 @@
8 8
 
9 9
 ttail_t *ttail;
10 10
 
11
-char *samples[5] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
12
-	"./samples/4.log", "./samples/5.log"};
13
-off_t samples_sz[5] = { 442, 0, 893, 2587, 2310 };
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 14
 FILE *fpl;
15 15
 
16 16
 void setup_file_line(void)
@@ -43,7 +43,7 @@ void setup_closest_fileinit(void)
43 43
 {
44 44
 	size_t i;
45 45
 	setup_closest();
46
-	for(i=0; i<5; i++)
46
+	for(i=0; i<6; i++)
47 47
 	{
48 48
 		ttail_add_logfile(ttail, samples[i]);
49 49
 	}
@@ -72,7 +72,7 @@ START_TEST (test_search_closest_init_filesz)
72 72
 {
73 73
 	size_t i;
74 74
 	_ttail_search_closest_files_init(ttail);
75
-	for(i=0;i<5;i++)
75
+	for(i=0;i<6;i++)
76 76
 	{
77 77
 		ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
78 78
 	}
@@ -88,7 +88,7 @@ START_TEST (test_search_closest_init_vfile)
88 88
 	_ttail_search_closest_files_init(ttail);
89 89
 
90 90
 	full_sz = 0;
91
-	for(i=0;i<5;i++)
91
+	for(i=0;i<6;i++)
92 92
 	{
93 93
 		ck_assert(full_sz == ttail->session->file.vfile[i]);
94 94
 		full_sz += samples_sz[i];
@@ -356,6 +356,35 @@ START_TEST (test_search_files2)
356 356
 }
357 357
 END_TEST
358 358
 
359
+START_TEST (test_search_files3)
360
+{
361
+	int ret;
362
+	size_t i;
363
+	struct tm tm;
364
+	memset(&tm, 0, sizeof(tm));
365
+	for(i=1;i<ttail->logfile_sz-1;i++)
366
+	{
367
+		fclose(ttail->logfile[i]);
368
+		ttail->logfile[i] = NULL;
369
+	}
370
+	ttail->flag |= TTAIL_FLAG_PREFIX;
371
+	ttail->prefix_sz = 0;
372
+	ttail_set_fmt(ttail, "%B%n%d %H:%M");
373
+
374
+	tm.tm_year = -1;
375
+	tm.tm_mon = 2;
376
+	tm.tm_mday = 6;
377
+	tm.tm_hour = 1;
378
+	tm.tm_min = 0;
379
+	tm.tm_sec = -1;
380
+
381
+	ret = _ttail_search_closest_files(ttail, &tm);
382
+	ck_assert_int_eq(ret, 0);
383
+	ck_assert_int_eq(ttail->session->file.id, 5);
384
+	ck_assert_int_eq(ttail->session->file.off, 0);
385
+}
386
+END_TEST
387
+
359 388
 Suite * ttail_search_files_suite(void)
360 389
 {
361 390
 	Suite *s;
@@ -407,6 +436,7 @@ ttail_logline_closest_files_init() checks");
407 436
 		setup_closest_fileinit, teardown_closest_fileinit);
408 437
 	tcase_add_test(tc_search_files, test_search_files1);
409 438
 	tcase_add_test(tc_search_files, test_search_files2);
439
+	tcase_add_test(tc_search_files, test_search_files3);
410 440
 	
411 441
 
412 442
 	suite_add_tcase(s, tc_search_closest_fileinit);

Loading…
Cancel
Save