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