wibble 1.1
grcal.test.h
Go to the documentation of this file.
1/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2 (c) 2007 Enrico Zini <enrico@enricozini.org> */
3
4#include <wibble/test.h>
6
7namespace {
8
9using namespace std;
10using namespace wibble;
11using namespace wibble::grcal;
12
13#define assert_dt_eq(x, ...) assert_dt_eq_fn( LOCATION( #x " == " #__VA_ARGS__ ), x, __VA_ARGS__)
14void assert_dt_eq_fn( Location l, const int* val, int ye, int mo=-1, int da=-1, int ho=-1, int mi=-1, int se=-1 )
15{
16 int cmp[6] = { ye, mo, da, ho, mi, se };
17 std::string a = date::tostring(val);
18 std::string b = date::tostring(cmp);
19
20 if ( !( a == b ) ) {
21 AssertFailed f( l );
22 f << " got ["
23 << a << "] != [" << b
24 << "] instead";
25 }
26}
27
28// This is copied from grcal.cpp, which is dangerous as I may forget to keep
29// in sync; however, it's not a function I'd like to export, but it's a
30// function I'd like to test
31static inline void normalN(int& lo, int& hi, int N)
32{
33 if (lo < 0)
34 {
35 int m = (-lo)/N;
36 if (lo % N) ++m;
37 hi -= m;
38 lo = (lo + (m*N)) % N;
39 } else {
40 hi += lo / N;
41 lo = lo % N;
42 }
43}
44
45#define assert_nn_eq(x, y, N, x1, y1) assert_nn_eq_fn( LOCATION( #x ", " #y " mod " #N " == " #x1 ", " #y1 ), x, y, N, x1, y1)
46void assert_nn_eq_fn( Location l, int x, int y, int N, int x1, int y1)
47{
48 int vx = x;
49 int vy = y;
50 normalN(vx, vy, N);
51
52 if (vx == x1 && vy == y1)
53 return;
54
55 AssertFailed f( l );
56 f << " got ["
57 << vx << ", " << vy << "] != ["
58 << x1 << ", " << y1 << "] instead";
59}
60
61
62struct TestGrcalDate {
63 void fill(int* dst, int ye, int mo=-1, int da=-1, int ho=-1, int mi=-1, int se=-1)
64 {
65 dst[0] = ye;
66 dst[1] = mo;
67 dst[2] = da;
68 dst[3] = ho;
69 dst[4] = mi;
70 dst[5] = se;
71 }
72
74 {
75 assert_nn_eq(0, 0, 60, 0, 0);
76 assert_nn_eq(-1, 0, 60, 59, -1);
77 assert_nn_eq(60, 60, 60, 0, 61);
78 assert_nn_eq(60, 0, 60, 0, 1);
79 assert_nn_eq(0, 60, 60, 0, 60);
80 assert_nn_eq(-3600, 0, 60, 0, -60);
81 assert_nn_eq(-61, 1, 60, 59, -1);
82 assert_nn_eq(-0, 0, 60, 0, 0);
83 }
84
86 {
87 // Trenta giorni ha novembre
88 assert_eq(date::daysinmonth(2008, 11), 30);
89 // Con april, giugno e settembre
90 assert_eq(date::daysinmonth(2008, 4), 30);
91 assert_eq(date::daysinmonth(2008, 6), 30);
92 assert_eq(date::daysinmonth(2008, 9), 30);
93 // Di ventotto ce n'รจ uno
94 assert_eq(date::daysinmonth(2001, 2), 28);
95 assert_eq(date::daysinmonth(2004, 2), 29);
96 assert_eq(date::daysinmonth(2100, 2), 28);
97 assert_eq(date::daysinmonth(2000, 2), 29);
98 // Tutti gli altri ne han trentuno
99 assert_eq(date::daysinmonth(2008, 1), 31);
100 assert_eq(date::daysinmonth(2008, 3), 31);
101 assert_eq(date::daysinmonth(2008, 5), 31);
102 assert_eq(date::daysinmonth(2008, 7), 31);
103 assert_eq(date::daysinmonth(2008, 8), 31);
104 assert_eq(date::daysinmonth(2008, 10), 31);
105 assert_eq(date::daysinmonth(2008, 12), 31);
106 }
107
109 {
110 assert_eq(date::daysinyear(2001), 365);
111 assert_eq(date::daysinyear(2004), 366);
112 assert_eq(date::daysinyear(2100), 365);
113 assert_eq(date::daysinyear(2000), 366);
114 }
115
116 Test easter()
117 {
118 int month, day;
119 date::easter(2008, &month, &day);
120 assert_eq(month, 3);
121 assert_eq(day, 23);
122 }
123
124 Test tostring()
125 {
126 int val[6];
127 fill(val, 2008);
128 assert_eq(date::tostring(val), "2008");
129 fill(val, 2008, 3);
130 assert_eq(date::tostring(val), "2008-03");
131 fill(val, 2008, 3, 31);
132 assert_eq(date::tostring(val), "2008-03-31");
133 fill(val, 2008, 3, 31, 3);
134 assert_eq(date::tostring(val), "2008-03-31 03");
135 fill(val, 2008, 3, 31, 3, 21);
136 assert_eq(date::tostring(val), "2008-03-31 03:21");
137 fill(val, 2008, 3, 31, 3, 21, 0);
138 assert_eq(date::tostring(val), "2008-03-31 03:21:00");
139 }
140
142 {
143 int src[6];
144 int dst[6];
145 fill(src, 2008);
147 assert_dt_eq(dst, 2008, 1, 1, 0, 0, 0);
148
150 assert_dt_eq(src, 2008, 1, 1, 0, 0, 0);
151 }
152
154 {
155 int val[6];
156 fill(val, 2008, 1, 1, 0, 0, 0);
157 date::normalise(val);
158 assert_dt_eq(val, 2008, 1, 1, 0, 0, 0);
159
160 fill(val, 2008, 1, 1, 0, 0, 0);
161 val[1] -= 12;
162 date::normalise(val);
163 assert_dt_eq(val, 2007, 1, 1, 0, 0, 0);
164
165 fill(val, 2008, 3, 1, 0, 0, 0);
166 val[5] -= 1;
167 date::normalise(val);
168 assert_dt_eq(val, 2008, 2, 29, 23, 59, 59);
169
170 fill(val, 2008, 2, 28, 23, 0, 0);
171 val[5] += 3600;
172 date::normalise(val);
173 assert_dt_eq(val, 2008, 2, 29, 0, 0, 0);
174
175 fill(val, 2008, 2, 28, 23, 0, 0);
176 val[5] += 3600;
177 val[0] += 1;
178 date::normalise(val);
179 assert_dt_eq(val, 2009, 3, 1, 0, 0, 0);
180
181 fill(val, 2008, 2, 28, 23, 0, 0);
182 val[5] += 3600;
183 val[1] += 12;
184 date::normalise(val);
185 assert_dt_eq(val, 2009, 3, 1, 0, 0, 0);
186 }
187
189 {
190 int src[6];
191 int dst[6];
192 fill(src, 2008);
194 assert_dt_eq(dst, 2008, 12, 31, 23, 59, 59);
195
197 assert_dt_eq(src, 2008, 12, 31, 23, 59, 59);
198
199 fill(src, 2008, 2);
201 assert_dt_eq(src, 2008, 2, 29, 23, 59, 59);
202 }
203
204 Test duration()
205 {
206 int val1[6];
207 int val2[6];
208
209 fill(val1, 2007, 12, 25);
210 fill(val2, 2007, 12, 26);
211 assert_eq(date::duration(val1, val2), 3600*24);
212
213
214 fill(val1, 2007, 1, 2, 3, 4, 5);
215 assert_eq(date::secondsfrom(2006, val1), 3600*24*365+3600*24+3*3600+4*60+5);
216
217 fill(val2, 2007, 1, 1, 0, 0, 0);
218 assert_eq(date::secondsfrom(2006, val2), 3600*24*365);
219
220 fill(val2, 2006, 12, 31, 23, 59, 59);
221 assert_eq(date::secondsfrom(2006, val2), 3600*24*365-1);
222
223 fill(val1, 2006, 12, 31, 23, 59, 59);
224 fill(val2, 2007, 1, 2, 3, 4, 5);
225 assert_eq(date::duration(val1, val2), 1+3600*24+3*3600+4*60+5);
226 }
227};
228
229struct TestGrcalTime {
230 void fill(int* dst, int ho=-1, int mi=-1, int se=-1)
231 {
232 dst[0] = ho;
233 dst[1] = mi;
234 dst[2] = se;
235 }
236
237 Test tostring()
238 {
239 int val[3];
240 fill(val);
241 assert_eq(dtime::tostring(val), "");
242 fill(val, 9);
243 assert_eq(dtime::tostring(val), "09");
244 fill(val, 10, 9);
245 assert_eq(dtime::tostring(val), "10:09");
246 fill(val, 11, 10, 9);
247 assert_eq(dtime::tostring(val), "11:10:09");
248 }
250 {
251 assert_eq(dtime::tostring(3600), "01:00:00");
252 assert_eq(dtime::tostring(3661), "01:01:01");
253 }
254};
255
256}
257
258// vim:set ts=4 sw=4:
This header provides functions to handle Gregorian calendar dates and times.
#define assert_nn_eq(x, y, N, x1, y1)
Definition grcal.test.h:45
#define assert_dt_eq(x,...)
Definition grcal.test.h:13
std::string tostring(const int *val)
Convert a datetime to a string.
Definition grcal.cpp:319
int daysinyear(int year)
Return the number of days in a year.
Definition grcal.cpp:84
long long int secondsfrom(int year, const int *val)
Convert the given time in seconds elapsed since the beginning of the given year.
Definition grcal.cpp:229
void upperbound(const int *src, int *dst)
Make a copy of the datetime, filling in missing values with the highest possible value they can have.
Definition grcal.cpp:191
void lowerbound(const int *src, int *dst)
Make a copy of the datetime, filling in missing values with the lowest possible value they can have.
Definition grcal.cpp:112
int daysinmonth(int year, int month)
Return the number of days in a month.
Definition grcal.cpp:57
void normalise(int *res)
Normalise a datetime, in place.
Definition grcal.cpp:145
void easter(int year, int *month, int *day)
Compute the day of Easter.
Definition grcal.cpp:91
long long int duration(const int *begin, const int *end)
Give the duration in seconds of the interval between begin and end.
Definition grcal.cpp:245
std::string tostring(const int *val)
Format a time of day to a string.
Definition grcal.cpp:391
Definition grcal.cpp:53
Definition amorph.h:17
Definition test.h:54
Definition test.h:15
Definition amorph.h:30
#define assert_eq(x, y)
Definition test.h:33