aboutsummaryrefslogtreecommitdiff
path: root/libc/test/run-time.c
blob: aae7f4f5f80aae5fd35b596bd4f1e7e51efaef53 (plain)
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
#include "/usr/include/assert.h"
#include <stdio.h>
#include <libc/include/time.h>
#include <stdint.h>

#include "../time.c"

#define MKTIME_TEST(Y,M,D,h,m,s,t) \
	tm.tm_year = Y; \
	tm.tm_mon = M; \
	tm.tm_mday = D; \
	tm.tm_hour = h; \
	tm.tm_min = m; \
	tm.tm_sec = s; \
	assert(mktime(&tm) == t); \
	assert(tm.tm_year == Y); \
	assert(tm.tm_mon == M); \
	assert(tm.tm_mday == D); \
	assert(tm.tm_hour == h); \
	assert(tm.tm_min == m); \
	assert(tm.tm_sec == s)

#define GMTIME_TEST(Y,M,D,h,m,s,tv) \
	t = tv; \
	gmtime_r(&t, &tm); \
	assert(tm.tm_year == Y); \
	assert(tm.tm_mon == M); \
	assert(tm.tm_mday == D); \
	assert(tm.tm_hour == h); \
	assert(tm.tm_min == m); \
	assert(tm.tm_sec == s)

#define TIME_TEST(Y,M,D,h,m,s,tv) \
	MKTIME_TEST(Y,M,D,h,m,s,tv);		\
	GMTIME_TEST(Y,M,D,h,m,s,tv)

int main(void)
{
	struct tm tm;
	time_t t = 0;

	TIME_TEST(1970, 0, 1, 0, 0, 0, 0);
	TIME_TEST(1971, 0, 1, 0, 0, 0, 365*SECS_PER_DAY);
	TIME_TEST(1972, 0, 1, 0, 0, 0, 2*365*SECS_PER_DAY);
	TIME_TEST(1972, 11, 31, 0, 0, 0, 3*365*SECS_PER_DAY);
	TIME_TEST(1973, 0, 1, 0, 0, 0, (3*365+1)*SECS_PER_DAY);
	TIME_TEST(2000, 11, 31, 0, 0, 0, 978220800);
	TIME_TEST(2001, 0, 1, 0, 0, 0, 978307200);
	TIME_TEST(2003, 11, 31, 0, 0, 0, 1072828800);
	TIME_TEST(2004, 0, 1, 0, 0, 0, 1072828800+SECS_PER_DAY);
	TIME_TEST(2004, 11, 29, 0, 0, 0, 1072828800+364*SECS_PER_DAY);
	TIME_TEST(2004, 11, 30, 0, 0, 0, 1072828800+365*SECS_PER_DAY);
	TIME_TEST(2004, 11, 31, 0, 0, 0, 1072828800+366*SECS_PER_DAY);
	TIME_TEST(2004, 11, 31, 23, 59, 59, 1072828800+367*SECS_PER_DAY-1);
	TIME_TEST(2100, 11, 31, 0, 0, 0, 4133894400);
	TIME_TEST(2101, 0, 1, 0, 0, 0, 4133980800);

	/* Test the normalisation functionality of mktime */
	tm.tm_year = 2000;
	tm.tm_mon = 1;
	tm.tm_mday = 10;
	tm.tm_hour = 5;
	tm.tm_min = 32;
	tm.tm_sec = 105;
	mktime(&tm);
	assert(tm.tm_year == 2000);
	assert(tm.tm_mon == 1);
	assert(tm.tm_mday == 10);
	assert(tm.tm_hour == 5);
	assert(tm.tm_min == 33);
	assert(tm.tm_sec == 45);
	tm.tm_sec += 366*24*60*60;
	mktime(&tm);
	assert(tm.tm_year == 2001);
	assert(tm.tm_mon == 1);
	assert(tm.tm_mday == 10);
	assert(tm.tm_hour == 5);
	assert(tm.tm_min == 33);
	assert(tm.tm_sec == 45);

	return 0;
}