tests/check/: use the new macro
[platform/upstream/gstreamer.git] / tests / check / gst / gstsystemclock.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstsystemclock.c: Unit test for GstSystemClock
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 GST_START_TEST (test_signedness)
25 {
26   GstClockTime time[] = { 0, 1, G_MAXUINT64 / GST_SECOND };
27   GstClockTimeDiff diff[] =
28       { 0, 1, -1, G_MAXINT64 / GST_SECOND, G_MININT64 / GST_SECOND };
29   guint i;
30
31   for (i = 0; i < G_N_ELEMENTS (time); i++) {
32     fail_if (time[i] != (time[i] * GST_SECOND / GST_SECOND));
33   }
34   for (i = 0; i < G_N_ELEMENTS (diff); i++) {
35     fail_if (diff[i] != (diff[i] * GST_SECOND / GST_SECOND));
36   }
37 }
38
39 GST_END_TEST
40 #define TIME_UNIT (GST_SECOND / 5)
41     static void
42 gst_clock_debug (GstClock * clock)
43 {
44   GstClockTime time;
45
46   time = gst_clock_get_time (clock);
47   g_message ("Clock info: time %" G_GUINT64_FORMAT "\n", time);
48 }
49
50 static gboolean
51 ok_callback (GstClock * clock, GstClockTime time,
52     GstClockID id, gpointer user_data)
53 {
54   g_message ("unlocked async id %p\n", id);
55   return FALSE;
56 }
57
58 static gboolean
59 error_callback (GstClock * clock, GstClockTime time,
60     GstClockID id, gpointer user_data)
61 {
62   g_message ("unlocked unscheduled async id %p, this is wrong\n", id);
63   fail_if (TRUE);
64
65   return FALSE;
66 }
67
68 GST_START_TEST (test_single_shot)
69 {
70   GstClock *clock;
71   GstClockID id, id2;
72   GstClockTime base;
73   GstClockReturn result;
74
75   clock = gst_system_clock_obtain ();
76   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
77
78   gst_clock_debug (clock);
79   base = gst_clock_get_time (clock);
80
81   id = gst_clock_new_single_shot_id (clock, base + TIME_UNIT);
82   fail_unless (id != NULL, "Could not create single shot id");
83
84   g_message ("waiting one second\n");
85   result = gst_clock_id_wait (id, NULL);
86   gst_clock_debug (clock);
87   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
88
89   g_message ("waiting in the past\n");
90   result = gst_clock_id_wait (id, NULL);
91   gst_clock_debug (clock);
92   fail_unless (result == GST_CLOCK_EARLY, "Waiting did not return EARLY");
93   gst_clock_id_unref (id);
94
95   id = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
96   g_message ("waiting one second async id %p\n", id);
97   result = gst_clock_id_wait_async (id, ok_callback, NULL);
98   gst_clock_id_unref (id);
99   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
100   g_usleep (TIME_UNIT / (2 * 1000));
101
102   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
103   g_message ("waiting one second async, with cancel on id %p\n", id);
104   result = gst_clock_id_wait_async (id, error_callback, NULL);
105   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
106   g_usleep (TIME_UNIT / (2 * 1000));
107   g_message ("cancel id %p after half a time unit\n", id);
108   gst_clock_id_unschedule (id);
109   gst_clock_id_unref (id);
110   g_message ("canceled id %p\n", id);
111
112   g_message ("waiting multiple one second async, with cancel\n");
113   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
114   id2 = gst_clock_new_single_shot_id (clock, base + 6 * TIME_UNIT);
115   g_message ("waiting id %p\n", id);
116   result = gst_clock_id_wait_async (id, ok_callback, NULL);
117   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
118   gst_clock_id_unref (id);
119   g_message ("waiting id %p\n", id2);
120   result = gst_clock_id_wait_async (id2, error_callback, NULL);
121   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
122   g_usleep (TIME_UNIT / (2 * 1000));
123   g_message ("cancel id %p after half a time unit\n", id2);
124   gst_clock_id_unschedule (id2);
125   g_message ("canceled id %p\n", id2);
126   gst_clock_id_unref (id2);
127   g_usleep (TIME_UNIT / (2 * 1000));
128 }
129
130 GST_END_TEST
131 GST_START_TEST (test_periodic_shot)
132 {
133   GstClock *clock;
134   GstClockID id, id2;
135   GstClockTime base;
136   GstClockReturn result;
137
138   clock = gst_system_clock_obtain ();
139   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
140
141   gst_clock_debug (clock);
142   base = gst_clock_get_time (clock);
143
144   /* signal every half a time unit */
145   id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
146   fail_unless (id != NULL, "Could not create periodic id");
147
148   g_message ("waiting one time unit\n");
149   result = gst_clock_id_wait (id, NULL);
150   gst_clock_debug (clock);
151   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
152
153   g_message ("waiting for the next\n");
154   result = gst_clock_id_wait (id, NULL);
155   gst_clock_debug (clock);
156   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
157
158   g_message ("waiting for the next async %p\n", id);
159   result = gst_clock_id_wait_async (id, ok_callback, NULL);
160   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
161   g_usleep (TIME_UNIT / (2 * 1000));
162
163   g_message ("waiting some more for the next async %p\n", id);
164   result = gst_clock_id_wait_async (id, ok_callback, NULL);
165   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
166   g_usleep (TIME_UNIT / (2 * 1000));
167
168   id2 = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
169   fail_unless (id2 != NULL, "Could not create second periodic id");
170
171   g_message ("waiting some more for another async %p\n", id2);
172   result = gst_clock_id_wait_async (id2, ok_callback, NULL);
173   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
174   g_usleep (TIME_UNIT / (2 * 1000));
175
176   g_message ("unschedule %p\n", id);
177   gst_clock_id_unschedule (id);
178
179   /* entry cannot be used again */
180   result = gst_clock_id_wait_async (id, error_callback, NULL);
181   fail_unless (result == GST_CLOCK_UNSCHEDULED,
182       "Waiting did not return UNSCHEDULED");
183   result = gst_clock_id_wait (id, NULL);
184   fail_unless (result == GST_CLOCK_UNSCHEDULED,
185       "Waiting did not return UNSCHEDULED");
186   g_usleep (TIME_UNIT / (2 * 1000));
187
188   /* clean up */
189   gst_clock_id_unref (id);
190 }
191
192 GST_END_TEST
193 GST_START_TEST (test_diff)
194 {
195   GstClockTime time1[] =
196       { 0, -1, 0, 1, 2 * GST_SECOND, -GST_SECOND, -GST_SECOND };
197   GstClockTime time2[] =
198       { 0, 1, 1, 0, 1 * GST_SECOND, -GST_SECOND, GST_SECOND };
199   GstClockTimeDiff d[] = { 0, 2, 1, -1, -GST_SECOND, 0, 2 * GST_SECOND };
200   guint i;
201
202   for (i = 0; i < G_N_ELEMENTS (d); i++) {
203     fail_if (d[i] != GST_CLOCK_DIFF (time1[i], time2[i]));
204   }
205 }
206 GST_END_TEST Suite *
207 gst_systemclock_suite (void)
208 {
209   Suite *s = suite_create ("GstSystemClock");
210   TCase *tc_chain = tcase_create ("waiting");
211
212   suite_add_tcase (s, tc_chain);
213   tcase_add_test (tc_chain, test_signedness);
214   tcase_add_test (tc_chain, test_single_shot);
215   tcase_add_test (tc_chain, test_periodic_shot);
216   tcase_add_test (tc_chain, test_diff);
217
218   return s;
219 }
220
221 GST_CHECK_MAIN (gst_systemclock);