-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsafe_year.t.c
107 lines (85 loc) · 2.83 KB
/
safe_year.t.c
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
/* Test the internal safe_year() function which does the critical mapping
to make the localtime trick work
*/
#include "time64.c"
#include "t/tap.h"
static void year_to_TM(const Year year, struct TM *date) {
Time64_T time;
date->tm_year = (year_t)(year - 1900);
date->tm_mon = 0;
date->tm_mday = 1;
date->tm_hour = 0;
date->tm_min = 0;
date->tm_sec = 0;
time = timegm64(date);
gmtime64_r(&time, date);
}
static void year_to_tm(const Year year, struct tm *ret) {
Time64_T time;
struct TM date;
date.tm_year = (year_t)(year - 1900);
date.tm_mon = 0;
date.tm_mday = 1;
date.tm_hour = 0;
date.tm_min = 0;
date.tm_sec = 0;
time = timegm64(&date);
gmtime64_r(&time, &date);
copy_TM64_to_tm(&date, ret);
}
static void test_safe_year(Year orig_year) {
int year = safe_year( orig_year );
struct tm safe_tm;
struct TM orig_tm;
if( orig_year > 1970 && orig_year < 2038 ) {
is_Int64( orig_year, (Year)year, "safe_year(%d) returns same year if already safe",
orig_year);
}
else if( orig_year <= 1970 ) {
ok( year < 2000, "safe_year(%d) < 2000 for year <= 1970", year );
}
else if( orig_year >= 2038 ) {
ok( year > 2000, "safe_year(%d) > 2000 for year >= 2038", year );
}
else {
assert(0);
}
year_to_tm((Year)year, &safe_tm);
year_to_TM(orig_year, &orig_tm);
is_Int64( (Year)orig_tm.tm_year, (Year)(orig_year - 1900), "year_to_tm(orig %d)",
(int)orig_year );
is_int( safe_tm.tm_year, year - 1900, "year_to_TM(safe %d)", year );
ok(1, "orig_year: %"PRId64", safe_year: %d", orig_year, year);
is_int( safe_tm.tm_wday, orig_tm.tm_wday, " tm_wday" );
is_int( IS_LEAP( year - 1900), IS_LEAP( orig_year - 1900), " ISLEAP(%d)", year );
if (orig_year > 0)
is_int( IS_LEAP_ABS(year), IS_LEAP_ABS(orig_year), " ISLEAP_ABS(%d)", year );
year--;
orig_year--;
year_to_tm((Year)year, &safe_tm);
year_to_TM(orig_year, &orig_tm);
is_int( safe_tm.tm_wday, orig_tm.tm_wday, " previous tm_wday" );
if (orig_year > 0)
is_int( IS_LEAP_ABS(year), IS_LEAP_ABS(orig_year)," previous ISLEAP(%d)", orig_year );
year += 2;
orig_year += 2;
year_to_tm((Year)year, &safe_tm);
year_to_TM(orig_year, &orig_tm);
is_int( safe_tm.tm_wday, orig_tm.tm_wday, " next tm_wday" );
/* Don't care about the next leap status */
}
int main(void) {
Year year;
/* Boundry tests */
test_safe_year((Year)1);
test_safe_year((Year)0);
test_safe_year((Year)-1);
for( year = 1599; year <= 2401; year++ ) {
test_safe_year(year);
}
for( year = -401; year <= 401; year++ ) {
test_safe_year(year);
}
done_testing();
return(0);
}