tocsetter: fix memory leaks in unit test
[platform/upstream/gstreamer.git] / tests / check / gst / gsttocsetter.c
1 /* GStreamer GstTocSetter interface unit tests
2  * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21 #include <gst/gst.h>
22 #include <string.h>
23
24 #define ENTRY_ED1       "/edition1"
25 #define ENTRY_ED2       "/edition2"
26 #define ENTRY_ED3       "test-edition"
27
28 #define ENTRY_CH1       "/edition1/chapter1"
29 #define ENTRY_CH2       "/edition1/chapter2"
30 #define ENTRY_CH3       "/edition2/chapter3"
31 #define ENTRY_CH4       "/test-chapter"
32
33 #define ENTRY_SUB1      "/edition2/chapter3/subchapter1"
34
35 #define ENTRY_TAG       "EntryTag"
36 #define TOC_TAG         "TocTag"
37 #define INFO_NAME       "gst-toc-setter-check"
38 #define INFO_FIELD      "info-test"
39 #define INFO_TEXT_EN    "info-text-entry"
40 #define INFO_TEXT_TOC   "info-text-toc"
41
42 #define CHECK_TOC_ENTRY(entry_c,type_c,uid_c)                            \
43 {                                                                        \
44   gchar *tag_c;                                                          \
45   const GValue *val;                                                     \
46   GstStructure *struct_c;                                                \
47                                                                          \
48   fail_unless_equals_string (entry_c->uid, uid_c);                       \
49   fail_unless (entry_c->type == type_c);                                 \
50   fail_unless (entry_c->tags != NULL);                                   \
51   fail_unless (entry_c->pads == NULL);                                   \
52                                                                          \
53   fail_unless (entry_c->info != NULL);                                   \
54   gst_structure_get (entry_c->info, INFO_NAME, GST_TYPE_STRUCTURE,       \
55       &struct_c, NULL);                                                  \
56   fail_unless (struct_c != NULL);                                        \
57   val = gst_structure_get_value (struct_c, INFO_FIELD);             \
58   fail_unless (val != NULL);                                             \
59   fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_EN);    \
60                                                                          \
61   fail_unless (gst_tag_list_get_string (entry_c->tags,                   \
62                GST_TAG_TITLE, &tag_c));                                  \
63   fail_unless_equals_string (tag_c, ENTRY_TAG);                          \
64   g_free (tag_c);                                                        \
65   gst_structure_free (struct_c);                                         \
66 }
67
68 #define CHECK_TOC(toc_t)                                                 \
69 {                                                                        \
70   GstTocEntry *entry_t, *subentry_t;                                     \
71   gchar *tag_t;                                                          \
72   const GValue *val;                                                     \
73   GstStructure *struct_toc;                                              \
74                                                                          \
75   /* check TOC */                                                        \
76   fail_unless (g_list_length (toc_t->entries) == 2);                     \
77   fail_unless (toc_t->tags != NULL);                                     \
78   fail_unless (gst_tag_list_get_string (toc_t->tags,                     \
79                GST_TAG_TITLE, &tag_t));                                  \
80   fail_unless_equals_string (tag_t, TOC_TAG);                            \
81   g_free (tag_t);                                                        \
82                                                                          \
83   fail_unless (toc_t->info != NULL);                                     \
84   gst_structure_get (toc_t->info, INFO_NAME, GST_TYPE_STRUCTURE,         \
85       &struct_toc, NULL);                                                \
86   fail_unless (struct_toc != NULL);                                      \
87   val = gst_structure_get_value (struct_toc, INFO_FIELD);                \
88   fail_unless (val != NULL);                                             \
89   fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_TOC);   \
90   gst_structure_free (struct_toc);                                       \
91                                                                          \
92   /* check edition1 */                                                   \
93   entry_t = g_list_nth_data (toc_t->entries, 0);                         \
94   fail_if (entry_t == NULL);                                             \
95   fail_unless (g_list_length (entry_t->subentries) == 2);                \
96   CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);      \
97   /* check chapter1 */                                                   \
98   subentry_t = g_list_nth_data (entry_t->subentries, 0);                 \
99   fail_if (subentry_t == NULL);                                          \
100   fail_unless (g_list_length (subentry_t->subentries) == 0);             \
101   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);   \
102   /* check chapter2 */                                                   \
103   subentry_t = g_list_nth_data (entry_t->subentries, 1);                 \
104   fail_if (subentry_t == NULL);                                          \
105   fail_unless (g_list_length (subentry_t->subentries) == 0);             \
106   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);   \
107   /* check edition2 */                                                   \
108   entry_t = g_list_nth_data (toc_t->entries, 1);                         \
109   fail_if (entry_t == NULL);                                             \
110   fail_unless (g_list_length (entry_t->subentries) == 1);                \
111   CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);      \
112   /* check chapter3 */                                                   \
113   subentry_t = g_list_nth_data (entry_t->subentries, 0);                 \
114   fail_if (subentry_t == NULL);                                          \
115   fail_unless (g_list_length (subentry_t->subentries) == 1);             \
116   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);   \
117   /* check subchapter1 */                                                \
118   subentry_t = g_list_nth_data (subentry_t->subentries, 0);              \
119   fail_if (subentry_t == NULL);                                          \
120   fail_unless (g_list_length (subentry_t->subentries) == 0);             \
121   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);  \
122 }
123
124 /* some minimal GstTocSetter object */
125 #define GST_TYPE_DUMMY_ENC gst_dummy_enc_get_type()
126
127 typedef GstElement GstDummyEnc;
128 typedef GstElementClass GstDummyEncClass;
129
130 static void gst_dummy_enc_add_interfaces (GType enc_type);
131
132 GType gst_dummy_enc_get_type (void);
133 GST_BOILERPLATE_FULL (GstDummyEnc, gst_dummy_enc, GstElement,
134     GST_TYPE_ELEMENT, gst_dummy_enc_add_interfaces);
135
136 static void
137 gst_dummy_enc_add_interfaces (GType enc_type)
138 {
139   static const GInterfaceInfo toc_setter_info = { NULL, NULL, NULL };
140
141   g_type_add_interface_static (enc_type, GST_TYPE_TOC_SETTER, &toc_setter_info);
142 }
143
144 static void
145 gst_dummy_enc_base_init (gpointer g_class)
146 {
147 }
148
149 static void
150 gst_dummy_enc_class_init (GstDummyEncClass * klass)
151 {
152 }
153
154 static void
155 gst_dummy_enc_init (GstDummyEnc * enc, GstDummyEncClass * klass)
156 {
157 }
158
159 static GstToc *
160 create_toc (void)
161 {
162   GstStructure *structure;
163   GstToc *toc;
164   GstTocEntry *ed, *ch, *subch;
165
166   toc = gst_toc_new ();
167   gst_tag_list_add (toc->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
168       TOC_TAG, NULL);
169   structure =
170       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_TOC,
171       NULL);
172   gst_structure_set (toc->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
173   gst_structure_free (structure);
174
175   /* create edition1 */
176   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
177   gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
178       ENTRY_TAG, NULL);
179   structure =
180       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
181       NULL);
182   gst_structure_set (ed->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
183   gst_structure_free (structure);
184
185   /* append chapter1 to edition1 */
186   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
187   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
188       ENTRY_TAG, NULL);
189   structure =
190       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
191       NULL);
192   gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
193   gst_structure_free (structure);
194
195   ed->subentries = g_list_append (ed->subentries, ch);
196
197   /* append chapter2 to edition1 */
198   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
199   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
200       ENTRY_TAG, NULL);
201   structure =
202       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
203       NULL);
204   gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
205   gst_structure_free (structure);
206
207   ed->subentries = g_list_append (ed->subentries, ch);
208
209   /* append edition1 to the TOC */
210   toc->entries = g_list_append (toc->entries, ed);
211
212   /* create edition2 */
213   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
214   gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
215       ENTRY_TAG, NULL);
216   structure =
217       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
218       NULL);
219   gst_structure_set (ed->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
220   gst_structure_free (structure);
221
222   /* create chapter3 */
223   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
224   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
225       ENTRY_TAG, NULL);
226   structure =
227       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
228       NULL);
229   gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
230   gst_structure_free (structure);
231
232   /* create subchapter1 */
233   subch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
234   gst_tag_list_add (subch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
235       ENTRY_TAG, NULL);
236   structure =
237       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
238       NULL);
239   gst_structure_set (subch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure,
240       NULL);
241   gst_structure_free (structure);
242
243   /* append subchapter1 to chapter3 */
244   ch->subentries = g_list_append (ch->subentries, subch);
245
246   /* append chapter3 to edition2 */
247   ed->subentries = g_list_append (ed->subentries, ch);
248
249   /* finally append edition2 to the TOC */
250   toc->entries = g_list_append (toc->entries, ed);
251
252   return toc;
253 }
254
255 GST_START_TEST (test_set)
256 {
257   GstToc *toc;
258   GstTocEntry *entry, *ed;
259   GstTocSetter *setter;
260   GstElement *enc;
261
262   enc = g_object_new (GST_TYPE_DUMMY_ENC, NULL);
263   fail_unless (enc != NULL);
264
265   setter = GST_TOC_SETTER (enc);
266
267   toc = create_toc ();
268   fail_unless (toc != NULL);
269
270   gst_toc_setter_set_toc (setter, toc);
271
272   gst_toc_free (toc);
273   toc = gst_toc_setter_get_toc_copy (setter);
274
275   CHECK_TOC (toc);
276
277   /* test entry adding into the root TOC */
278   entry = g_list_last (toc->entries)->data;
279   toc->entries = g_list_remove (toc->entries, entry);
280
281   gst_toc_setter_set_toc (setter, toc);
282   gst_toc_setter_add_toc_entry (setter, "0", entry);
283
284   gst_toc_free (toc);
285   gst_toc_entry_free (entry);
286   toc = gst_toc_setter_get_toc_copy (setter);
287
288   CHECK_TOC (toc);
289
290   /* test entry adding into the arbitrary entry */
291   entry = gst_toc_find_entry (toc, ENTRY_CH2);
292   fail_if (entry == NULL);
293
294   ed = toc->entries->data;
295   ed->subentries = g_list_remove (ed->subentries, entry);
296
297   gst_toc_setter_add_toc_entry (setter, ed->uid, entry);
298
299   CHECK_TOC (toc);
300
301   gst_toc_free (toc);
302   gst_toc_setter_reset_toc (setter);
303   toc = gst_toc_setter_get_toc_copy (setter);
304
305   fail_unless (toc == NULL);
306
307   g_object_unref (enc);
308 }
309
310 GST_END_TEST static int spin_and_wait = 1;
311 static int threads_running = 0;
312
313 #define THREADS_TEST_SECONDS 1.5
314
315 static gpointer
316 test_threads_thread_func1 (gpointer data)
317 {
318   GstToc *toc;
319   GstTocSetter *setter = GST_TOC_SETTER (data);
320   GTimer *timer;
321
322   toc = create_toc ();
323   timer = g_timer_new ();
324
325   g_atomic_int_inc (&threads_running);
326   while (g_atomic_int_get (&spin_and_wait))
327     g_usleep (0);
328
329   GST_INFO ("Go!");
330   g_timer_start (timer);
331
332   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
333     gst_toc_setter_set_toc (setter, toc);
334
335   gst_toc_free (toc);
336   g_timer_destroy (timer);
337   GST_INFO ("Done");
338
339   return NULL;
340 }
341
342 static gpointer
343 test_threads_thread_func2 (gpointer data)
344 {
345   GstToc *toc;
346   GstTocSetter *setter = GST_TOC_SETTER (data);
347   GTimer *timer;
348
349   toc = create_toc ();
350   timer = g_timer_new ();
351
352   g_atomic_int_inc (&threads_running);
353   while (g_atomic_int_get (&spin_and_wait))
354     g_usleep (0);
355
356   GST_INFO ("Go!");
357   g_timer_start (timer);
358
359   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
360     gst_toc_setter_set_toc (setter, toc);
361
362   gst_toc_free (toc);
363   g_timer_destroy (timer);
364   GST_INFO ("Done");
365
366   return NULL;
367 }
368
369 static gpointer
370 test_threads_thread_func3 (gpointer data)
371 {
372   GstTocSetter *setter = GST_TOC_SETTER (data);
373   GTimer *timer;
374
375   timer = g_timer_new ();
376
377   g_atomic_int_inc (&threads_running);
378   while (g_atomic_int_get (&spin_and_wait))
379     g_usleep (0);
380
381   GST_INFO ("Go!");
382   g_timer_start (timer);
383
384   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS) {
385     gst_toc_setter_reset_toc (setter);
386   }
387
388   g_timer_destroy (timer);
389   GST_INFO ("Done");
390
391   return NULL;
392 }
393
394 GST_START_TEST (test_threads)
395 {
396   GstTocSetter *setter;
397   GThread *threads[3];
398
399   setter = GST_TOC_SETTER (g_object_new (GST_TYPE_DUMMY_ENC, NULL));
400
401   spin_and_wait = TRUE;
402   threads[0] = g_thread_create (test_threads_thread_func1, setter, TRUE, NULL);
403   threads[1] = g_thread_create (test_threads_thread_func2, setter, TRUE, NULL);
404   threads[2] = g_thread_create (test_threads_thread_func3, setter, TRUE, NULL);
405
406   while (g_atomic_int_get (&threads_running) < 3)
407     g_usleep (10);
408
409   g_atomic_int_set (&spin_and_wait, FALSE);
410
411   g_thread_join (threads[0]);
412   g_thread_join (threads[1]);
413   g_thread_join (threads[2]);
414
415   g_object_unref (G_OBJECT (setter));
416 }
417
418 GST_END_TEST static Suite *
419 gst_toc_setter_suite (void)
420 {
421   Suite *s = suite_create ("GstTocSetter");
422   TCase *tc_chain = tcase_create ("general");
423
424   suite_add_tcase (s, tc_chain);
425   tcase_add_test (tc_chain, test_set);
426   tcase_add_test (tc_chain, test_threads);
427
428   return s;
429 }
430
431 GST_CHECK_MAIN (gst_toc_setter);