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
27 test_timer_basic (void)
33 timer
= g_timer_new ();
35 elapsed
= g_timer_elapsed (timer
, µs
);
37 g_assert_cmpfloat (elapsed
, <, 1.0);
38 g_assert_cmpuint (micros
, ==, ((guint64
)(elapsed
* 1e6
)) % 1000000);
40 g_timer_destroy (timer
);
44 test_timer_stop (void)
47 gdouble elapsed
, elapsed2
;
49 timer
= g_timer_new ();
53 elapsed
= g_timer_elapsed (timer
, NULL
);
55 elapsed2
= g_timer_elapsed (timer
, NULL
);
57 g_assert_cmpfloat (elapsed
, ==, elapsed2
);
59 g_timer_destroy (timer
);
63 test_timer_continue (void)
66 gdouble elapsed
, elapsed2
;
68 timer
= g_timer_new ();
72 elapsed
= g_timer_elapsed (timer
, NULL
);
73 g_timer_continue (timer
);
75 elapsed2
= g_timer_elapsed (timer
, NULL
);
77 g_assert_cmpfloat (elapsed
, <, elapsed2
);
79 g_timer_destroy (timer
);
83 test_timer_reset (void)
86 gdouble elapsed
, elapsed2
;
88 timer
= g_timer_new ();
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
);
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);
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 } }
155 for (i
= 0; i
< G_N_ELEMENTS (tests
); i
++)
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
);
171 const gchar
*expected
;
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" }
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
);
195 g_assert_cmpint (val
.tv_sec
, ==, tests
[i
].val
.tv_sec
);
196 g_assert_cmpint (val
.tv_usec
, ==, tests
[i
].val
.tv_usec
);
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 ();