Browse Source

Implements ttail_set_date() + tests

Yann Weber 7 years ago
parent
commit
038730b980
3 changed files with 207 additions and 37 deletions
  1. 10
    0
      src/include/ttail.h
  2. 51
    33
      src/ttail.c
  3. 146
    4
      tests/ttail_init_check.c

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

@@ -63,6 +63,15 @@ int ttail_add_logfile(ttail_t*, const char*);
63 63
  */
64 64
 int ttail_set_prefix(ttail_t*, const char*);
65 65
 
66
+/**@brief Set dates min/max
67
+ *
68
+ *After the call dates are free and set to NULL except if error
69
+ *@param ttail_t ttail instance
70
+ *@param char*[2] dates {min,max} both can be NULL
71
+ *@return -1 if error 0 else
72
+ */
73
+int ttail_set_dates(ttail_t*, char*[2]);
74
+
66 75
 /**@brief Attempt to guess a dateformat
67 76
  *@param ttail_t* ttail if manage to guess set the ttail_t.fmt
68 77
  *@param const char* date as a dtring
@@ -71,6 +80,7 @@ int ttail_set_prefix(ttail_t*, const char*);
71 80
  */
72 81
 int ttail_format_guess(ttail_t*, const char*, struct tm*);
73 82
 
83
+
74 84
 void ttail_free(ttail_t*);
75 85
 
76 86
 

+ 51
- 33
src/ttail.c View File

@@ -113,40 +113,9 @@ for date format");
113 113
 		goto ttail_init_err;
114 114
 	}
115 115
 
