tests/gst-launch: Fix sample memory leak
[platform/upstream/gstreamer.git] / tests / check / gst / gsttag.c
1 /*
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21
22 #include <string.h>
23
24 /* multiple artists are possible (unfixed) */
25 #define UTAG GST_TAG_ARTIST
26 #define UNFIXED1 "Britney Spears"
27 #define UNFIXED2 "Evanescence"
28 #define UNFIXED3 "AC/DC"
29 #define UNFIXED4 "The Prodigy"
30
31 /* license is fixed */
32 #define FTAG GST_TAG_LICENSE
33 #define FIXED1 "Lesser General Public License"
34 #define FIXED2 "Microsoft End User License Agreement"
35 #define FIXED3 "Mozilla Public License"
36 #define FIXED4 "Public Domain"
37
38 /* checks that a tag contains the given values and not more values */
39 static void
40 check_tags (const GstTagList * list, const gchar * tag, const gchar * value,
41     ...)
42 {
43   va_list args;
44   gchar *str;
45   guint i = 0;
46
47   va_start (args, value);
48   while (value != NULL) {
49     fail_unless (gst_tag_list_get_string_index (list, tag, i, &str));
50     fail_unless (strcmp (value, str) == 0);
51     g_free (str);
52
53     value = va_arg (args, gchar *);
54     i++;
55   }
56   fail_unless (i == gst_tag_list_get_tag_size (list, tag));
57   va_end (args);
58 }
59
60 static void
61 check_tags_empty (const GstTagList * list)
62 {
63   GST_DEBUG ("taglist: %" GST_PTR_FORMAT, list);
64   fail_unless ((list == NULL) || (gst_tag_list_is_empty (list)));
65 }
66
67 #define NEW_LIST_FIXED(mode)                                    \
68 G_STMT_START {                                                  \
69   if (list) gst_tag_list_unref (list);                          \
70   list = gst_tag_list_new_empty ();                             \
71   gst_tag_list_add (list, mode, FTAG, FIXED1, FTAG, FIXED2,     \
72                     FTAG, FIXED3, FTAG, FIXED4, NULL);          \
73   mark_point();                                                 \
74 } G_STMT_END;
75
76 #define NEW_LIST_UNFIXED(mode)                                  \
77 G_STMT_START {                                                  \
78   if (list) gst_tag_list_unref (list);                          \
79   list = gst_tag_list_new_empty ();                             \
80   gst_tag_list_add (list, mode, UTAG, UNFIXED1, UTAG, UNFIXED2, \
81                     UTAG, UNFIXED3, UTAG, UNFIXED4, NULL);      \
82   mark_point();                                                 \
83 } G_STMT_END;
84
85 #define NEW_LISTS_FIXED(mode)                                   \
86 G_STMT_START {                                                  \
87   if (list) gst_tag_list_unref (list);                          \
88   list = gst_tag_list_new_empty ();                             \
89   gst_tag_list_add (list, GST_TAG_MERGE_APPEND, FTAG, FIXED1,   \
90                     FTAG, FIXED2, NULL);                        \
91   if (list2) gst_tag_list_unref (list2);                        \
92   list2 = gst_tag_list_new_empty ();                            \
93   gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, FTAG, FIXED3,  \
94                     FTAG, FIXED4, NULL);                        \
95   if (merge) gst_tag_list_unref (merge);                        \
96   merge = gst_tag_list_merge (list, list2, mode);               \
97   mark_point();                                                 \
98 } G_STMT_END;
99
100 #define NEW_LISTS_UNFIXED(mode)                                 \
101 G_STMT_START {                                                  \
102   if (list) gst_tag_list_unref (list);                          \
103   list = gst_tag_list_new_empty ();                             \
104   gst_tag_list_add (list, GST_TAG_MERGE_APPEND, UTAG, UNFIXED1, \
105                     UTAG, UNFIXED2, NULL);                      \
106   if (list2) gst_tag_list_unref (list2);                        \
107   list2 = gst_tag_list_new_empty ();                            \
108   gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, UTAG, UNFIXED3,\
109                     UTAG, UNFIXED4, NULL);                      \
110   if (merge) gst_tag_list_unref (merge);                        \
111   merge = gst_tag_list_merge (list, list2, mode);               \
112   mark_point();                                                 \
113 } G_STMT_END;
114
115 #define NEW_LISTS_EMPTY1(mode)                                  \
116 G_STMT_START {                                                  \
117   if (list) gst_tag_list_unref (list);                          \
118   list = NULL;                                                  \
119   if (list2) gst_tag_list_unref (list2);                        \
120   list2 = gst_tag_list_new_empty ();                            \
121   gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, FTAG, FIXED3,  \
122                     FTAG, FIXED4, NULL);                        \
123   if (merge) gst_tag_list_unref (merge);                        \
124   merge = gst_tag_list_merge (list, list2, mode);               \
125   mark_point();                                                 \
126 } G_STMT_END;
127
128 #define NEW_LISTS_EMPTY2(mode)                                  \
129 G_STMT_START {                                                  \
130   if (list) gst_tag_list_unref (list);                          \
131   list = gst_tag_list_new_empty ();                             \
132   gst_tag_list_add (list, GST_TAG_MERGE_APPEND, FTAG, FIXED1,   \
133                     FTAG, FIXED2, NULL);                        \
134   if (list2) gst_tag_list_unref (list2);                        \
135   list2 = NULL;                                                 \
136   if (merge) gst_tag_list_unref (merge);                        \
137   merge = gst_tag_list_merge (list, list2, mode);               \
138   mark_point();                                                 \
139 } G_STMT_END;
140
141
142 GST_START_TEST (test_basics)
143 {
144   /* make sure the assumptions work */
145   fail_unless (gst_tag_is_fixed (FTAG));
146   fail_unless (!gst_tag_is_fixed (UTAG));
147   /* we check string here only */
148   fail_unless (gst_tag_get_type (FTAG) == G_TYPE_STRING);
149   fail_unless (gst_tag_get_type (UTAG) == G_TYPE_STRING);
150 }
151
152 GST_END_TEST
153 GST_START_TEST (test_add)
154 {
155   GstTagList *list = NULL;
156
157   /* check additions */
158   /* unfixed */
159   NEW_LIST_UNFIXED (GST_TAG_MERGE_REPLACE_ALL);
160   check_tags (list, UTAG, UNFIXED4, NULL);
161   NEW_LIST_UNFIXED (GST_TAG_MERGE_REPLACE);
162   check_tags (list, UTAG, UNFIXED4, NULL);
163   NEW_LIST_UNFIXED (GST_TAG_MERGE_PREPEND);
164   check_tags (list, UTAG, UNFIXED4, UNFIXED3, UNFIXED2, UNFIXED1, NULL);
165   NEW_LIST_UNFIXED (GST_TAG_MERGE_APPEND);
166   check_tags (list, UTAG, UNFIXED1, UNFIXED2, UNFIXED3, UNFIXED4, NULL);
167   NEW_LIST_UNFIXED (GST_TAG_MERGE_KEEP);
168   check_tags (list, UTAG, UNFIXED1, NULL);
169   NEW_LIST_UNFIXED (GST_TAG_MERGE_KEEP_ALL);
170   check_tags (list, UTAG, NULL);
171
172   /* fixed */
173   NEW_LIST_FIXED (GST_TAG_MERGE_REPLACE_ALL);
174   check_tags (list, FTAG, FIXED4, NULL);
175   NEW_LIST_FIXED (GST_TAG_MERGE_REPLACE);
176   check_tags (list, FTAG, FIXED4, NULL);
177   NEW_LIST_FIXED (GST_TAG_MERGE_PREPEND);
178   check_tags (list, FTAG, FIXED4, NULL);
179   NEW_LIST_FIXED (GST_TAG_MERGE_APPEND);
180   check_tags (list, FTAG, FIXED1, NULL);
181   NEW_LIST_FIXED (GST_TAG_MERGE_KEEP);
182   check_tags (list, FTAG, FIXED1, NULL);
183   NEW_LIST_FIXED (GST_TAG_MERGE_KEEP_ALL);
184   check_tags (list, FTAG, NULL);
185
186   /* clean up */
187   if (list)
188     gst_tag_list_unref (list);
189 }
190
191 GST_END_TEST
192 GST_START_TEST (test_merge)
193 {
194   GstTagList *list = NULL, *list2 = NULL, *merge = NULL;
195
196   /* check merging */
197   /* unfixed */
198   GST_DEBUG ("unfixed");
199   NEW_LISTS_UNFIXED (GST_TAG_MERGE_REPLACE_ALL);
200   check_tags (merge, UTAG, UNFIXED3, UNFIXED4, NULL);
201   NEW_LISTS_UNFIXED (GST_TAG_MERGE_REPLACE);
202   check_tags (merge, UTAG, UNFIXED3, UNFIXED4, NULL);
203   NEW_LISTS_UNFIXED (GST_TAG_MERGE_PREPEND);
204   check_tags (merge, UTAG, UNFIXED3, UNFIXED4, UNFIXED1, UNFIXED2, NULL);
205   NEW_LISTS_UNFIXED (GST_TAG_MERGE_APPEND);
206   check_tags (merge, UTAG, UNFIXED1, UNFIXED2, UNFIXED3, UNFIXED4, NULL);
207   NEW_LISTS_UNFIXED (GST_TAG_MERGE_KEEP);
208   check_tags (merge, UTAG, UNFIXED1, UNFIXED2, NULL);
209   NEW_LISTS_UNFIXED (GST_TAG_MERGE_KEEP_ALL);
210   check_tags (merge, UTAG, UNFIXED1, UNFIXED2, NULL);
211
212   /* fixed */
213   GST_DEBUG ("fixed");
214   NEW_LISTS_FIXED (GST_TAG_MERGE_REPLACE_ALL);
215   check_tags (merge, FTAG, FIXED3, NULL);
216   NEW_LISTS_FIXED (GST_TAG_MERGE_REPLACE);
217   check_tags (merge, FTAG, FIXED3, NULL);
218   NEW_LISTS_FIXED (GST_TAG_MERGE_PREPEND);
219   check_tags (merge, FTAG, FIXED3, NULL);
220   NEW_LISTS_FIXED (GST_TAG_MERGE_APPEND);
221   check_tags (merge, FTAG, FIXED1, NULL);
222   NEW_LISTS_FIXED (GST_TAG_MERGE_KEEP);
223   check_tags (merge, FTAG, FIXED1, NULL);
224   NEW_LISTS_FIXED (GST_TAG_MERGE_KEEP_ALL);
225   check_tags (merge, FTAG, FIXED1, NULL);
226
227   /* first list empty */
228   GST_DEBUG ("first empty");
229   NEW_LISTS_EMPTY1 (GST_TAG_MERGE_REPLACE_ALL);
230   check_tags (merge, FTAG, FIXED3, NULL);
231   NEW_LISTS_EMPTY1 (GST_TAG_MERGE_REPLACE);
232   check_tags (merge, FTAG, FIXED3, NULL);
233   NEW_LISTS_EMPTY1 (GST_TAG_MERGE_PREPEND);
234   check_tags (merge, FTAG, FIXED3, NULL);
235   NEW_LISTS_EMPTY1 (GST_TAG_MERGE_APPEND);
236   check_tags (merge, FTAG, FIXED3, NULL);
237   NEW_LISTS_EMPTY1 (GST_TAG_MERGE_KEEP);
238   check_tags (merge, FTAG, FIXED3, NULL);
239   NEW_LISTS_EMPTY1 (GST_TAG_MERGE_KEEP_ALL);
240   check_tags_empty (merge);
241
242   /* second list empty */
243   GST_DEBUG ("second empty");
244   NEW_LISTS_EMPTY2 (GST_TAG_MERGE_REPLACE_ALL);
245   check_tags_empty (merge);
246   NEW_LISTS_EMPTY2 (GST_TAG_MERGE_REPLACE);
247   check_tags (merge, FTAG, FIXED1, NULL);
248   NEW_LISTS_EMPTY2 (GST_TAG_MERGE_PREPEND);
249   check_tags (merge, FTAG, FIXED1, NULL);
250   NEW_LISTS_EMPTY2 (GST_TAG_MERGE_APPEND);
251   check_tags (merge, FTAG, FIXED1, NULL);
252   NEW_LISTS_EMPTY2 (GST_TAG_MERGE_KEEP);
253   check_tags (merge, FTAG, FIXED1, NULL);
254   NEW_LISTS_EMPTY2 (GST_TAG_MERGE_KEEP_ALL);
255   check_tags (merge, FTAG, FIXED1, NULL);
256
257   /* clean up */
258   if (list)
259     gst_tag_list_unref (list);
260   if (list2)
261     gst_tag_list_unref (list2);
262   if (merge)
263     gst_tag_list_unref (merge);
264 }
265
266 GST_END_TEST
267 GST_START_TEST (test_date_tags)
268 {
269   GstTagList *tag_list, *tag_list2;
270   GDate *date, *date2;
271   gchar *str;
272
273   date = g_date_new_dmy (14, 10, 2005);
274   tag_list = gst_tag_list_new_empty ();
275   gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_DATE, date, NULL);
276
277   str = gst_tag_list_to_string (tag_list);
278   fail_if (str == NULL);
279   fail_if (strstr (str, "2005-10-14") == NULL);
280
281   tag_list2 = gst_tag_list_new_from_string (str);
282   fail_if (tag_list2 == NULL);
283   fail_if (!gst_tag_list_get_date (tag_list2, GST_TAG_DATE, &date2));
284   fail_unless (gst_tag_list_is_equal (tag_list2, tag_list));
285   gst_tag_list_unref (tag_list2);
286   g_free (str);
287
288   fail_if (g_date_compare (date, date2) != 0);
289   fail_if (g_date_get_day (date) != 14);
290   fail_if (g_date_get_month (date) != 10);
291   fail_if (g_date_get_year (date) != 2005);
292   fail_if (g_date_get_day (date2) != 14);
293   fail_if (g_date_get_month (date2) != 10);
294   fail_if (g_date_get_year (date2) != 2005);
295   g_date_free (date2);
296
297   gst_tag_list_unref (tag_list);
298   g_date_free (date);
299 }
300
301 GST_END_TEST;
302
303 GST_START_TEST (test_type)
304 {
305   GstTagList *taglist;
306
307   taglist = gst_tag_list_new_empty ();
308   fail_unless (GST_IS_TAG_LIST (taglist));
309   gst_tag_list_unref (taglist);
310
311   /* this should be fine */
312   fail_if (GST_IS_TAG_LIST (NULL));
313
314   /* check gst_tag_list_is_empty */
315   ASSERT_CRITICAL (gst_tag_list_is_empty (NULL));
316   taglist = gst_tag_list_new_empty ();
317   fail_unless (gst_tag_list_is_empty (taglist));
318   gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "JD", NULL);
319   fail_if (gst_tag_list_is_empty (taglist));
320   gst_tag_list_unref (taglist);
321 }
322
323 GST_END_TEST;
324
325 GST_START_TEST (test_set_non_utf8_string)
326 {
327   GstTagList *taglist;
328   guint8 foobar[2] = { 0xff, 0x00 };    /* not UTF-8 */
329
330   taglist = gst_tag_list_new_empty ();
331   fail_unless (taglist != NULL);
332
333   ASSERT_WARNING (gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
334           GST_TAG_ARTIST, (gchar *) foobar, NULL));
335
336   /* That string field with a non-UTF8 string should not have been added */
337   fail_unless (gst_tag_list_is_empty (taglist));
338
339   gst_tag_list_unref (taglist);
340 }
341
342 GST_END_TEST;
343
344 GST_START_TEST (test_buffer_tags)
345 {
346   GstTagList *tags;
347   GstBuffer *buf1, *buf2;
348   GstSample *s1, *s2;
349
350   tags = gst_tag_list_new_empty ();
351
352   buf1 = gst_buffer_new_and_alloc (222);
353   s1 = gst_sample_new (buf1, NULL, NULL, NULL);
354   gst_buffer_unref (buf1);
355
356   buf2 = gst_buffer_new_and_alloc (100);
357   s2 = gst_sample_new (buf2, NULL, NULL, NULL);
358   gst_buffer_unref (buf2);
359
360   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, s1,
361       GST_TAG_PREVIEW_IMAGE, s2, NULL);
362
363   gst_sample_unref (s1);
364   gst_sample_unref (s2);
365   s1 = s2 = NULL;
366
367   fail_if (!gst_tag_list_get_sample (tags, GST_TAG_IMAGE, &s1));
368   fail_unless (gst_sample_get_buffer (s1) == buf1);
369   gst_sample_unref (s1);
370
371   fail_if (!gst_tag_list_get_sample (tags, GST_TAG_PREVIEW_IMAGE, &s2));
372   fail_unless (gst_sample_get_buffer (s2) == buf2);
373   gst_sample_unref (s2);
374
375   fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_IMAGE, 1, &s1));
376   gst_sample_unref (s1);
377   fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_IMAGE, 2, &s1));
378   gst_sample_unref (s1);
379   fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_PREVIEW_IMAGE, 1, &s1));
380   gst_sample_unref (s1);
381   fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_PREVIEW_IMAGE, 2, &s1));
382   gst_sample_unref (s1);
383
384   fail_if (!gst_tag_list_get_sample_index (tags, GST_TAG_IMAGE, 0, &s1));
385   fail_if (!gst_tag_list_get_sample_index (tags, GST_TAG_PREVIEW_IMAGE, 0,
386           &s2));
387   buf1 = gst_sample_get_buffer (s1);
388   fail_unless_equals_int (gst_buffer_get_size (buf1), 222);
389   buf2 = gst_sample_get_buffer (s2);
390   fail_unless_equals_int (gst_buffer_get_size (buf2), 100);
391
392   gst_sample_unref (s1);
393   gst_sample_unref (s2);
394
395   gst_tag_list_unref (tags);
396 }
397
398 GST_END_TEST;
399
400 GST_START_TEST (test_empty_tags)
401 {
402   GstTagList *tags;
403
404   /* only get g_warnings() with git */
405   if (GST_VERSION_NANO != 1)
406     return;
407
408   tags = gst_tag_list_new_empty ();
409   ASSERT_WARNING (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
410           GST_TAG_ARTIST, NULL, NULL));
411   ASSERT_WARNING (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
412           GST_TAG_ARTIST, "", NULL));
413   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "xyz", NULL);
414   gst_tag_list_unref (tags);
415 }
416
417 GST_END_TEST;
418
419 GST_START_TEST (test_new_full)
420 {
421   GstTagList *tags;
422   gchar *artist, *title;
423   gdouble track_gain;
424   guint track_num;
425
426   tags = gst_tag_list_new (GST_TAG_ARTIST, "Arty Ist",
427       GST_TAG_TRACK_NUMBER, 9, GST_TAG_TRACK_GAIN, 4.242, GST_TAG_TITLE,
428       "Title!", NULL);
429
430   fail_unless (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &artist));
431   fail_unless_equals_string (artist, "Arty Ist");
432   fail_unless (gst_tag_list_get_string (tags, GST_TAG_TITLE, &title));
433   fail_unless_equals_string (title, "Title!");
434   fail_unless (gst_tag_list_get_uint (tags, GST_TAG_TRACK_NUMBER, &track_num));
435   fail_unless_equals_int (track_num, 9);
436   fail_unless (gst_tag_list_get_double (tags, GST_TAG_TRACK_GAIN, &track_gain));
437   fail_unless_equals_float (track_gain, 4.242);
438   fail_unless (tags != NULL);
439
440   gst_tag_list_unref (tags);
441   g_free (artist);
442   g_free (title);
443 }
444
445 GST_END_TEST;
446
447 GST_START_TEST (test_merge_strings_with_comma)
448 {
449   GstTagList *tags;
450   gchar *artists = NULL;
451
452   tags = gst_tag_list_new_empty ();
453   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
454   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
455   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Yay", NULL);
456   gst_tag_list_get_string (tags, GST_TAG_ARTIST, &artists);
457   fail_unless (artists != NULL);
458   /* can't check for exact string since the comma separator is i18n-ed */
459   fail_unless (strstr (artists, "Foo") != NULL);
460   fail_unless (strstr (artists, "Bar") != NULL);
461   fail_unless (strstr (artists, "Yay") != NULL);
462   g_free (artists);
463   gst_tag_list_unref (tags);
464 }
465
466 GST_END_TEST;
467
468 GST_START_TEST (test_equal)
469 {
470   GstTagList *tags, *tags2;
471   GstSample *sample1, *sample2, *sample11;
472   GstBuffer *buf;
473
474   tags = gst_tag_list_new_empty ();
475   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
476   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
477   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Yay", NULL);
478
479   tags2 = gst_tag_list_new_empty ();
480   fail_unless (!gst_tag_list_is_equal (tags2, tags));
481   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Yay", NULL);
482   fail_unless (!gst_tag_list_is_equal (tags2, tags));
483   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
484   fail_unless (!gst_tag_list_is_equal (tags2, tags));
485   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
486   fail_unless (gst_tag_list_is_equal (tags2, tags));
487
488   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_REFERENCE_LEVEL,
489       9.87654321, NULL);
490   fail_unless (!gst_tag_list_is_equal (tags2, tags));
491   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_REFERENCE_LEVEL,
492       9.87654320, NULL);
493   /* want these two double values to be equal despite minor differences */
494   fail_unless (gst_tag_list_is_equal (tags2, tags));
495
496   /* want this to be unequal though, difference too large */
497   gst_tag_list_add (tags2, GST_TAG_MERGE_REPLACE, GST_TAG_REFERENCE_LEVEL,
498       9.87654310, NULL);
499   fail_unless (!gst_tag_list_is_equal (tags2, tags));
500
501   gst_tag_list_unref (tags);
502   gst_tag_list_unref (tags2);
503
504   /* samples */
505   buf = gst_buffer_new_wrapped (g_strdup ("test 1-2-3"), 10);
506   sample1 = gst_sample_new (buf, NULL, NULL, NULL);
507   gst_buffer_unref (buf);
508
509   buf = gst_buffer_new_wrapped (g_strdup ("test 1-2-3"), 10);
510   sample11 = gst_sample_new (buf, NULL, NULL, NULL);
511   gst_buffer_unref (buf);
512
513   buf = gst_buffer_new_wrapped (g_strdup ("test 2-3-4-5"), 12);
514   sample2 = gst_sample_new (buf, NULL, NULL, NULL);
515   gst_buffer_unref (buf);
516
517   tags = gst_tag_list_new_empty ();
518   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample1, NULL);
519
520   tags2 = gst_tag_list_new_empty ();
521   fail_unless (!gst_tag_list_is_equal (tags2, tags));
522
523   /* sample sample, should be very equal now */
524   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample1, NULL);
525   fail_unless (gst_tag_list_is_equal (tags2, tags));
526   gst_tag_list_unref (tags2);
527
528   /* same buffer content, but different buffer instances, still rather equal */
529   tags2 = gst_tag_list_new_empty ();
530   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample11, NULL);
531   fail_unless (gst_tag_list_is_equal (tags2, tags));
532   gst_tag_list_unref (tags2);
533
534   /* different buffer content, should not be equal */
535   tags2 = gst_tag_list_new_empty ();
536   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample2, NULL);
537   fail_unless (!gst_tag_list_is_equal (tags2, tags));
538   gst_tag_list_unref (tags2);
539
540   gst_tag_list_unref (tags);
541
542   gst_sample_unref (sample1);
543   gst_sample_unref (sample11);
544   gst_sample_unref (sample2);
545 }
546
547 GST_END_TEST;
548
549 GST_START_TEST (test_writability)
550 {
551   GstTagList *tags, *wtags;
552
553   tags = gst_tag_list_new_empty ();
554   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
555
556   /* take ref, should no longer be writable */
557   gst_tag_list_ref (tags);
558   ASSERT_CRITICAL (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
559           GST_TAG_ARTIST, "Bar", NULL));
560
561   wtags = gst_tag_list_make_writable (tags);
562   /* should be ok now */
563   gst_tag_list_add (wtags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
564   gst_tag_list_unref (wtags);
565
566   /* this too, since we dropped the second ref in make_writable */
567   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
568   gst_tag_list_unref (tags);
569 }
570
571 GST_END_TEST;
572
573 /* this tests GstSample serialisation/deserialisation, esp. with multiple
574  * samples in a tag list */
575 GST_START_TEST (test_serialization)
576 {
577   GstTagList *tags, *tags2;
578   GstBuffer *b1, *b2;
579   GstSample *s1, *s2;
580   GstCaps *c2;
581   gchar *s;
582
583   b1 = gst_buffer_new_allocate (NULL, 1, NULL);
584   gst_buffer_memset (b1, 0, 0xb3, -1);
585   s1 = gst_sample_new (b1, NULL, NULL, NULL);
586   gst_buffer_unref (b1);
587
588   b2 = gst_buffer_new_allocate (NULL, 8, NULL);
589   gst_buffer_memset (b2, 0, 0x2f, -1);
590   c2 = gst_caps_new_empty_simple ("foo/bar");
591   s2 = gst_sample_new (b2, c2, NULL, NULL);
592   gst_buffer_unref (b2);
593   gst_caps_unref (c2);
594   c2 = NULL;
595
596   tags = gst_tag_list_new (GST_TAG_ATTACHMENT, s1, NULL);
597   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ATTACHMENT, s2, NULL);
598   GST_INFO ("tags: %" GST_PTR_FORMAT, tags);
599
600   s = gst_tag_list_to_string (tags);
601   GST_INFO ("taglist -> string: %s", s);
602   tags2 = gst_tag_list_new_from_string (s);
603   GST_INFO ("string -> taglist: %" GST_PTR_FORMAT, tags2);
604   fail_unless (gst_tag_list_is_equal (tags, tags2));
605   gst_tag_list_unref (tags2);
606   g_free (s);
607
608   gst_sample_unref (s1);
609   gst_sample_unref (s2);
610   gst_tag_list_unref (tags);
611 }
612
613 GST_END_TEST;
614
615 GST_START_TEST (test_empty_taglist_serialization)
616 {
617   GstTagList *taglist, *taglist2;
618   gchar *str;
619
620   taglist = gst_tag_list_new_empty ();
621   str = gst_tag_list_to_string (taglist);
622   taglist2 = gst_tag_list_new_from_string (str);
623   fail_if (taglist2 == NULL);
624   fail_unless (gst_tag_list_is_equal (taglist, taglist2));
625
626   gst_tag_list_unref (taglist);
627   gst_tag_list_unref (taglist2);
628   g_free (str);
629 }
630
631 GST_END_TEST;
632
633
634 static Suite *
635 gst_tag_suite (void)
636 {
637   Suite *s = suite_create ("GstTag");
638   TCase *tc_chain = tcase_create ("general");
639
640   suite_add_tcase (s, tc_chain);
641   tcase_add_test (tc_chain, test_basics);
642   tcase_add_test (tc_chain, test_add);
643   tcase_add_test (tc_chain, test_merge);
644   tcase_add_test (tc_chain, test_merge_strings_with_comma);
645   tcase_add_test (tc_chain, test_date_tags);
646   tcase_add_test (tc_chain, test_type);
647   tcase_add_test (tc_chain, test_set_non_utf8_string);
648   tcase_add_test (tc_chain, test_buffer_tags);
649   tcase_add_test (tc_chain, test_empty_tags);
650   tcase_add_test (tc_chain, test_new_full);
651   tcase_add_test (tc_chain, test_equal);
652   tcase_add_test (tc_chain, test_writability);
653   tcase_add_test (tc_chain, test_serialization);
654   tcase_add_test (tc_chain, test_empty_taglist_serialization);
655
656   return s;
657 }
658
659 GST_CHECK_MAIN (gst_tag);