浏览代码

Tests + bugfix for ttail_tm_cmp() function

Yann Weber 7 年前
父节点
当前提交
5af21265b0
共有 3 个文件被更改,包括 121 次插入7 次删除
  1. 0
    1
      src/include/ttail_search.h
  2. 6
    6
      src/ttail_search.c
  3. 115
    0
      tests/ttail_search_tm_cmp.c

+ 0
- 1
src/include/ttail_search.h 查看文件

@@ -104,7 +104,6 @@ const char* ttail_logline_subst(ttail_t*, const char*);
104 104
  *@param a struct tm*
105 105
  *@param b struct tm*
106 106
  *@return <0 if a<b, >0 if a>b and 0 if a == b
107
- *@todo checks
108 107
  */
109 108
 int ttail_tm_cmp(const struct tm*, const struct tm*);
110 109
 

+ 6
- 6
src/ttail_search.c 查看文件

@@ -127,32 +127,32 @@ int ttail_tm_cmp(const struct tm *ta, const struct tm *tb)
127 127
 {
128 128
 	int r;
129 129
 	r=0;
130
-	if(ta->tm_year >= 0 && ta->tm_year >= 0)
130
+	if(ta->tm_year >= 0 && tb->tm_year >= 0)
131 131
 	{
132 132
 		r = ta->tm_year - tb->tm_year;
133 133
 		if(r) { return r; }
134 134
 	}
135
-	if(ta->tm_mon >= 0 && ta->tm_mon >= 0)
135
+	if(ta->tm_mon >= 0 && tb->tm_mon >= 0)
136 136
 	{
137 137
 		r = ta->tm_mon - tb->tm_mon;
138 138
 		if(r) { return r; }
139 139
 	}
140
-	if(ta->tm_mday >= 0 && ta->tm_mday >= 0)
140
+	if(ta->tm_mday >= 0 && tb->tm_mday >= 0)
141 141
 	{
142 142
 		r = ta->tm_mday - tb->tm_mday;
143 143
 		if(r) { return r; }
144 144
 	}
145
-	if(ta->tm_hour >= 0 && ta->tm_hour >= 0)
145
+	if(ta->tm_hour >= 0 && tb->tm_hour >= 0)
146 146
 	{
147 147
 		r = ta->tm_hour - tb->tm_hour;
148 148
 		if(r) { return r; }
149 149
 	}
150
-	if(ta->tm_min >= 0 && ta->tm_min >= 0)
150
+	if(ta->tm_min >= 0 && tb->tm_min >= 0)
151 151
 	{
152 152
 		r = ta->tm_min - tb->tm_min;
153 153
 		if(r) { return r; }
154 154
 	}
155
-	if(ta->tm_sec >= 0 && ta->tm_sec >= 0)
155
+	if(ta->tm_sec >= 0 && tb->tm_sec >= 0)
156 156
 	{
157 157
 		r = ta->tm_sec - tb->tm_sec;
158 158
 	}

+ 115
- 0
tests/ttail_search_tm_cmp.c 查看文件

@@ -0,0 +1,115 @@
1
+#include <check.h>
2
+#include <stdio.h>
3
+
4
+#include "ttail_check.h"
5
+#include "ttail.h"
6
+#include "ttail_init.h"
7
+#include "ttail_search.h"
8
+
9
+#define TTAIL_TM_CMP_INIT \
10
+struct tm a, b;\
11
+int ret;\
12
+ttail_tm_init(&a);\
13
+ttail_tm_init(&b);\
14
+
15
+START_TEST (test_tm_cmp_init)
16
+{
17
+	TTAIL_TM_CMP_INIT
18
+	ret = ttail_tm_cmp(&a, &b);
19
+	ck_assert_int_eq(ret, 0);
20
+}
21
+END_TEST
22
+
23
+START_TEST (test_tm_cmp_field_priority)
24
+{
25
+	TTAIL_TM_CMP_INIT
26
+	a.tm_sec = 42;
27
+	b.tm_sec = 41;
28
+	ret = ttail_tm_cmp(&a, &b);
29
+	ck_assert_int_gt(ret, 0);
30
+	a.tm_min = 41;
31
+	b.tm_min = 42;
32
+	ret = ttail_tm_cmp(&a, &b);
33
+	ck_assert_int_lt(ret, 0);
34
+	a.tm_hour = 42;
35
+	b.tm_hour = 41;
36
+	ret = ttail_tm_cmp(&a, &b);
37
+	ck_assert_int_gt(ret, 0);
38
+	a.tm_mday = 21;
39
+	b.tm_mday = 22;
40
+	ret = ttail_tm_cmp(&a, &b);
41
+	ck_assert_int_lt(ret, 0);
42
+	a.tm_mon = 10;
43
+	b.tm_mon = 9;
44
+	ret = ttail_tm_cmp(&a, &b);
45
+	ck_assert_int_gt(ret, 0);
46
+	a.tm_year = 14;
47
+	b.tm_year = 15;
48
+	ret = ttail_tm_cmp(&a, &b);
49
+	ck_assert_int_lt(ret, 0);
50
+}
51
+END_TEST
52
+
53
+START_TEST (test_tm_cmp_empty_fields1)
54
+{
55
+	TTAIL_TM_CMP_INIT
56
+	a.tm_sec = 42;
57
+	b.tm_min = 41;
58
+	a.tm_hour = 1;
59
+	b.tm_hour = 1;
60
+	ret = ttail_tm_cmp(&a, &b);
61
+	ck_assert_int_eq(ret, 0);
62
+}
63
+END_TEST
64
+
65
+START_TEST (test_tm_cmp_empty_fields2)
66
+{
67
+	TTAIL_TM_CMP_INIT
68
+	a.tm_sec = 42;
69
+	b.tm_min = 41;
70
+	b.tm_hour = 1;
71
+	b.tm_mday = 6;
72
+	b.tm_mon = 7;
73
+	b.tm_year = 15;
74
+	ret = ttail_tm_cmp(&a, &b);
75
+	ck_assert_int_eq(ret, 0);
76
+}
77
+END_TEST
78
+
79
+START_TEST (test_tm_cmp_empty_fields3)
80
+{
81
+	TTAIL_TM_CMP_INIT
82
+	a.tm_sec = 42;
83
+	b.tm_sec = 55;
84
+	b.tm_min = 41;
85
+	b.tm_hour = 1;
86
+	b.tm_mday = 6;
87
+	b.tm_mon = 7;
88
+	b.tm_year = 15;
89
+	ret = ttail_tm_cmp(&a, &b);
90
+	ck_assert_int_lt(ret, 0);
91
+}
92
+END_TEST
93
+
94
+START_TEST (test_tm_cmp_empty_fields4)
95
+{
96
+	TTAIL_TM_CMP_INIT
97
+	b.tm_sec = 42;
98
+	a.tm_min = 41;
99
+	a.tm_hour = 1;
100
+	a.tm_mday = 6;
101
+	a.tm_mon = 7;
102
+	a.tm_year = 15;
103
+	ret = ttail_tm_cmp(&a, &b);
104
+	ck_assert_int_eq(ret, 0);
105
+}
106
+END_TEST
107
+
108
+TTAIL_CHECK_START("ttail search functions checks", "ttail_tm_cmp() checks")
109
+	TTAIL_ADD_TEST(test_tm_cmp_init);
110
+	TTAIL_ADD_TEST(test_tm_cmp_field_priority);
111
+	TTAIL_ADD_TEST(test_tm_cmp_empty_fields1);
112
+	TTAIL_ADD_TEST(test_tm_cmp_empty_fields2);
113
+	TTAIL_ADD_TEST(test_tm_cmp_empty_fields3);
114
+	TTAIL_ADD_TEST(test_tm_cmp_empty_fields4);
115
+TTAIL_CHECK_END

正在加载...
取消
保存