1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/* SPDX-License-Identifier: GPL-2.0-only */
#include <inttypes.h>
#include <rtc.h>
#include <string.h>
#include <tests/test.h>
static void test_rtc_to_tm_from_unix_time(void **state)
{
struct rtc_time tm;
int tim;
/* Zero-day */
tim = 0;
assert_int_equal(0, rtc_to_tm(tim, &tm));
assert_int_equal(1970, tm.year);
assert_int_equal(1, tm.mon);
assert_int_equal(1, tm.mday);
assert_int_equal(0, tm.hour);
assert_int_equal(0, tm.min);
assert_int_equal(0, tm.sec);
assert_int_equal(4, tm.wday); /* Thursday */
/* One second from time base */
tim = 1;
assert_int_equal(0, rtc_to_tm(tim, &tm));
assert_int_equal(1970, tm.year);
assert_int_equal(1, tm.mon);
assert_int_equal(1, tm.mday);
assert_int_equal(0, tm.hour);
assert_int_equal(0, tm.min);
assert_int_equal(1, tm.sec);
assert_int_equal(4, tm.wday); /* Thursday */
/* Full time value */
tim = INT32_MAX;
assert_int_equal(0, rtc_to_tm(tim, &tm));
assert_int_equal(2038, tm.year);
assert_int_equal(1, tm.mon);
assert_int_equal(19, tm.mday);
assert_int_equal(3, tm.hour);
assert_int_equal(14, tm.min);
assert_int_equal(7, tm.sec);
assert_int_equal(2, tm.wday); /* Tuesday */
/* Other common value */
tim = 1618484725;
assert_int_equal(0, rtc_to_tm(tim, &tm));
assert_int_equal(2021, tm.year);
assert_int_equal(4, tm.mon);
assert_int_equal(15, tm.mday);
assert_int_equal(11, tm.hour);
assert_int_equal(5, tm.min);
assert_int_equal(25, tm.sec);
assert_int_equal(4, tm.wday); /* Thursday */
/* Negative value - expect incorrect output */
tim = -1;
assert_int_equal(0, rtc_to_tm(tim, &tm));
assert_int_equal(1970, tm.year);
assert_int_equal(1, tm.mon);
assert_int_equal(1, tm.mday);
assert_int_equal(0, tm.hour);
assert_int_equal(0, tm.min);
assert_int_equal(-1, tm.sec);
assert_int_equal(4, tm.wday); /* Thursday */
}
static void test_mktime(void **state)
{
struct rtc_time tm;
struct rtc_time tm2;
memset(&tm, 0, sizeof(tm));
memset(&tm2, 0, sizeof(tm2));
/* Epoch start */
tm = (struct rtc_time){
.year = 1970, .mon = 1, .mday = 1, .hour = 0, .min = 0, .sec = 0,
};
assert_int_equal(0, rtc_mktime(&tm));
/* Last correct value */
tm = (struct rtc_time){
.year = 2038, .mon = 1, .mday = 19, .hour = 3, .min = 14, .sec = 7,
};
assert_int_equal(INT32_MAX, rtc_mktime(&tm));
/* Common non-leap year */
tm = (struct rtc_time){
.year = 1999, .mon = 12, .mday = 6, .hour = 16, .min = 13, .sec = 59,
};
assert_int_equal(944496839, rtc_mktime(&tm));
/* Ensure that February 29 gives the same result as March 1 in non-leap year */
tm = (struct rtc_time){
.year = 2017, .mon = 2, .mday = 29, .hour = 1, .min = 2, .sec = 3,
};
tm2 = (struct rtc_time){
.year = 2017, .mon = 3, .mday = 1, .hour = 1, .min = 2, .sec = 3,
};
assert_int_equal(rtc_mktime(&tm), rtc_mktime(&tm2));
/* Leap year (only division by 4 rule applies) */
tm = (struct rtc_time){
.year = 2004, .mon = 8, .mday = 30, .hour = 13, .min = 45, .sec = 33,
};
assert_int_equal(1093873533, rtc_mktime(&tm));
/* Last day of February in leap year */
tm.mon = 2;
tm.mday = 29;
assert_int_equal(1078062333, rtc_mktime(&tm));
/* Ensure that February 29 and March 1 have different different and correct values
in leap year */
tm = (struct rtc_time){
.year = 2004, .mon = 3, .mday = 1, .hour = 7, .min = 7, .sec = 17,
};
tm2 = (struct rtc_time){
.year = 2004, .mon = 2, .mday = 29, .hour = 7, .min = 7, .sec = 17,
};
/* There should be exactly one day of difference */
assert_int_equal(24 * 60 * 60, rtc_mktime(&tm) - rtc_mktime(&tm2));
/* Leap year (division by 400 rule applies and division by 100 is excluded) */
tm = (struct rtc_time){
.year = 2000, .mon = 6, .mday = 11, .hour = 21, .min = 3, .sec = 6,
};
assert_int_equal(960757386, rtc_mktime(&tm));
tm.mon = 2;
tm.mday = 29;
assert_int_equal(951858186, rtc_mktime(&tm));
tm = (struct rtc_time){
.year = 2000, .mon = 3, .mday = 1, .hour = 10, .min = 55, .sec = 21,
};
tm2 = (struct rtc_time){
.year = 2000, .mon = 2, .mday = 29, .hour = 10, .min = 55, .sec = 21,
};
assert_int_equal(24 * 60 * 60, rtc_mktime(&tm) - rtc_mktime(&tm2));
}
static void assert_rtc_time_equal(struct rtc_time *tm1, struct rtc_time *tm2)
{
assert_int_equal(tm1->sec, tm2->sec);
assert_int_equal(tm1->min, tm2->min);
assert_int_equal(tm1->hour, tm2->hour);
assert_int_equal(tm1->mday, tm2->mday);
assert_int_equal(tm1->mon, tm2->mon);
assert_int_equal(tm1->year, tm2->year);
assert_int_equal(tm1->wday, tm2->wday);
}
/* This test check if combination of rtc_to_tm and rtc_mktime gives result equal to input.
Week day is ignored by rtc_mktime, but is calculated by rtc_to_tm, so it is included
in input. */
static void test_rtc_mktime_with_rtc_to_tm(void **state)
{
struct rtc_time tm_in;
struct rtc_time tm_out;
int tim;
memset(&tm_in, 0, sizeof(tm_in));
memset(&tm_out, 0, sizeof(tm_out));
/* Conversion from rtc_time to timestamp and back to rtc_time */
tm_in = (struct rtc_time){
.year = 1970, .mon = 1, .mday = 1, .hour = 0, .min = 0, .sec = 0, .wday = 4
};
assert_int_equal(0, rtc_to_tm(rtc_mktime(&tm_in), &tm_out));
assert_rtc_time_equal(&tm_in, &tm_out);
tm_in = (struct rtc_time){
.year = 2000, .mon = 2, .mday = 29, .hour = 13, .min = 4, .sec = 15, .wday = 2
};
assert_int_equal(0, rtc_to_tm(rtc_mktime(&tm_in), &tm_out));
assert_rtc_time_equal(&tm_in, &tm_out);
tm_in = (struct rtc_time){
.year = 2000, .mon = 3, .mday = 1, .hour = 13, .min = 8, .sec = 37, .wday = 3
};
assert_int_equal(0, rtc_to_tm(rtc_mktime(&tm_in), &tm_out));
assert_rtc_time_equal(&tm_in, &tm_out);
tm_in = (struct rtc_time){
.year = 2017, .mon = 12, .mday = 7, .hour = 8, .min = 18, .sec = 9, .wday = 4
};
assert_int_equal(0, rtc_to_tm(rtc_mktime(&tm_in), &tm_out));
assert_rtc_time_equal(&tm_in, &tm_out);
tm_in = (struct rtc_time){
.year = 2020, .mon = 2, .mday = 29, .hour = 18, .min = 50, .sec = 0, .wday = 6
};
assert_int_equal(0, rtc_to_tm(rtc_mktime(&tm_in), &tm_out));
assert_rtc_time_equal(&tm_in, &tm_out);
tm_in = (struct rtc_time){
.year = 2020, .mon = 3, .mday = 1, .hour = 1, .min = 20, .sec = 23, .wday = 0
};
assert_int_equal(0, rtc_to_tm(rtc_mktime(&tm_in), &tm_out));
assert_rtc_time_equal(&tm_in, &tm_out);
/* Conversion from timestamp to rtc_time and back to timestamp */
tim = 0;
rtc_to_tm(tim, &tm_out);
assert_int_equal(tim, rtc_mktime(&tm_out));
tim = INT32_MAX;
rtc_to_tm(tim, &tm_out);
assert_int_equal(tim, rtc_mktime(&tm_out));
/* 2000-02-29 1:23:34 */
tim = 951787414;
rtc_to_tm(tim, &tm_out);
assert_int_equal(tim, rtc_mktime(&tm_out));
/* 2000-03-01 1:23:34 */
tim = 951873814;
rtc_to_tm(tim, &tm_out);
assert_int_equal(tim, rtc_mktime(&tm_out));
/* 1999-09-09 9:09:09 */
tim = 936868149;
rtc_to_tm(tim, &tm_out);
assert_int_equal(tim, rtc_mktime(&tm_out));
/* 2020-02-29 2:29:02 */
tim = 1582943342;
rtc_to_tm(tim, &tm_out);
assert_int_equal(tim, rtc_mktime(&tm_out));
/* 2020-03-01 3:01:03 */
tim = 1583031663;
rtc_to_tm(tim, &tm_out);
assert_int_equal(tim, rtc_mktime(&tm_out));
}
static void test_leap_day_secday(void **state)
{
const int secday = 60 * 60 * 24;
struct rtc_time tm_in;
struct rtc_time tm_out;
struct rtc_time tm_expected;
int tim;
memset(&tm_in, 0, sizeof(tm_in));
memset(&tm_out, 0, sizeof(tm_out));
/* Non-leap year */
tm_in = (struct rtc_time) {
.year = 1999, .mon = 2, .mday = 28, .hour = 5, .min = 37, .sec = 15, .wday = 0
};
tim = rtc_mktime(&tm_in) + secday;
tm_expected = (struct rtc_time) {
.year = 1999, .mon = 3, .mday = 1, .hour = 5, .min = 37, .sec = 15, .wday = 1
};
assert_int_equal(0, rtc_to_tm(tim, &tm_out));
assert_rtc_time_equal(&tm_out, &tm_expected);
/* Leap-year February 28 to February 29 */
tm_in = (struct rtc_time) {
.year = 2000, .mon = 2, .mday = 28, .hour = 0, .min = 33, .sec = 11, .wday = 1,
};
tim = rtc_mktime(&tm_in) + secday;
tm_expected = (struct rtc_time) {
.year = 2000, .mon = 2, .mday = 29, .hour = 0, .min = 33, .sec = 11, .wday = 2,
};
assert_int_equal(0, rtc_to_tm(tim, &tm_out));
assert_rtc_time_equal(&tm_out, &tm_expected);
tm_in = (struct rtc_time) {
.year = 2004, .mon = 2, .mday = 28, .hour = 9, .min = 13, .sec = 45, .wday = 6,
};
tim = rtc_mktime(&tm_in) + secday;
tm_expected = (struct rtc_time) {
.year = 2004, .mon = 2, .mday = 29, .hour = 9, .min = 13, .sec = 45, .wday = 0,
};
assert_int_equal(0, rtc_to_tm(tim, &tm_out));
assert_rtc_time_equal(&tm_out, &tm_expected);
/* Leap-year February 29 to March 1 */
tm_in = (struct rtc_time) {
.year = 2000, .mon = 2, .mday = 29, .hour = 22, .min = 50, .sec = 25, .wday = 2,
};
tim = rtc_mktime(&tm_in) + secday;
tm_expected = (struct rtc_time) {
.year = 2000, .mon = 3, .mday = 1, .hour = 22, .min = 50, .sec = 25, .wday = 3,
};
assert_int_equal(0, rtc_to_tm(tim, &tm_out));
assert_rtc_time_equal(&tm_out, &tm_expected);
tm_in = (struct rtc_time) {
.year = 2004, .mon = 2, .mday = 29, .hour = 17, .min = 56, .sec = 27, .wday = 0,
};
tim = rtc_mktime(&tm_in) + secday;
tm_expected = (struct rtc_time) {
.year = 2004, .mon = 3, .mday = 1, .hour = 17, .min = 56, .sec = 27, .wday = 1,
};
assert_int_equal(0, rtc_to_tm(tim, &tm_out));
assert_rtc_time_equal(&tm_out, &tm_expected);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_rtc_to_tm_from_unix_time),
cmocka_unit_test(test_mktime),
cmocka_unit_test(test_rtc_mktime_with_rtc_to_tm),
cmocka_unit_test(test_leap_day_secday),
};
return cb_run_group_tests(tests, NULL, NULL);
}
|