Branch data Line data Source code
1 : : /* Unit tests for GTimer
2 : : * Copyright (C) 2013 Red Hat, Inc.
3 : : *
4 : : * SPDX-License-Identifier: LicenseRef-old-glib-tests
5 : : *
6 : : * This work is provided "as is"; redistribution and modification
7 : : * in whole or in part, in any medium, physical or electronic is
8 : : * permitted without restriction.
9 : : *
10 : : * This work is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 : : *
14 : : * In no event shall the authors or contributors be liable for any
15 : : * direct, indirect, incidental, special, exemplary, or consequential
16 : : * damages (including, but not limited to, procurement of substitute
17 : : * goods or services; loss of use, data, or profits; or business
18 : : * interruption) however caused and on any theory of liability, whether
19 : : * in contract, strict liability, or tort (including negligence or
20 : : * otherwise) arising in any way out of the use of this software, even
21 : : * if advised of the possibility of such damage.
22 : : *
23 : : * Author: Matthias Clasen
24 : : */
25 : :
26 : : /* We test a few deprecated APIs here. */
27 : : #define GLIB_DISABLE_DEPRECATION_WARNINGS 1
28 : :
29 : : #include "glib.h"
30 : :
31 : : static void
32 : 1 : test_timer_basic (void)
33 : : {
34 : : GTimer *timer;
35 : : gdouble elapsed;
36 : : gulong micros;
37 : :
38 : 1 : timer = g_timer_new ();
39 : :
40 : 1 : g_timer_start (timer);
41 : 1 : elapsed = g_timer_elapsed (timer, NULL);
42 : 1 : g_timer_stop (timer);
43 : 1 : g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
44 : :
45 : 1 : g_timer_destroy (timer);
46 : :
47 : 1 : timer = g_timer_new ();
48 : :
49 : 1 : g_timer_start (timer);
50 : 1 : elapsed = g_timer_elapsed (timer, NULL);
51 : 1 : g_timer_stop (timer);
52 : 1 : g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
53 : :
54 : 1 : g_timer_destroy (timer);
55 : :
56 : 1 : timer = g_timer_new ();
57 : :
58 : 1 : elapsed = g_timer_elapsed (timer, µs);
59 : :
60 : 1 : g_assert_cmpfloat (elapsed, <, 1.0);
61 : 1 : g_assert_cmpfloat_with_epsilon (elapsed, micros / 1e6, 0.001);
62 : :
63 : 1 : g_timer_destroy (timer);
64 : 1 : }
65 : :
66 : : static void
67 : 1 : test_timer_stop (void)
68 : : {
69 : : GTimer *timer;
70 : : gdouble elapsed, elapsed2;
71 : :
72 : 1 : timer = g_timer_new ();
73 : :
74 : 1 : g_timer_stop (timer);
75 : :
76 : 1 : elapsed = g_timer_elapsed (timer, NULL);
77 : 1 : g_usleep (100);
78 : 1 : elapsed2 = g_timer_elapsed (timer, NULL);
79 : :
80 : 1 : g_assert_cmpfloat (elapsed, ==, elapsed2);
81 : :
82 : 1 : g_timer_destroy (timer);
83 : 1 : }
84 : :
85 : : static void
86 : 1 : test_timer_continue (void)
87 : : {
88 : : GTimer *timer;
89 : : gdouble elapsed, elapsed2;
90 : :
91 : 1 : timer = g_timer_new ();
92 : :
93 : : /* Continue on a running timer */
94 [ + - ]: 1 : if (g_test_undefined ())
95 : : {
96 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
97 : : "*assertion*== FALSE*");
98 : 1 : g_timer_continue (timer);
99 : 1 : g_test_assert_expected_messages ();
100 : : }
101 : :
102 : 1 : g_timer_reset (timer);
103 : :
104 : : /* Continue on a stopped timer */
105 : 1 : g_usleep (100);
106 : 1 : g_timer_stop (timer);
107 : :
108 : 1 : elapsed = g_timer_elapsed (timer, NULL);
109 : 1 : g_timer_continue (timer);
110 : 1 : g_usleep (100);
111 : 1 : elapsed2 = g_timer_elapsed (timer, NULL);
112 : :
113 : 1 : g_assert_cmpfloat (elapsed, <, elapsed2);
114 : :
115 : 1 : g_timer_destroy (timer);
116 : 1 : }
117 : :
118 : : static void
119 : 1 : test_timer_reset (void)
120 : : {
121 : : GTimer *timer;
122 : : gdouble elapsed, elapsed2;
123 : :
124 : 1 : timer = g_timer_new ();
125 : 1 : g_usleep (100);
126 : 1 : g_timer_stop (timer);
127 : :
128 : 1 : elapsed = g_timer_elapsed (timer, NULL);
129 : 1 : g_timer_reset (timer);
130 : 1 : elapsed2 = g_timer_elapsed (timer, NULL);
131 : :
132 : 1 : g_assert_cmpfloat (elapsed, >, elapsed2);
133 : :
134 : 1 : g_timer_destroy (timer);
135 : 1 : }
136 : :
137 : : static void
138 : 1 : test_timer_is_active (void)
139 : : {
140 : : GTimer *timer;
141 : : gboolean is_active;
142 : :
143 : 1 : timer = g_timer_new ();
144 : 1 : is_active = g_timer_is_active (timer);
145 : 1 : g_assert_true (is_active);
146 : 1 : g_timer_stop (timer);
147 : 1 : is_active = g_timer_is_active (timer);
148 : 1 : g_assert_false (is_active);
149 : :
150 : 1 : g_timer_destroy (timer);
151 : 1 : }
152 : :
153 : : static void
154 : 1 : test_timeval_add (void)
155 : : {
156 : 1 : GTimeVal time = { 1, 0 };
157 : :
158 : 1 : g_time_val_add (&time, 10);
159 : :
160 : 1 : g_assert_cmpint (time.tv_sec, ==, 1);
161 : 1 : g_assert_cmpint (time.tv_usec, ==, 10);
162 : :
163 : 1 : g_time_val_add (&time, -500);
164 : 1 : g_assert_cmpint (time.tv_sec, ==, 0);
165 : 1 : g_assert_cmpint (time.tv_usec, ==, G_USEC_PER_SEC - 490);
166 : :
167 : 1 : g_time_val_add (&time, 1000);
168 : 1 : g_assert_cmpint (time.tv_sec, ==, 1);
169 : 1 : g_assert_cmpint (time.tv_usec, ==, 510);
170 : :
171 : 1 : g_time_val_add (&time, 0);
172 : 1 : g_assert_cmpint (time.tv_sec, ==, 1);
173 : 1 : g_assert_cmpint (time.tv_usec, ==, 510);
174 : :
175 : 1 : g_time_val_add (&time, -210);
176 : 1 : g_assert_cmpint (time.tv_sec, ==, 1);
177 : 1 : g_assert_cmpint (time.tv_usec, ==, 300);
178 : 1 : }
179 : :
180 : : typedef struct {
181 : : gboolean success;
182 : : const gchar *in;
183 : : GTimeVal val;
184 : : } TimeValParseTest;
185 : :
186 : : static void
187 : 1 : test_timeval_from_iso8601 (void)
188 : : {
189 : 1 : gchar *old_tz = g_strdup (g_getenv ("TZ"));
190 : 1 : TimeValParseTest tests[] = {
191 : : { TRUE, "1990-11-01T10:21:17Z", { 657454877, 0 } },
192 : : { TRUE, "19901101T102117Z", { 657454877, 0 } },
193 : : { TRUE, "19901101T102117+5", { 657454577, 0 } },
194 : : { TRUE, "19901101T102117+3:15", { 657443177, 0 } },
195 : : { TRUE, " 1990-11-01T10:21:17Z ", { 657454877, 0 } },
196 : : { TRUE, "1970-01-01T00:00:17.12Z", { 17, 120000 } },
197 : : { TRUE, "1970-01-01T00:00:17.1234Z", { 17, 123400 } },
198 : : { TRUE, "1970-01-01T00:00:17.123456Z", { 17, 123456 } },
199 : : { TRUE, "1980-02-22T12:36:00+02:00", { 320063760, 0 } },
200 : : { TRUE, "1980-02-22T10:36:00Z", { 320063760, 0 } },
201 : : { TRUE, "1980-02-22T10:36:00", { 320063760, 0 } },
202 : : { TRUE, "1980-02-22T12:36:00+02:00", { 320063760, 0 } },
203 : : { TRUE, "19800222T053600-0500", { 320063760, 0 } },
204 : : { TRUE, "1980-02-22T07:06:00-03:30", { 320063760, 0 } },
205 : : { TRUE, "1980-02-22T10:36:00.050000Z", { 320063760, 50000 } },
206 : : { TRUE, "1980-02-22T05:36:00,05-05:00", { 320063760, 50000 } },
207 : : { TRUE, "19800222T123600.050000000+0200", { 320063760, 50000 } },
208 : : { TRUE, "19800222T070600,0500-0330", { 320063760, 50000 } },
209 : : { FALSE, " ", { 0, 0 } },
210 : : { FALSE, "x", { 0, 0 } },
211 : : { FALSE, "123x", { 0, 0 } },
212 : : { FALSE, "2001-10+x", { 0, 0 } },
213 : : { FALSE, "1980-02-22", { 0, 0 } },
214 : : { FALSE, "1980-02-22T", { 0, 0 } },
215 : : { FALSE, "2001-10-08Tx", { 0, 0 } },
216 : : { FALSE, "2001-10-08T10:11x", { 0, 0 } },
217 : : { FALSE, "Wed Dec 19 17:20:20 GMT 2007", { 0, 0 } },
218 : : { FALSE, "1980-02-22T10:36:00Zulu", { 0, 0 } },
219 : : { FALSE, "2T0+819855292164632335", { 0, 0 } },
220 : : { FALSE, "1980-02-22", { 320063760, 50000 } },
221 : : { TRUE, "2018-08-03T14:08:05.446178377+01:00", { 1533301685, 446178 } },
222 : : { FALSE, "2147483648-08-03T14:08:05.446178377+01:00", { 0, 0 } },
223 : : { FALSE, "2018-13-03T14:08:05.446178377+01:00", { 0, 0 } },
224 : : { FALSE, "2018-00-03T14:08:05.446178377+01:00", { 0, 0 } },
225 : : { FALSE, "2018-08-00T14:08:05.446178377+01:00", { 0, 0 } },
226 : : { FALSE, "2018-08-32T14:08:05.446178377+01:00", { 0, 0 } },
227 : : { FALSE, "2018-08-03T24:08:05.446178377+01:00", { 0, 0 } },
228 : : { FALSE, "2018-08-03T14:60:05.446178377+01:00", { 0, 0 } },
229 : : { FALSE, "2018-08-03T14:08:63.446178377+01:00", { 0, 0 } },
230 : : { FALSE, "2018-08-03T14:08:05.446178377+100:00", { 0, 0 } },
231 : : { FALSE, "2018-08-03T14:08:05.446178377+01:60", { 0, 0 } },
232 : : { TRUE, "20180803T140805.446178377+0100", { 1533301685, 446178 } },
233 : : { FALSE, "21474836480803T140805.446178377+0100", { 0, 0 } },
234 : : { FALSE, "20181303T140805.446178377+0100", { 0, 0 } },
235 : : { FALSE, "20180003T140805.446178377+0100", { 0, 0 } },
236 : : { FALSE, "20180800T140805.446178377+0100", { 0, 0 } },
237 : : { FALSE, "20180832T140805.446178377+0100", { 0, 0 } },
238 : : { FALSE, "20180803T240805.446178377+0100", { 0, 0 } },
239 : : { FALSE, "20180803T146005.446178377+0100", { 0, 0 } },
240 : : { FALSE, "20180803T140863.446178377+0100", { 0, 0 } },
241 : : { FALSE, "20180803T140805.446178377+10000", { 0, 0 } },
242 : : { FALSE, "20180803T140805.446178377+0160", { 0, 0 } },
243 : : { TRUE, "+1980-02-22T12:36:00+02:00", { 320063760, 0 } },
244 : : { FALSE, "-0005-01-01T00:00:00Z", { 0, 0 } },
245 : : { FALSE, "2018-08-06", { 0, 0 } },
246 : : { FALSE, "2018-08-06 13:51:00Z", { 0, 0 } },
247 : : { TRUE, "20180803T140805,446178377+0100", { 1533301685, 446178 } },
248 : : { TRUE, "2018-08-03T14:08:05.446178377-01:00", { 1533308885, 446178 } },
249 : : { FALSE, "2018-08-03T14:08:05.446178377 01:00", { 0, 0 } },
250 : : { TRUE, "1990-11-01T10:21:17", { 657454877, 0 } },
251 : : { TRUE, "1990-11-01T10:21:17 ", { 657454877, 0 } },
252 : : };
253 : : GTimeVal out;
254 : : gboolean success;
255 : : gsize i;
256 : :
257 : : /* Always run in UTC so the comparisons of parsed values are valid. */
258 [ - + ]: 1 : if (!g_setenv ("TZ", "UTC", TRUE))
259 : : {
260 : 0 : g_test_skip ("Failed to set TZ=UTC");
261 : 0 : return;
262 : : }
263 : :
264 [ + + ]: 62 : for (i = 0; i < G_N_ELEMENTS (tests); i++)
265 : : {
266 : 61 : out.tv_sec = 0;
267 : 61 : out.tv_usec = 0;
268 : 61 : success = g_time_val_from_iso8601 (tests[i].in, &out);
269 : 61 : g_assert_cmpint (success, ==, tests[i].success);
270 [ + + ]: 61 : if (tests[i].success)
271 : : {
272 : 25 : g_assert_cmpint (out.tv_sec, ==, tests[i].val.tv_sec);
273 : 25 : g_assert_cmpint (out.tv_usec, ==, tests[i].val.tv_usec);
274 : : }
275 : : }
276 : :
277 : : /* revert back user defined time zone */
278 [ - + ]: 1 : if (old_tz != NULL)
279 : 0 : g_assert_true (g_setenv ("TZ", old_tz, TRUE));
280 : : else
281 : 1 : g_unsetenv ("TZ");
282 : 1 : tzset ();
283 : :
284 [ + + ]: 62 : for (i = 0; i < G_N_ELEMENTS (tests); i++)
285 : : {
286 : 61 : out.tv_sec = 0;
287 : 61 : out.tv_usec = 0;
288 : 61 : success = g_time_val_from_iso8601 (tests[i].in, &out);
289 : 61 : g_assert_cmpint (success, ==, tests[i].success);
290 : : }
291 : :
292 : 1 : g_free (old_tz);
293 : : }
294 : :
295 : : typedef struct {
296 : : GTimeVal val;
297 : : const gchar *expected;
298 : : } TimeValFormatTest;
299 : :
300 : : static void
301 : 1 : test_timeval_to_iso8601 (void)
302 : : {
303 : 1 : TimeValFormatTest tests[] = {
304 : : { { 657454877, 0 }, "1990-11-01T10:21:17Z" },
305 : : { { 17, 123400 }, "1970-01-01T00:00:17.123400Z" }
306 : : };
307 : : gsize i;
308 : : gchar *out;
309 : : GTimeVal val;
310 : : gboolean ret;
311 : :
312 : 1 : g_unsetenv ("TZ");
313 : :
314 [ + + ]: 3 : for (i = 0; i < G_N_ELEMENTS (tests); i++)
315 : : {
316 : 2 : out = g_time_val_to_iso8601 (&(tests[i].val));
317 : 2 : g_assert_cmpstr (out, ==, tests[i].expected);
318 : :
319 : 2 : ret = g_time_val_from_iso8601 (out, &val);
320 : 2 : g_assert (ret);
321 : 2 : g_assert_cmpint (val.tv_sec, ==, tests[i].val.tv_sec);
322 : 2 : g_assert_cmpint (val.tv_usec, ==, tests[i].val.tv_usec);
323 : 2 : g_free (out);
324 : : }
325 : 1 : }
326 : :
327 : : /* Test error handling for g_time_val_to_iso8601() on dates which are too large. */
328 : : static void
329 : 1 : test_timeval_to_iso8601_overflow (void)
330 : : {
331 : : GTimeVal val;
332 : 1 : gchar *out = NULL;
333 : :
334 : : if ((glong) G_MAXINT == G_MAXLONG)
335 : : {
336 : : g_test_skip ("G_MAXINT == G_MAXLONG - we can't make g_time_val_to_iso8601() overflow.");
337 : : return;
338 : : }
339 : :
340 : 1 : g_unsetenv ("TZ");
341 : :
342 : 1 : val.tv_sec = G_MAXLONG;
343 : 1 : val.tv_usec = G_USEC_PER_SEC - 1;
344 : :
345 : 1 : out = g_time_val_to_iso8601 (&val);
346 : 1 : g_assert_null (out);
347 : : }
348 : :
349 : : static void
350 : 1 : test_usleep_with_zero_wait (void)
351 : : {
352 : : GTimer *timer;
353 : 1 : unsigned int n_times_shorter = 0;
354 : :
355 : 1 : timer = g_timer_new ();
356 : :
357 : : /* Test that g_usleep(0) sleeps for less time than g_usleep(1). We can’t
358 : : * actually guarantee this, since the exact length of g_usleep(1) is not
359 : : * guaranteed, but we can say that it probably should be longer 9 times out
360 : : * of 10. */
361 [ + + ]: 11 : for (unsigned int i = 0; i < 10; i++)
362 : : {
363 : : gdouble elapsed0, elapsed1;
364 : :
365 : 10 : g_timer_start (timer);
366 : 10 : g_usleep (0);
367 : 10 : elapsed0 = g_timer_elapsed (timer, NULL);
368 : 10 : g_timer_stop (timer);
369 : :
370 : 10 : g_timer_start (timer);
371 : 10 : g_usleep (1);
372 : 10 : elapsed1 = g_timer_elapsed (timer, NULL);
373 : 10 : g_timer_stop (timer);
374 : :
375 [ + - ]: 10 : if (elapsed0 <= elapsed1)
376 : 10 : n_times_shorter++;
377 : : }
378 : :
379 : 1 : g_assert_cmpuint (n_times_shorter, >=, 9);
380 : :
381 : 1 : g_clear_pointer (&timer, g_timer_destroy);
382 : 1 : }
383 : :
384 : : int
385 : 1 : main (int argc, char *argv[])
386 : : {
387 : 1 : g_test_init (&argc, &argv, NULL);
388 : :
389 : 1 : g_test_add_func ("/timer/basic", test_timer_basic);
390 : 1 : g_test_add_func ("/timer/stop", test_timer_stop);
391 : 1 : g_test_add_func ("/timer/continue", test_timer_continue);
392 : 1 : g_test_add_func ("/timer/reset", test_timer_reset);
393 : 1 : g_test_add_func ("/timer/is_active", test_timer_is_active);
394 : 1 : g_test_add_func ("/timeval/add", test_timeval_add);
395 : 1 : g_test_add_func ("/timeval/from-iso8601", test_timeval_from_iso8601);
396 : 1 : g_test_add_func ("/timeval/to-iso8601", test_timeval_to_iso8601);
397 : 1 : g_test_add_func ("/timeval/to-iso8601/overflow", test_timeval_to_iso8601_overflow);
398 : 1 : g_test_add_func ("/usleep/with-zero-wait", test_usleep_with_zero_wait);
399 : :
400 : 1 : return g_test_run ();
401 : : }
|