Add some more cases to the app-id unit tests
[glib.git] / glib / tests / timer.c
blobcb9a2686ce6337767342f0ef560be7fbe606e361
1 /* Unit tests for GTimer
2 * Copyright (C) 2013 Red Hat, Inc.
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.
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.
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.
21 * Author: Matthias Clasen
24 #include "glib.h"
26 static void
27 test_timer_basic (void)
29 GTimer *timer;
30 gdouble elapsed;
31 gulong micros;
33 timer = g_timer_new ();
35 elapsed = g_timer_elapsed (timer, &micros);
37 g_assert_cmpfloat (elapsed, <, 1.0);
38 g_assert_cmpuint (micros, ==, ((guint64)(elapsed * 1e6)) % 1000000);
40 g_timer_destroy (timer);
43 static void
44 test_timer_stop (void)
46 GTimer *timer;
47 gdouble elapsed, elapsed2;
49 timer = g_timer_new ();
51 g_timer_stop (timer);
53 elapsed = g_timer_elapsed (timer, NULL);
54 g_usleep (100);
55 elapsed2 = g_timer_elapsed (timer, NULL);
57 g_assert_cmpfloat (elapsed, ==, elapsed2);
59 g_timer_destroy (timer);
62 static void
63 test_timer_continue (void)
65 GTimer *timer;
66 gdouble elapsed, elapsed2;
68 timer = g_timer_new ();
69 g_usleep (100);
70 g_timer_stop (timer);
72 elapsed = g_timer_elapsed (timer, NULL);
73 g_timer_continue (timer);
74 g_usleep (100);
75 elapsed2 = g_timer_elapsed (timer, NULL);
77 g_assert_cmpfloat (elapsed, <, elapsed2);
79 g_timer_destroy (timer);
82 static void
83 test_timer_reset (void)
85 GTimer *timer;
86 gdouble elapsed, elapsed2;
88 timer = g_timer_new ();
89 g_usleep (100);
90 g_timer_stop (timer);
92 elapsed = g_timer_elapsed (timer, NULL);
93 g_timer_reset (timer);
94 elapsed2 = g_timer_elapsed (timer, NULL);
96 g_assert_cmpfloat (elapsed, >, elapsed2);
98 g_timer_destroy (timer);
101 static void
102 test_timeval_add (void)
104 GTimeVal time = { 1, 0 };
106 g_time_val_add (&time, 10);
108 g_assert_cmpint (time.tv_sec, ==, 1);
109 g_assert_cmpint (time.tv_usec, ==, 10);
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);
115 g_time_val_add (&time, 1000);
116 g_assert_cmpint (time.tv_sec, ==, 1);
117 g_assert_cmpint (time.tv_usec, ==, 510);
120 typedef struct {
121 gboolean success;
122 const gchar *in;
123 GTimeVal val;
124 } TimeValParseTest;
126 static void
127 test_timeval_from_iso8601 (void)
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 } }
149 GTimeVal out;
150 gboolean success;
151 gint i;
153 g_unsetenv ("TZ");
155 for (i = 0; i < G_N_ELEMENTS (tests); i++)
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)
163 g_assert_cmpint (out.tv_sec, ==, tests[i].val.tv_sec);
164 g_assert_cmpint (out.tv_usec, ==, tests[i].val.tv_usec);
169 typedef struct {
170 GTimeVal val;
171 const gchar *expected;
172 } TimeValFormatTest;
174 static void
175 test_timeval_to_iso8601 (void)
177 TimeValFormatTest tests[] = {
178 { { 657454877, 0 }, "1990-11-01T10:21:17Z" },
179 { { 17, 123400 }, "1970-01-01T00:00:17.123400Z" }
181 gint i;
182 gchar *out;
183 GTimeVal val;
184 gboolean ret;
186 g_unsetenv ("TZ");
188 for (i = 0; i < G_N_ELEMENTS (tests); i++)
190 out = g_time_val_to_iso8601 (&(tests[i].val));
191 g_assert_cmpstr (out, ==, tests[i].expected);
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);
202 main (int argc, char *argv[])
204 g_test_init (&argc, &argv, NULL);
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);
214 return g_test_run ();