Browse Source

Implemented ttail_init_check() + tests

Yann Weber 7 years ago
parent
commit
ab53a6a6b9
4 changed files with 82 additions and 6 deletions
  1. 6
    0
      src/include/ttail.h
  2. 7
    0
      src/main.c
  3. 20
    1
      src/ttail.c
  4. 49
    5
      tests/ttail_init_check.c

+ 6
- 0
src/include/ttail.h View File

@@ -49,6 +49,12 @@ typedef struct _ttail_s ttail_t;
49 49
  */
50 50
 ttail_t *ttail_init(int argc, char **argv);
51 51
 
52
+/**@brief Checks that init lead to a valid runtime
53
+ *@param ttail_t* t
54
+ *@return 0 if no error -1 if fatal error
55
+ */
56
+int ttail_init_check(ttail_t*);
57
+
52 58
 /**@brief Add a logfile
53 59
  *@param ttail_t*
54 60
  *@param const char * filename

+ 7
- 0
src/main.c View File

@@ -2,5 +2,12 @@
2 2
 
3 3
 int main(int argc, char **argv)
4 4
 {
5
+	ttail_t *ttail;
6
+	ttail = ttail_init(argc, argv);
7
+	if(ttail_init_check(ttail) < 0)
8
+	{
9
+		ttail_free(ttail);
10
+		return 1;
11
+	}
5 12
 	return 0;
6 13
 }

+ 20
- 1
src/ttail.c View File

@@ -128,6 +128,25 @@ for date format");
128 128
 	return NULL;
129 129
 }
130 130
 
131
+int ttail_init_check(ttail_t* t)
132
+{
133
+	if(t->flag & TTAIL_FLAG_DATE_MAX)
134
+	{
135
+		fprintf(stderr, "Warning : date-max not yet implemented\n");
136
+	}
137
+	if(!(t->flag & TTAIL_FLAG_DATE_MIN))
138
+	{
139
+		fprintf(stderr, "--date-min -d is mandatory\n");
140
+		return -1;
141
+	}
142
+	if(!(t->flag & TTAIL_FLAG_FORMAT))
143
+	{
144
+		fprintf(stderr, "No date format set nor detected. Abording.\n");
145
+		return -1;
146
+	}
147
+	return 0;
148
+}
149
+
131 150
 int ttail_add_logfile(ttail_t* res, const char* filename)
132 151
 {
133 152
 	void *tmp;
@@ -249,7 +268,7 @@ int ttail_set_dates(ttail_t* res, char* dates[2])
249 268
 			/* no format specified */
250 269
 			ret = ttail_format_guess(res, dates[c],
251 270
 				c==0?&(res->date_min):&(res->date_max));
252
-			if(res < 0)
271
+			if(ret < 0)
253 272
 			{
254 273
 				fprintf(stderr, "Unable to guess format for \
255 274
 date '%s'\n", dates[c]);

+ 49
- 5
tests/ttail_init_check.c View File

@@ -434,11 +434,46 @@ START_TEST (test_init_setdates_nofmt_both)
434 434
 }
435 435
 END_TEST
436 436
 
437
+START_TEST (test_init_check_bad1)
438
+{
439
+	ck_assert_int_eq(ttail_init_check(ttail), -1);
440
+}
441
+END_TEST
442
+
443
+START_TEST (test_init_check_bad2)
444
+{
445
+	ttail->flag |= TTAIL_FLAG_DATE_MAX;
446
+	ck_assert_int_eq(ttail_init_check(ttail), -1);
447
+}
448
+END_TEST
449
+
450
+START_TEST (test_init_check1)
451
+{
452
+	char *arg[] = {"88/10/22", NULL};
453
+	struct tm zero;
454
+	char *arg0;
455
+
456
+	arg0 = malloc(sizeof(char)*(strlen(arg[0])+1));
457
+	if(!arg0)
458
+	{
459
+		perror("Malloc failed for argument");
460
+		ck_abort_msg("Unable to allocate memory");
461
+	}
462
+	strcpy(arg0, arg[0]);
463
+	arg[0]=arg0;
464
+
465
+	memset(&zero, 0, sizeof(struct tm));
466
+
467
+	ttail_set_dates(ttail, arg);
468
+	ck_assert_int_eq(ttail_init_check(ttail), 0);
469
+}
470
+END_TEST
471
+
437 472
 Suite * ttail_init_suite(void)
438 473
 {
439 474
 	Suite *s;
440 475
 	TCase *tc_init_logfile, *tc_init_prefix ,*tc_init_fmt_guess, \
441
-		*tc_init_set_dates;
476
+		*tc_init_set_dates, *tc_init_check;
442 477
 
443 478
 	s = suite_create("ttail init checks");
444 479
 
@@ -473,14 +508,23 @@ Suite * ttail_init_suite(void)
473 508
 	tc_init_set_dates = tcase_create("dates min/max init checks");
474 509
 	tcase_add_checked_fixture(tc_init_set_dates,
475 510
 		setup_ttail_empty, teardown_ttail);
476
-	tcase_add_test(tc_init_fmt_guess, test_init_setdates_nothing);
477
-	tcase_add_test(tc_init_fmt_guess, test_init_setdates_nofmt_min);
478
-	tcase_add_test(tc_init_fmt_guess, test_init_setdates_nofmt_max);
479
-	tcase_add_test(tc_init_fmt_guess, test_init_setdates_nofmt_both);
511
+	tcase_add_test(tc_init_set_dates, test_init_setdates_nothing);
512
+	tcase_add_test(tc_init_set_dates, test_init_setdates_nofmt_min);
513
+	tcase_add_test(tc_init_set_dates, test_init_setdates_nofmt_max);
514
+	tcase_add_test(tc_init_set_dates, test_init_setdates_nofmt_both);
515
+
516
+	tc_init_check = tcase_create("ttail_init_check() checks");
517
+	tcase_add_checked_fixture(tc_init_check,
518
+		setup_ttail_empty, teardown_ttail);
519
+	tcase_add_test(tc_init_fmt_guess, test_init_check_bad1);
520
+	tcase_add_test(tc_init_fmt_guess, test_init_check_bad2);
521
+	tcase_add_test(tc_init_fmt_guess, test_init_check1);
480 522
 
481 523
 	suite_add_tcase(s, tc_init_logfile);
482 524
 	suite_add_tcase(s, tc_init_prefix);
483 525
 	suite_add_tcase(s, tc_init_fmt_guess);
526
+	suite_add_tcase(s, tc_init_set_dates);
527
+	suite_add_tcase(s, tc_init_check);
484 528
 	return s;
485 529
 }
486 530
 

Loading…
Cancel
Save