Simplify code that uses g_queue_insert_before() and insert_after()
[platform/upstream/glib.git] / glib / tests / timer.c
1 /* Unit tests for GTimer
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
4  * This work is provided "as is"; redistribution and modification
5  * in whole or in part, in any medium, physical or electronic is
6  * permitted without restriction.
7  *
8  * This work is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * In no event shall the authors or contributors be liable for any
13  * direct, indirect, incidental, special, exemplary, or consequential
14  * damages (including, but not limited to, procurement of substitute
15  * goods or services; loss of use, data, or profits; or business
16  * interruption) however caused and on any theory of liability, whether
17  * in contract, strict liability, or tort (including negligence or
18  * otherwise) arising in any way out of the use of this software, even
19  * if advised of the possibility of such damage.
20  *
21  * Author: Matthias Clasen
22  */
23
24 #include "glib.h"
25
26 static void
27 test_timer_basic (void)
28 {
29   GTimer *timer;
30   gdouble elapsed;
31   gulong micros;
32
33   timer = g_timer_new ();
34
35   elapsed = g_timer_elapsed (timer, &micros);
36
37   g_assert_cmpfloat (elapsed, <, 1.0);
38   g_assert_cmpuint (micros, ==, ((guint64)(elapsed * 1e6)) % 1000000);
39
40   g_timer_destroy (timer);
41 }
42
43 static void
44 test_timer_stop (void)
45 {
46   GTimer *timer;
47   gdouble elapsed, elapsed2;
48
49   timer = g_timer_new ();
50
51   g_timer_stop (timer);
52
53   elapsed = g_timer_elapsed (timer, NULL);
54   g_usleep (100);
55   elapsed2 = g_timer_elapsed (timer, NULL);
56
57   g_assert_cmpfloat (elapsed, ==, elapsed2);
58
59   g_timer_destroy (timer);
60 }
61
62 static void
63 test_timer_continue (void)
64 {
65   GTimer *timer;
66   gdouble elapsed, elapsed2;
67
68   timer = g_timer_new ();
69   g_usleep (100);
70   g_timer_stop (timer);
71
72   elapsed = g_timer_elapsed (timer, NULL);
73   g_timer_continue (timer);
74   g_usleep (100);
75   elapsed2 = g_timer_elapsed (timer, NULL);
76
77   g_assert_cmpfloat (elapsed, <, elapsed2);
78
79   g_timer_destroy (timer);
80 }
81
82 static void
83 test_timer_reset (void)
84 {
85   GTimer *timer;
86   gdouble elapsed, elapsed2;
87
88   timer = g_timer_new ();
89   g_usleep (100);
90   g_timer_stop (timer);
91
92   elapsed = g_timer_elapsed (timer, NULL);
93   g_timer_reset (timer);
94   elapsed2 = g_timer_elapsed (timer, NULL);
95
96   g_assert_cmpfloat (elapsed, >, elapsed2);
97
98   g_timer_destroy (timer);
99 }
100
101 static void
102 test_timeval_add (void)
103 {
104   GTimeVal time = { 1, 0 };
105
106   g_time_val_add (&time, 10);
107
108   g_assert_cmpint (time.tv_sec, ==, 1); 
109   g_assert_cmpint (time.tv_usec, ==, 10); 
110
111   g_time_val_add (&time, -500);
112   g_assert_cmpint (time.tv_sec, ==, 0); 
113   g_assert_cmpint (time.tv_usec, ==, G_USEC_PER_SEC - 490); 
114
115   g_time_val_add (&time, 1000);
116   g_assert_cmpint (time.tv_sec, ==, 1); 
117   g_assert_cmpint (time.tv_usec, ==, 510);
118 }
119
120 typedef struct {
121   gboolean success;
122   const gchar *in;
123   GTimeVal val;
124 } TimeValParseTest;
125
126 static void
127 test_timeval_from_iso8601 (void)
128 {
129   TimeValParseTest tests[] = {
130     { TRUE, "1990-11-01T10:21:17Z", { 657454877, 0 } },
131     { TRUE, "19901101T102117Z", { 657454877, 0 } },
132     { TRUE, "19901101T102117+5", { 657454577, 0 } },
133     { TRUE, "19901101T102117+3:15", { 657443177, 0 } },
134     { TRUE, "  1990-11-01T10:21:17Z  ", { 657454877, 0 } },
135     { TRUE, "1970-01-01T00:00:17.12Z", { 17, 120000 } },
136     { TRUE, "1970-01-01T00:00:17.1234Z", { 17, 123400 } },
137     { TRUE, "1970-01-01T00:00:17.123456Z", { 17, 123456 } },
138     { TRUE, "1980-02-22T12:36:00+02:00", { 320063760, 0 } },
139     { FALSE, "   ", { 0, 0 } },
140     { FALSE, "x", { 0, 0 } },
141     { FALSE, "123x", { 0, 0 } },
142     { FALSE, "2001-10+x", { 0, 0 } },
143     { FALSE, "1980-02-22T", { 0, 0 } },
144     { FALSE, "2001-10-08Tx", { 0, 0 } },
145     { FALSE, "2001-10-08T10:11x", { 0, 0 } },
146     { FALSE, "Wed Dec 19 17:20:20 GMT 2007", { 0, 0 } },
147     { FALSE, "1980-02-22T10:36:00Zulu", { 0, 0 } }
148   };
149   GTimeVal out;
150   gboolean success;
151   gint i;
152
153   g_unsetenv ("TZ");
154
155   for (i = 0; i < G_N_ELEMENTS (tests); i++)
156     {
157       out.tv_sec = 0;
158       out.tv_usec = 0;
159       success = g_time_val_from_iso8601 (tests[i].in, &out);
160       g_assert (success == tests[i].success);
161       if (tests[i].success)
162         {
163           g_assert_cmpint (out.tv_sec, ==, tests[i].val.tv_sec);
164           g_assert_cmpint (out.tv_usec, ==, tests[i].val.tv_usec);
165         }
166     }
167 }
168
169 typedef struct {
170   GTimeVal val;
171   const gchar *expected;
172 } TimeValFormatTest;
173
174 static void
175 test_timeval_to_iso8601 (void)
176 {
177   TimeValFormatTest tests[] = {
178     { { 657454877, 0 }, "1990-11-01T10:21:17Z" },
179     { { 17, 123400 }, "1970-01-01T00:00:17.123400Z" }
180   };
181   gint i;
182   gchar *out;
183   GTimeVal val;
184   gboolean ret;
185
186   g_unsetenv ("TZ");
187
188   for (i = 0; i < G_N_ELEMENTS (tests); i++)
189     {
190       out = g_time_val_to_iso8601 (&(tests[i].val));
191       g_assert_cmpstr (out, ==, tests[i].expected);
192
193       ret = g_time_val_from_iso8601 (out, &val);
194       g_assert (ret);
195       g_assert_cmpint (val.tv_sec, ==, tests[i].val.tv_sec);
196       g_assert_cmpint (val.tv_usec, ==, tests[i].val.tv_usec);
197       g_free (out);
198     }
199 }
200
201 int
202 main (int argc, char *argv[])
203 {
204   g_test_init (&argc, &argv, NULL);
205
206   g_test_add_func ("/timer/basic", test_timer_basic);
207   g_test_add_func ("/timer/stop", test_timer_stop);
208   g_test_add_func ("/timer/continue", test_timer_continue);
209   g_test_add_func ("/timer/reset", test_timer_reset);
210   g_test_add_func ("/timeval/add", test_timeval_add);
211   g_test_add_func ("/timeval/from-iso8601", test_timeval_from_iso8601);
212   g_test_add_func ("/timeval/to-iso8601", test_timeval_to_iso8601);
213
214   return g_test_run ();
215 }