forked from thatguystone/approxidate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
130 lines (101 loc) · 3.33 KB
/
test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include "approxidate.h"
static int errors = 0;
#define assert_equal(a, b) if (a != b) { \
fprintf(stderr, "Error at line %d: %ld != %ld\n", __LINE__, (long)a, (long)b); \
errors++; }
static time_t _start_of_day(time_t sec)
{
return sec - (sec % 86400);
}
int main()
{
long usec;
time_t ts;
struct tm *tm;
char buff[128];
struct timeval tv;
approxidate("10/Mar/2013:00:00:02.003 UTC", &tv);
assert_equal(tv.tv_sec, 1362873602);
assert_equal(tv.tv_usec, 3000);
approxidate("10/Mar/2013:00:00:02 UTC", &tv);
assert_equal(tv.tv_sec, 1362873602);
assert_equal(tv.tv_usec, 0);
approxidate("10/Mar/2013:00:00:07 UTC", &tv);
assert_equal(tv.tv_sec, 1362873607);
assert_equal(tv.tv_usec, 0);
approxidate("10/Mar/2012:00:00:07 UTC", &tv);
assert_equal(tv.tv_sec, 1331337607);
assert_equal(tv.tv_usec, 0);
approxidate("10/Mar/2012:00:00:07 +0500", &tv);
assert_equal(tv.tv_sec, 1331319607);
assert_equal(tv.tv_usec, 0);
approxidate("10/Mar/2012:00:00:07.657891 +0500", &tv);
assert_equal(tv.tv_sec, 1331319607);
assert_equal(tv.tv_usec, 657891);
approxidate("10/Mar/2012:00:00:07.657891 +1400", &tv);
assert_equal(tv.tv_sec, 1331287207);
assert_equal(tv.tv_usec, 657891);
approxidate("10/Mar/2012:00:00:07.657891 -0110", &tv);
assert_equal(tv.tv_sec, 1331341807);
assert_equal(tv.tv_usec, 657891);
approxidate("mar 10 2013 00:00:07 UTC", &tv);
assert_equal(tv.tv_sec, 1362873607);
assert_equal(tv.tv_usec, 0);
approxidate("mar 10 2013 04:00:07 -0500", &tv);
assert_equal(tv.tv_sec, 1362906007);
assert_equal(tv.tv_usec, 0);
approxidate("march 10 2013 04:00:07 -0500", &tv);
assert_equal(tv.tv_sec, 1362906007);
assert_equal(tv.tv_usec, 0);
approxidate("march 10 2013 04:00:07 -0500", &tv);
assert_equal(tv.tv_sec, 1362906007);
assert_equal(tv.tv_usec, 0);
approxidate("10 march 2013 04:00:07 -0500", &tv);
assert_equal(tv.tv_sec, 1362906007);
assert_equal(tv.tv_usec, 0);
approxidate("2013 10 march 04:00:07 -0500", &tv);
assert_equal(tv.tv_sec, 1362906007);
assert_equal(tv.tv_usec, 0);
approxidate("2013 march 10 04:00:07 -0500", &tv);
assert_equal(tv.tv_sec, 1362906007);
assert_equal(tv.tv_usec, 0);
approxidate("00:00:07.657891", &tv);
assert_equal(tv.tv_usec, 657891);
approxidate("23:11:07.9876 +1400", &tv);
assert_equal(tv.tv_usec, 987600);
approxidate("23:11:07.9876", &tv);
assert_equal(tv.tv_usec, 987600);
approxidate("1/1/2014", &tv);
assert_equal(_start_of_day(tv.tv_sec), 1388534400);
approxidate("1/1/2014 UTC", &tv);
assert_equal(_start_of_day(tv.tv_sec), 1388534400);
/*
* Git doesn't allow dates more than 10 days in the future. Make sure
* approxidate does.
* * if today is 3/15/2015
* * 8/15/2015 should parse as "Aug 15, 2015", not "Mar 8, 2015".
*/
ts = time(NULL);
ts += 86400 * 31 * 5;
tm = gmtime(&ts);
strftime(buff, sizeof(buff), "%m/%d/%Y", tm);
approxidate(buff, &tv);
assert_equal(_start_of_day(tv.tv_sec), _start_of_day(ts));
gettimeofday(&tv, NULL);
usec = tv.tv_usec;
approxidate("10/Mar/2012", &tv);
if (!((usec - 10000) < tv.tv_usec && (usec + 10000) > tv.tv_usec)) {
fprintf(stderr, "Error: usec calculation for anonymous time is off\n"); \
errors++;
}
if (errors > 0) {
fprintf(stderr, "%d tests failed\n", errors);
return 1;
} else {
fprintf(stderr, "All tests passed!\n");
}
return 0;
}