caps: improve _do_simplify
[platform/upstream/gstreamer.git] / tests / check / gst / gstdatetime.c
1 /* GStreamer
2  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
3  * Copyright (C) 2010 Christian Hergert <chris@dronelabs.com>
4  *
5  * gstdatetime.c: Unit tests for GstDateTime
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <string.h>
24 #include <time.h>
25 #include <gst/check/gstcheck.h>
26
27 #define assert_almost_equals_int(a, b)                            \
28 G_STMT_START {                                                           \
29   int first = a;                                                         \
30   int second = b;                                                        \
31   fail_unless(ABS (first - second) <= 1,                                 \
32     "'" #a "' (%d) is not almost equal to '" #b"' (%d)", first, second); \
33 } G_STMT_END;
34
35 GST_START_TEST (test_GstDateTime_now)
36 {
37   GstDateTime *dt;
38   time_t t;
39   struct tm tm;
40
41   memset (&tm, 0, sizeof (tm));
42   t = time (NULL);
43 #ifdef HAVE_LOCALTIME_R
44   localtime_r (&t, &tm);
45 #else
46   memcpy (&tm, localtime (&t), sizeof (struct tm));
47 #endif
48   dt = gst_date_time_new_now_local_time ();
49   assert_equals_int (gst_date_time_get_year (dt), 1900 + tm.tm_year);
50   assert_equals_int (gst_date_time_get_month (dt), 1 + tm.tm_mon);
51   assert_equals_int (gst_date_time_get_day (dt), tm.tm_mday);
52   assert_equals_int (gst_date_time_get_hour (dt), tm.tm_hour);
53   assert_equals_int (gst_date_time_get_minute (dt), tm.tm_min);
54   assert_almost_equals_int (gst_date_time_get_second (dt), tm.tm_sec);
55   gst_date_time_unref (dt);
56 }
57
58 GST_END_TEST;
59
60 GST_START_TEST (test_GstDateTime_new_from_unix_epoch_local_time)
61 {
62   GstDateTime *dt;
63   struct tm tm;
64   time_t t;
65
66   memset (&tm, 0, sizeof (tm));
67   t = time (NULL);
68 #ifdef HAVE_LOCALTIME_R
69   localtime_r (&t, &tm);
70 #else
71   memcpy (&tm, localtime (&t), sizeof (struct tm));
72 #endif
73   dt = gst_date_time_new_from_unix_epoch_local_time (t);
74   assert_equals_int (gst_date_time_get_year (dt), 1900 + tm.tm_year);
75   assert_equals_int (gst_date_time_get_month (dt), 1 + tm.tm_mon);
76   assert_equals_int (gst_date_time_get_day (dt), tm.tm_mday);
77   assert_equals_int (gst_date_time_get_hour (dt), tm.tm_hour);
78   assert_equals_int (gst_date_time_get_minute (dt), tm.tm_min);
79   assert_equals_int (gst_date_time_get_second (dt), tm.tm_sec);
80   gst_date_time_unref (dt);
81
82   memset (&tm, 0, sizeof (tm));
83   tm.tm_year = 70;
84   tm.tm_mday = 1;
85   tm.tm_mon = 0;
86   tm.tm_hour = 0;
87   tm.tm_min = 0;
88   tm.tm_sec = 0;
89   t = mktime (&tm);
90   dt = gst_date_time_new_from_unix_epoch_local_time (t);
91   assert_equals_int (gst_date_time_get_year (dt), 1970);
92   assert_equals_int (gst_date_time_get_month (dt), 1);
93   assert_equals_int (gst_date_time_get_day (dt), 1);
94   assert_equals_int (gst_date_time_get_hour (dt), 0);
95   assert_equals_int (gst_date_time_get_minute (dt), 0);
96   assert_equals_int (gst_date_time_get_second (dt), 0);
97   gst_date_time_unref (dt);
98 }
99
100 GST_END_TEST;
101
102
103 GST_START_TEST (test_GstDateTime_new_from_unix_epoch_utc)
104 {
105   GstDateTime *dt;
106   struct tm tm;
107   time_t t;
108
109   memset (&tm, 0, sizeof (tm));
110   t = time (NULL);
111 #ifdef HAVE_GMTIME_R
112   gmtime_r (&t, &tm);
113 #else
114   memcpy (&tm, gmtime (&t), sizeof (struct tm));
115 #endif
116   dt = gst_date_time_new_from_unix_epoch_utc (t);
117   assert_equals_int (gst_date_time_get_year (dt), 1900 + tm.tm_year);
118   assert_equals_int (gst_date_time_get_month (dt), 1 + tm.tm_mon);
119   assert_equals_int (gst_date_time_get_day (dt), tm.tm_mday);
120   assert_equals_int (gst_date_time_get_hour (dt), tm.tm_hour);
121   assert_equals_int (gst_date_time_get_minute (dt), tm.tm_min);
122   assert_equals_int (gst_date_time_get_second (dt), tm.tm_sec);
123   assert_equals_int (gst_date_time_get_time_zone_offset (dt), 0);
124   gst_date_time_unref (dt);
125 }
126
127 GST_END_TEST;
128
129 GST_START_TEST (test_GstDateTime_get_dmy)
130 {
131   GstDateTime *dt;
132   time_t t;
133   struct tm tt;
134
135   t = time (NULL);
136 #ifdef HAVE_LOCALTIME_R
137   localtime_r (&t, &tt);
138 #else
139   memcpy (&tt, localtime (&t), sizeof (struct tm));
140 #endif
141   dt = gst_date_time_new_from_unix_epoch_local_time (t);
142   assert_equals_int (gst_date_time_get_year (dt), tt.tm_year + 1900);
143   assert_equals_int (gst_date_time_get_month (dt), tt.tm_mon + 1);
144   assert_equals_int (gst_date_time_get_day (dt), tt.tm_mday);
145
146   gst_date_time_unref (dt);
147 }
148
149 GST_END_TEST;
150
151 GST_START_TEST (test_GstDateTime_get_hour)
152 {
153   GstDateTime *dt;
154
155   dt = gst_date_time_new (0, 2009, 10, 19, 15, 13, 11);
156   assert_equals_int (15, gst_date_time_get_hour (dt));
157   gst_date_time_unref (dt);
158
159   dt = gst_date_time_new (0, 100, 10, 19, 1, 0, 0);
160   assert_equals_int (1, gst_date_time_get_hour (dt));
161   gst_date_time_unref (dt);
162
163   dt = gst_date_time_new (0, 100, 10, 19, 0, 0, 0);
164   assert_equals_int (0, gst_date_time_get_hour (dt));
165   gst_date_time_unref (dt);
166
167   dt = gst_date_time_new (0, 100, 10, 1, 23, 59, 59);
168   assert_equals_int (23, gst_date_time_get_hour (dt));
169   gst_date_time_unref (dt);
170 }
171
172 GST_END_TEST;
173
174 GST_START_TEST (test_GstDateTime_get_microsecond)
175 {
176   GTimeVal tv;
177   GstDateTime *dt;
178
179   g_get_current_time (&tv);
180   dt = gst_date_time_new (0, 2010, 7, 15, 11, 12,
181       13 + (tv.tv_usec / 1000000.0));
182   assert_almost_equals_int (tv.tv_usec, gst_date_time_get_microsecond (dt));
183   gst_date_time_unref (dt);
184 }
185
186 GST_END_TEST;
187
188 GST_START_TEST (test_GstDateTime_get_minute)
189 {
190   GstDateTime *dt;
191
192   dt = gst_date_time_new (0, 2009, 12, 1, 1, 31, 0);
193   assert_equals_int (31, gst_date_time_get_minute (dt));
194   gst_date_time_unref (dt);
195 }
196
197 GST_END_TEST;
198
199 GST_START_TEST (test_GstDateTime_get_second)
200 {
201   GstDateTime *dt;
202
203   dt = gst_date_time_new (0, 2009, 12, 1, 1, 31, 44);
204   assert_equals_int (44, gst_date_time_get_second (dt));
205   gst_date_time_unref (dt);
206 }
207
208 GST_END_TEST;
209
210 GST_START_TEST (test_GstDateTime_new_full)
211 {
212   GstDateTime *dt;
213
214   dt = gst_date_time_new (0, 2009, 12, 11, 12, 11, 10.001234);
215   assert_equals_int (2009, gst_date_time_get_year (dt));
216   assert_equals_int (12, gst_date_time_get_month (dt));
217   assert_equals_int (11, gst_date_time_get_day (dt));
218   assert_equals_int (12, gst_date_time_get_hour (dt));
219   assert_equals_int (11, gst_date_time_get_minute (dt));
220   assert_equals_int (10, gst_date_time_get_second (dt));
221   assert_equals_int (1234, gst_date_time_get_microsecond (dt));
222   assert_equals_float (0, gst_date_time_get_time_zone_offset (dt));
223   gst_date_time_unref (dt);
224
225   dt = gst_date_time_new (2.5, 2010, 3, 29, 12, 13, 16.5);
226   assert_equals_int (2010, gst_date_time_get_year (dt));
227   assert_equals_int (3, gst_date_time_get_month (dt));
228   assert_equals_int (29, gst_date_time_get_day (dt));
229   assert_equals_int (12, gst_date_time_get_hour (dt));
230   assert_equals_int (13, gst_date_time_get_minute (dt));
231   assert_equals_int (16, gst_date_time_get_second (dt));
232   assert_equals_int (500000, gst_date_time_get_microsecond (dt));
233   assert_equals_float (2.5, gst_date_time_get_time_zone_offset (dt));
234   gst_date_time_unref (dt);
235 }
236
237 GST_END_TEST;
238
239 GST_START_TEST (test_GstDateTime_utc_now)
240 {
241   GstDateTime *dt;
242   time_t t;
243   struct tm tm;
244
245   t = time (NULL);
246 #ifdef HAVE_GMTIME_R
247   gmtime_r (&t, &tm);
248 #else
249   memcpy (&tm, gmtime (&t), sizeof (struct tm));
250 #endif
251   dt = gst_date_time_new_now_utc ();
252   assert_equals_int (tm.tm_year + 1900, gst_date_time_get_year (dt));
253   assert_equals_int (tm.tm_mon + 1, gst_date_time_get_month (dt));
254   assert_equals_int (tm.tm_mday, gst_date_time_get_day (dt));
255   assert_equals_int (tm.tm_hour, gst_date_time_get_hour (dt));
256   assert_equals_int (tm.tm_min, gst_date_time_get_minute (dt));
257   assert_almost_equals_int (tm.tm_sec, gst_date_time_get_second (dt));
258   gst_date_time_unref (dt);
259 }
260
261 GST_END_TEST;
262
263 GST_START_TEST (test_GstDateTime_get_utc_offset)
264 {
265   GstDateTime *dt;
266   gfloat ts;
267   struct tm tm;
268   time_t t;
269
270   t = time (NULL);
271   memset (&tm, 0, sizeof (tm));
272 #ifdef HAVE_LOCALTIME_R
273   localtime_r (&t, &tm);
274 #else
275   memcpy (&tm, localtime (&t), sizeof (struct tm));
276 #endif
277
278   dt = gst_date_time_new_now_local_time ();
279   ts = gst_date_time_get_time_zone_offset (dt);
280   assert_equals_int (ts, tm.tm_gmtoff / 3600.0);
281   gst_date_time_unref (dt);
282 }
283
284 GST_END_TEST;
285
286 static Suite *
287 gst_date_time_suite (void)
288 {
289   Suite *s = suite_create ("GstDateTime");
290   TCase *tc_chain = tcase_create ("general");
291
292   suite_add_tcase (s, tc_chain);
293   tcase_add_test (tc_chain, test_GstDateTime_get_dmy);
294   tcase_add_test (tc_chain, test_GstDateTime_get_hour);
295   tcase_add_test (tc_chain, test_GstDateTime_get_microsecond);
296   tcase_add_test (tc_chain, test_GstDateTime_get_minute);
297   tcase_add_test (tc_chain, test_GstDateTime_get_second);
298   tcase_add_test (tc_chain, test_GstDateTime_get_utc_offset);
299   tcase_add_test (tc_chain, test_GstDateTime_new_from_unix_epoch_local_time);
300   tcase_add_test (tc_chain, test_GstDateTime_new_from_unix_epoch_utc);
301   tcase_add_test (tc_chain, test_GstDateTime_new_full);
302   tcase_add_test (tc_chain, test_GstDateTime_now);
303   tcase_add_test (tc_chain, test_GstDateTime_utc_now);
304
305   return s;
306 }
307
308 GST_CHECK_MAIN (gst_date_time);