Browse Source

Fix #16 Write tests for _ttail_set_date_relative()

And bugfixes in _ttail_set_date_relative() bad format detection
Yann Weber 7 years ago
parent
commit
050d81aeb2
2 changed files with 184 additions and 0 deletions
  1. 16
    0
      src/ttail_init.c
  2. 168
    0
      tests/ttail_init_set_date_relative.c

+ 16
- 0
src/ttail_init.c View File

@@ -485,6 +485,12 @@ int _ttail_set_date_relative(ttail_t* res, char* date, int c)
485 485
 	struct tm *tm, *tm_p;
486 486
 	short mod;
487 487
 	
488
+	if(strncmp(date, "#-", 2))
489
+	{
490
+		fprintf(stderr, "'%s' is not a relative date\n", date);
491
+		return -1;
492
+	}
493
+
488 494
 	tm = c==0?&(res->date_min):&(res->date_max);
489 495
 	ttail_tm_init(tm);
490 496
 	if(time(&now) < 0)
@@ -503,6 +509,16 @@ int _ttail_set_date_relative(ttail_t* res, char* date, int c)
503 509
 	mod = 0;
504 510
 	unit = NULL;
505 511
 	value = (int)strtol(date+2, &unit, 10);
512
+	if(unit == date+2)
513
+	{
514
+		fprintf(stderr, "No value found in format '%s'\n", date);
515
+		return -1;
516
+	}
517
+	if(value < 0)
518
+	{
519
+		fprintf(stderr, "Bad relative format '%s'\n", date);
520
+		return -1;
521
+	}
506 522
 	switch(*unit)