116
-	/* date min/max processing */
117
-	for(c=0;c<c;c++)
116
+	if(ttail_set_dates(res, dates) < 0)
118 117
 	{
119
-		if(!dates[c])
120
-		{
121
-			continue;
122
-		}
123
-		if(!(res->flag & TTAIL_FLAG_PREFIX))
124
-		{
125
-			/* no format specified */
126
-			ret = ttail_format_guess(res, dates[c],
127
-				c==0?&(res->date_min):&(res->date_max));
128
-			if(res < 0)
129
-			{
130
-				fprintf(stderr, "Unable to guess format for \
131
-date '%s'\n", dates[c]);
132
-				goto ttail_init_err;
133
-			}
134
-			continue;
135
-		}
136
-		date = strptime(dates[c], res->fmt, 
137
-			c==0?&(res->date_min):&(res->date_max));
138
-		if(!date)
139
-		{
140
-			fprintf(stderr, "Unable to parse date-%s '%s' with \
141
-format '%s'\n", c==0?"min":"max", dates[c], res->fmt);
142
-			goto ttail_init_err;
143
-		}
144
-		else if(*date != '\0')
145
-		{
146
-			fprintf(stderr, "Leading caracters for date-%s : %s\n",\
147
-				c==0?"min":"max", date);
148
-		}
149
-		free(dates[c]);
118
+		goto ttail_init_err;
150 119
 	}
151 120
 
152 121
 	return res;
@@ -262,6 +231,55 @@ error message");
262 231
 	return -1;
263 232
 }
264 233
 
234
+int ttail_set_dates(ttail_t* res, char* dates[2])
235
+{
236
+	int c, ret;
237
+	char *date;
238
+
239
+	for(c=0;c<2;c++)
240
+	{
241
+		memset(c==0?&(res->date_min):&(res->date_max), 0, \
242
+			sizeof(struct tm));
243
+		if(!dates[c])
244
+		{
245
+			continue;
246
+		}
247
+		if(!(res->flag & TTAIL_FLAG_FORMAT))
248
+		{
249
+			/* no format specified */
250
+			ret = ttail_format_guess(res, dates[c],
251
+				c==0?&(res->date_min):&(res->date_max));
252
+			if(res < 0)
253
+			{
254
+				fprintf(stderr, "Unable to guess format for \
255
+date '%s'\n", dates[c]);
256
+				return -1;
257
+			}
258
+			res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
259
+			continue;
260
+		}
261
+		date = strptime(dates[c], res->fmt, 
262
+			c==0?&(res->date_min):&(res->date_max));
263
+		if(!date)
264
+		{
265
+			fprintf(stderr, "Unable to parse date-%s '%s' with \
266
+format '%s'\n", c==0?"min":"max", dates[c], res->fmt);
267
+			return -1;
268
+		}
269
+		else if(*date != '\0')
270
+		{
271
+			fprintf(stderr, "Leading caracters for date-%s : %s\n",\
272
+				c==0?"min":"max", date);
273
+		}
274
+		res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
275
+	}
276
+
277
+	if(dates[0]) { free(dates[0]); dates[0] = NULL; }
278
+	if(dates[1]) { free(dates[1]); dates[1] = NULL; }
279
+
280
+	return 0;
281
+}
282
+
265 283
 int ttail_format_guess(ttail_t* t, const char* date_str, struct tm* tm)
266 284
 {
267 285
 	int i, res;

+ 146
- 4
tests/ttail_init_check.c View File

@@ -288,7 +288,6 @@ END_TEST
288 288
 START_TEST (test_init_guess_fmt_set)
289 289
 {
290 290
 	int ret;
291
-	struct tm tm;
292 291
 	char res[] = "%Y/%m/%d";
293 292
 	ret = ttail_format_guess(ttail, "1988/10/22", NULL);
294 293
 	ck_assert_int_eq(ret, 11);
@@ -299,12 +298,147 @@ START_TEST (test_init_guess_fmt_set)
299 298
 }
300 299
 END_TEST
301 300
 
301
+/*
302
+ * ttail_set_dates() tests
303
+ */
304
+
305
+START_TEST (test_init_setdates_nothing)
306
+{
307
+	int ret;
308
+	char *arg[] = {NULL, NULL};
309
+	struct tm zero;
310
+
311
+	memset(&zero, 0, sizeof(struct tm));
312
+
313
+	ret = ttail_set_dates(ttail, arg);
314
+	ck_assert_int_eq(ret, 0);
315
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == 0);
316
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == 0);
317
+	ck_assert(memcmp(&zero, &(ttail->date_min), sizeof(struct tm)) == 0);
318
+	ck_assert(memcmp(&zero, &(ttail->date_max), sizeof(struct tm)) == 0);
319
+}
320
+END_TEST
321
+
322
+START_TEST (test_init_setdates_nofmt_min)
323
+{
324
+	int ret;
325
+	char *arg[] = {"88/10/22", NULL};
326
+	struct tm zero;
327
+	char *arg0;
328
+
329
+	arg0 = malloc(sizeof(char)*(strlen(arg[0])+1));
330
+	if(!arg0)
331
+	{
332
+		perror("Malloc failed for argument");
333
+		ck_abort_msg("Unable to allocate memory");
334
+	}
335
+	strcpy(arg0, arg[0]);
336
+	arg[0]=arg0;
337
+
338
+	memset(&zero, 0, sizeof(struct tm));
339
+
340
+	ret = ttail_set_dates(ttail, arg);
341
+	ck_assert_int_eq(ret, 0);
342
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == TTAIL_FLAG_DATE_MIN);
343
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == 0);
344
+	ck_assert(memcmp(&zero, &(ttail->date_max), sizeof(struct tm)) == 0);
345
+
346
+	ck_assert_int_eq(ttail->date_min.tm_year, 88);
347
+	ck_assert_int_eq(ttail->date_min.tm_mon, 9);
348
+	ck_assert_int_eq(ttail->date_min.tm_mday, 22);
349
+	ck_assert_int_eq(ttail->date_min.tm_hour, 0);
350
+	ck_assert_int_eq(ttail->date_min.tm_min, 0);
351
+	ck_assert_int_eq(ttail->date_min.tm_sec, 0);
352
+}
353
+END_TEST
354
+
355
+START_TEST (test_init_setdates_nofmt_max)
356
+{
357
+	int ret;
358
+	char *arg[] = {NULL, "88/10/22"};
359
+	struct tm zero;
360
+	char *arg1;
361
+
362
+	arg1 = malloc(sizeof(char)*(strlen(arg[1])+1));
363
+	if(!arg1)
364
+	{
365
+		perror("Malloc failed for argument");
366
+		ck_abort_msg("Unable to allocate memory");
367
+	}
368
+	strcpy(arg1, arg[1]);
369
+	arg[1]=arg1;
370
+
371
+	memset(&zero, 0, sizeof(struct tm));
372
+
373
+	ret = ttail_set_dates(ttail, arg);
374
+	ck_assert_int_eq(ret, 0);
375
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == 0);
376
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == TTAIL_FLAG_DATE_MAX);
377
+	ck_assert(memcmp(&zero, &(ttail->date_min), sizeof(struct tm)) == 0);
378
+
379
+	ck_assert_int_eq(ttail->date_max.tm_year, 88);
380
+	ck_assert_int_eq(ttail->date_max.tm_mon, 9);
381
+	ck_assert_int_eq(ttail->date_max.tm_mday, 22);
382
+	ck_assert_int_eq(ttail->date_max.tm_hour, 0);
383
+	ck_assert_int_eq(ttail->date_max.tm_min, 0);
384
+	ck_assert_int_eq(ttail->date_max.tm_sec, 0);
385
+}
386
+END_TEST
387
+
388
+START_TEST (test_init_setdates_nofmt_both)
389
+{
390
+	int ret;
391
+	char *arg[] = {"1988/10/22", "2088/10/22"};
392
+	struct tm zero;
393
+	char *arg0, *arg1;
394
+
395
+	arg0 = malloc(sizeof(char)*(strlen(arg[0])+1));
396
+	if(!arg0)
397
+	{
398
+		perror("Malloc failed for argument");
399
+		ck_abort_msg("Unable to allocate memory");
400
+	}
401
+	strcpy(arg0, arg[0]);
402
+	arg[0]=arg0;
403
+
404
+	arg1 = malloc(sizeof(char)*(strlen(arg[1])+1));
405
+	if(!arg1)
406
+	{
407
+		perror("Malloc failed for argument");
408
+		ck_abort_msg("Unable to allocate memory");
409
+	}
410
+	strcpy(arg1, arg[1]);
411
+	arg[1]=arg1;
412
+
413
+
414
+	memset(&zero, 0, sizeof(struct tm));
415
+
416
+	ret = ttail_set_dates(ttail, arg);
417
+	ck_assert_int_eq(ret, 0);
418
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MIN) == TTAIL_FLAG_DATE_MIN);
419
+	ck_assert((ttail->flag & TTAIL_FLAG_DATE_MAX) == TTAIL_FLAG_DATE_MAX);
420
+
421
+	ck_assert_int_eq(ttail->date_min.tm_year, 88);
422
+	ck_assert_int_eq(ttail->date_min.tm_mon, 9);
423
+	ck_assert_int_eq(ttail->date_min.tm_mday, 22);
424
+	ck_assert_int_eq(ttail->date_min.tm_hour, 0);
425
+	ck_assert_int_eq(ttail->date_min.tm_min, 0);
426
+	ck_assert_int_eq(ttail->date_min.tm_sec, 0);
427
+
428
+	ck_assert_int_eq(ttail->date_max.tm_year, 188);
429
+	ck_assert_int_eq(ttail->date_max.tm_mon, 9);
430
+	ck_assert_int_eq(ttail->date_max.tm_mday, 22);
431
+	ck_assert_int_eq(ttail->date_max.tm_hour, 0);
432
+	ck_assert_int_eq(ttail->date_max.tm_min, 0);
433
+	ck_assert_int_eq(ttail->date_max.tm_sec, 0);
434
+}
435
+END_TEST
436
+
302 437
 Suite * ttail_init_suite(void)
303 438
 {
304 439
 	Suite *s;
305
-	TCase *tc_init_logfile;
306
-	TCase *tc_init_prefix;
307
-	TCase *tc_init_fmt_guess;
440
+	TCase *tc_init_logfile, *tc_init_prefix ,*tc_init_fmt_guess, \
441
+		*tc_init_set_dates;
308 442
 
309 443
 	s = suite_create("ttail init checks");
310 444
 
@@ -336,6 +470,14 @@ Suite * ttail_init_suite(void)
336 470
 	tcase_add_test(tc_init_fmt_guess, test_init_guess_noguess_notm);
337 471
 	tcase_add_test(tc_init_fmt_guess, test_init_guess_fmt_set);
338 472
 
473
+	tc_init_set_dates = tcase_create("dates min/max init checks");
474
+	tcase_add_checked_fixture(tc_init_set_dates,
475
+		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);
480
+
339 481
 	suite_add_tcase(s, tc_init_logfile);
340 482
 	suite_add_tcase(s, tc_init_prefix);
341 483
 	suite_add_tcase(s, tc_init_fmt_guess);

Loading…
Cancel
Save