507 523
 	{
508 524
 		case 's':

+ 168
- 0
tests/ttail_init_set_date_relative.c View File

@@ -0,0 +1,168 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+#include <libgen.h>
4
+#include <time.h>
5
+
6
+#include "ttail_check.h"
7
+#include "ttail.h"
8
+#include "ttail_init.h"
9
+#include "ttail_search.h"
10
+
11
+struct tm *now;
12
+struct check_relative_date_s
13
+{
14
+	char * f;
15
+	double d;
16
+};
17
+
18
+void setup_date_relative()
19
+{
20
+	time_t t;
21
+	if(time(&t) < 0)
22
+	{
23
+		fprintf(stderr,
24
+			"CHECK FAIL : Unable to retrieve time using time()\n");
25
+		exit(1);
26
+	}
27
+	if(!(now = localtime(&t)))
28
+	{
29
+		fprintf(stderr,
30
+			"CHECK FAIL : Unable to retreive localtime()\n");
31
+		exit(1);
32
+	}
33
+	setup_ttail_empty();
34
+}
35
+
36
+double tm_diff(struct tm *tm1, struct tm *tm2)
37
+{
38
+	time_t t1,t2;
39
+	t1 = mktime(tm1);
40
+	t2 = mktime(tm2);
41
+	return difftime(t1,t2);
42
+}
43
+
44
+START_TEST (wrong_formats)
45
+{
46
+	int r,i;
47
+	char *fmts[] = {	"foobar", "%d", "#-1f", "#-1dour", "#-h",
48
+				"-1h", "#-1h3m",
49
+				"#--1hour", "ab1h", "  1h", "123h"
50
+	};
51
+	for(i=0; i<sizeof(fmts)/sizeof(char*);i++)
52
+	{
53
+		r = _ttail_set_date_relative(ttail, fmts[i], 0);
54
+		ck_assert_printf(r == -1, "Returns %d with '%s' on date 0",
55
+			r, fmts[i]);
56
+		r = _ttail_set_date_relative(ttail, fmts[i], 1);
57
+		ck_assert_printf(r == -1, "Returns %d with '%s' on date 1",
58
+			r, fmts[i]);
59
+	}
60
+}
61
+END_TEST
62
+
63
+/* Test for sec, min, hour, day formats. Comparing with tm_diff */
64
+START_TEST (fmt_sec)
65
+{
66
+	struct check_relative_date_s datas[] = {
67
+		{"#-10s", 10}, {"#-142sec", 142}, {"#-4096s", 4096},
68
+		{"#-1m", 60}, {"#-10min", 600},
69
+		{"#-1h", 3600}, {"#-10h", 36000},
70
+		{"#-1d", 3600*24}, {"#-2day", 3600*24*2},
71
+	};
72
+
73
+	int r, i, c;
74
+	double d;
75
+	struct tm* tm;
76
+	for(i=0; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
77
+	{
78
+		for(c=0;c<2;c++)
79
+		{
80
+			tm = c?&(ttail->date_max):&(ttail->date_min);
81
+			ttail_tm_init(tm);
82
+			r = _ttail_set_date_relative(ttail, datas[i].f, c);
83
+			ck_assert_int_eq(r, 0);
84
+			d = tm_diff(now, tm);
85
+			ck_assert_printf( d == datas[i].d,
86
+				"Failed to set relative date for %s '%s' : \
87
+expected diff was %0.0f but %0.0f was found", c?"date_max":"date_min",
88
+				datas[i].f, datas[i].d, d);
89
+		}
90
+	}
91
+}
92
+END_TEST
93
+
94
+
95
+START_TEST (fmt_mon)
96
+{
97
+	char *fmts[] = { "#-1M", "#-1Month"};
98
+	int r, i, c;
99
+	struct tm* tm;
100
+	for(i=0; i<sizeof(fmts) / sizeof(char*); i++)
101
+	{
102
+		for(c=0; c<2; c++)
103
+		{
104
+			tm = c?&(ttail->date_max):&(ttail->date_min);
105
+			ttail_tm_init(tm);
106
+			r = _ttail_set_date_relative(ttail, "#-1M", c);
107
+			ck_assert_int_eq(r, 0);
108
+			ck_assert_int_eq(tm->tm_sec, now->tm_sec);
109
+			ck_assert_int_eq(tm->tm_min, now->tm_min);
110
+			ck_assert_int_eq(tm->tm_hour, now->tm_hour);
111
+			ck_assert_int_eq(tm->tm_mday, now->tm_mday);
112
+			if(now->tm_mon != 0)
113
+			{
114
+				ck_assert_int_eq(tm->tm_mon,
115
+					now->tm_mon - 1);
116
+				ck_assert_int_eq(tm->tm_year,
117
+					now->tm_year);
118
+			}
119
+			else
120
+			{
121
+				ck_assert_int_eq(tm->tm_mon, 11);
122
+				ck_assert_int_eq(tm->tm_year,
123
+					now->tm_year - 1);
124
+			}
125
+		}
126
+	}
127
+}
128
+END_TEST
129
+
130
+START_TEST (fmt_year)
131
+{
132
+	int r, i, c;
133
+	struct tm *tm;
134
+	struct check_relative_date_s datas[] = {
135
+		{"#-1y", 1} , {"#-10year", 10}};
136
+	for(i=0; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
137
+	{
138
+		for(c=0;c<2;c++)
139
+		{
140
+			tm = c?&(ttail->date_max):&(ttail->date_min);
141
+			ttail_tm_init(tm);
142
+			r = _ttail_set_date_relative(ttail, datas[i].f, c);
143
+			ck_assert_int_eq(r, 0);
144
+			ck_assert_int_eq(tm->tm_sec, now->tm_sec);
145
+			ck_assert_int_eq(tm->tm_min, now->tm_min);
146
+			ck_assert_int_eq(tm->tm_hour, now->tm_hour);
147
+			ck_assert_int_eq(tm->tm_mday, now->tm_mday);
148
+			ck_assert_int_eq(tm->tm_mon, now->tm_mon);
149
+			ck_assert_printf(
150
+				now->tm_year - tm->tm_year == datas[i].d,
151
+				"Failed for %s with format '%s' : found %d \
152
+year of difference instead of %d\n", c?"date_max":"date_min", datas[i].f,
153
+				now->tm_year - tm->tm_year, (int)datas[i].d);
154
+
155
+		}
156
+	}
157
+
158
+}
159
+END_TEST
160
+
161
+TTAIL_CHECK_START("ttail relative date checks", "ttail_set_date_relative() checks")
162
+	TTAIL_SET_FIXTURE(setup_date_relative, teardown_ttail);
163
+	TTAIL_ADD_TEST(wrong_formats);
164
+	TTAIL_ADD_TEST(fmt_sec);
165
+	TTAIL_ADD_TEST(fmt_mon);
166
+	TTAIL_ADD_TEST(fmt_year);
167
+TTAIL_CHECK_END
168
+

Loading…
Cancel
Save