gstcaps.c 58.5 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2
/* GStreamer
 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3
 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
Wim Taymans's avatar
Wim Taymans committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * gstcaps.c: Unit test for GstCaps
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Wim Taymans's avatar
Wim Taymans committed
21
 */
22 23 24
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Wim Taymans's avatar
Wim Taymans committed
25

26
#include <gst/check/gstcheck.h>
27
#include <gst/gstcaps.h>
28
#include "capslist.h"
Wim Taymans's avatar
Wim Taymans committed
29

30
GST_START_TEST (test_from_string)
31 32
{
  GstCaps *caps;
33 34
  GstCaps *caps2;
  gchar *to_str;
35 36 37 38 39 40
  int i;

  for (i = 0; i < G_N_ELEMENTS (caps_list); i++) {
    caps = gst_caps_from_string (caps_list[i]);
    fail_if (caps == NULL,
        "Could not create caps from string %s\n", caps_list[i]);
41 42 43
    to_str = gst_caps_to_string (caps);
    fail_if (to_str == NULL,
        "Could not convert caps back to string %s\n", caps_list[i]);
Jan Schmidt's avatar
Jan Schmidt committed
44
    caps2 = gst_caps_from_string (to_str);
45 46
    fail_if (caps2 == NULL, "Could not create caps from string %s\n", to_str);

47
    fail_unless (gst_caps_is_equal (caps, caps));
48 49
    fail_unless (gst_caps_is_equal (caps, caps2));

50 51
    gst_caps_unref (caps);
    gst_caps_unref (caps2);
52
    g_free (to_str);
53 54 55
  }
}

56
GST_END_TEST;
Wim Taymans's avatar
Wim Taymans committed
57

58
GST_START_TEST (test_double_append)
Wim Taymans's avatar
Wim Taymans committed
59 60 61 62 63
{
  GstStructure *s1;
  GstCaps *c1;

  c1 = gst_caps_new_any ();
Wim Taymans's avatar
Wim Taymans committed
64
  s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
Wim Taymans's avatar
Wim Taymans committed
65 66
  gst_caps_append_structure (c1, s1);
  ASSERT_CRITICAL (gst_caps_append_structure (c1, s1));
67 68

  gst_caps_unref (c1);
Wim Taymans's avatar
Wim Taymans committed
69 70
}

71
GST_END_TEST;
72

73
GST_START_TEST (test_mutability)
Wim Taymans's avatar
Wim Taymans committed
74 75 76 77 78 79
{
  GstStructure *s1;
  GstCaps *c1;
  gint ret;

  c1 = gst_caps_new_any ();
Wim Taymans's avatar
Wim Taymans committed
80
  s1 = gst_structure_from_string ("audio/x-raw,rate=44100", NULL);
Wim Taymans's avatar
Wim Taymans committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  gst_structure_set (s1, "rate", G_TYPE_INT, 48000, NULL);
  gst_caps_append_structure (c1, s1);
  gst_structure_set (s1, "rate", G_TYPE_INT, 22500, NULL);
  gst_caps_ref (c1);
  ASSERT_CRITICAL (gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL));
  fail_unless (gst_structure_get_int (s1, "rate", &ret));
  fail_unless (ret == 22500);
  ASSERT_CRITICAL (gst_caps_set_simple (c1, "rate", G_TYPE_INT, 11250, NULL));
  fail_unless (gst_structure_get_int (s1, "rate", &ret));
  fail_unless (ret == 22500);
  gst_caps_unref (c1);
  gst_structure_set (s1, "rate", G_TYPE_INT, 11250, NULL);
  fail_unless (gst_structure_get_int (s1, "rate", &ret));
  fail_unless (ret == 11250);
  gst_caps_set_simple (c1, "rate", G_TYPE_INT, 1, NULL);
  fail_unless (gst_structure_get_int (s1, "rate", &ret));
  fail_unless (ret == 1);
98
  gst_caps_unref (c1);
Wim Taymans's avatar
Wim Taymans committed
99
}
100

101
GST_END_TEST;
102

103 104
GST_START_TEST (test_static_caps)
{
Wim Taymans's avatar
Wim Taymans committed
105
  static GstStaticCaps scaps = GST_STATIC_CAPS ("audio/x-raw,rate=44100");
106 107
  GstCaps *caps1;
  GstCaps *caps2;
108 109
  static GstStaticCaps sany = GST_STATIC_CAPS_ANY;
  static GstStaticCaps snone = GST_STATIC_CAPS_NONE;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

  /* caps creation */
  caps1 = gst_static_caps_get (&scaps);
  fail_unless (caps1 != NULL);
  /* 1 refcount core, one from us */
  fail_unless (GST_CAPS_REFCOUNT (caps1) == 2);

  /* caps should be the same */
  caps2 = gst_static_caps_get (&scaps);
  fail_unless (caps2 != NULL);
  /* 1 refcount core, two from us */
  fail_unless (GST_CAPS_REFCOUNT (caps1) == 3);
  /* caps must be equal */
  fail_unless (caps1 == caps2);

  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
127 128 129 130 131 132 133 134

  caps1 = gst_static_caps_get (&sany);
  fail_unless (gst_caps_is_equal (caps1, GST_CAPS_ANY));
  caps2 = gst_static_caps_get (&snone);
  fail_unless (gst_caps_is_equal (caps2, GST_CAPS_NONE));
  fail_if (gst_caps_is_equal (caps1, caps2));
  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
135 136 137 138
}

GST_END_TEST;

139
static const gchar non_simple_caps_string[] =
Wim Taymans's avatar
Wim Taymans committed
140 141 142 143 144 145 146
    "video/x-raw, format=(string)I420, framerate=(fraction)[ 1/100, 100 ], "
    "width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ]; video/x-raw, "
    "format=(string)YUY2, framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
    "height=(int)[ 16, 4096 ]; video/x-raw, format=(string)RGB8_PALETTED, "
    "framerate=(fraction)[ 1/100, 100 ], width=(int)[ 16, 4096 ], "
    "height=(int)[ 16, 4096 ]; video/x-raw, "
    "format=(string){ I420, YUY2, YV12 }, width=(int)[ 16, 4096 ], "
147
    "height=(int)[ 16, 4096 ], framerate=(fraction)[ 1/100, 100 ]";
148 149

static gboolean
Wim Taymans's avatar
Wim Taymans committed
150
check_string_list (const GValue * format_value)
151
{
Wim Taymans's avatar
Wim Taymans committed
152 153
  const GValue *string_value;
  gboolean got_rgb8 = FALSE;
154 155 156
  gboolean got_yv12 = FALSE;
  gboolean got_i420 = FALSE;
  gboolean got_yuy2 = FALSE;
Wim Taymans's avatar
Wim Taymans committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  const gchar *string;

  string_value = gst_value_list_get_value (format_value, 0);
  fail_unless (string_value != NULL);
  fail_unless (G_VALUE_HOLDS_STRING (string_value));
  string = g_value_get_string (string_value);
  fail_unless (string != NULL);
  got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
  got_i420 = got_i420 || (g_str_equal (string, "I420"));
  got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
  got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));

  string_value = gst_value_list_get_value (format_value, 1);
  fail_unless (string_value != NULL);
  fail_unless (G_VALUE_HOLDS_STRING (string_value));
  string = g_value_get_string (string_value);
  fail_unless (string != NULL);
  got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
  got_i420 = got_i420 || (g_str_equal (string, "I420"));
  got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
  got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));

  string_value = gst_value_list_get_value (format_value, 2);
  fail_unless (string_value != NULL);
  fail_unless (G_VALUE_HOLDS_STRING (string_value));
  string = g_value_get_string (string_value);
  fail_unless (string != NULL);
  got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
  got_i420 = got_i420 || (g_str_equal (string, "I420"));
  got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
  got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));

  string_value = gst_value_list_get_value (format_value, 3);
  fail_unless (string_value != NULL);
  fail_unless (G_VALUE_HOLDS_STRING (string_value));
  string = g_value_get_string (string_value);
  fail_unless (string != NULL);
  got_rgb8 = got_rgb8 || (g_str_equal (string, "RGB8_PALETTED"));
  got_i420 = got_i420 || (g_str_equal (string, "I420"));
  got_yuy2 = got_yuy2 || (g_str_equal (string, "YUY2"));
  got_yv12 = got_yv12 || (g_str_equal (string, "YV12"));

  return (got_rgb8 && got_i420 && got_yuy2 && got_yv12);
200 201 202 203
}

GST_START_TEST (test_simplify)
{
Wim Taymans's avatar
Wim Taymans committed
204
  GstStructure *s1;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
205
  GstCaps *caps;
206 207 208 209 210

  caps = gst_caps_from_string (non_simple_caps_string);
  fail_unless (caps != NULL,
      "gst_caps_from_string (non_simple_caps_string) failed");

211 212
  caps = gst_caps_simplify (caps);
  fail_unless (caps != NULL, "gst_caps_simplify() should have worked");
213 214 215

  /* check simplified caps, should be:
   *
Wim Taymans's avatar
Wim Taymans committed
216
   * video/x-raw, format=(string){ RGB8_PALETTED, YV12, YUY2, I420 },
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
217
   *     width=(int)[ 16, 4096 ], height=(int)[ 16, 4096 ],
218
   *     framerate=(fraction)[ 1/100, 100 ]
219
   */
Wim Taymans's avatar
Wim Taymans committed
220 221
  GST_DEBUG ("simplyfied %" GST_PTR_FORMAT, caps);
  fail_unless (gst_caps_get_size (caps) == 1);
222 223 224
  s1 = gst_caps_get_structure (caps, 0);
  fail_unless (s1 != NULL);

Wim Taymans's avatar
Wim Taymans committed
225
  fail_unless (gst_structure_has_name (s1, "video/x-raw"));
226 227 228 229 230
  {
    const GValue *framerate_value;
    const GValue *format_value;
    const GValue *width_value;
    const GValue *height_value;
231 232
    const GValue *val_fps;
    GValue test_fps = { 0, };
233 234 235
    gint min_width, max_width;
    gint min_height, max_height;

Wim Taymans's avatar
Wim Taymans committed
236
    format_value = gst_structure_get_value (s1, "format");
237 238
    fail_unless (format_value != NULL);
    fail_unless (GST_VALUE_HOLDS_LIST (format_value));
Wim Taymans's avatar
Wim Taymans committed
239 240
    fail_unless (gst_value_list_get_size (format_value) == 4);
    fail_unless (check_string_list (format_value) == TRUE);
241

242
    g_value_init (&test_fps, GST_TYPE_FRACTION);
Wim Taymans's avatar
Wim Taymans committed
243
    framerate_value = gst_structure_get_value (s1, "framerate");
244
    fail_unless (framerate_value != NULL);
245 246 247 248 249 250 251 252 253 254 255
    fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (framerate_value));

    val_fps = gst_value_get_fraction_range_min (framerate_value);
    gst_value_set_fraction (&test_fps, 1, 100);
    fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);

    val_fps = gst_value_get_fraction_range_max (framerate_value);
    gst_value_set_fraction (&test_fps, 100, 1);
    fail_unless (gst_value_compare (&test_fps, val_fps) == GST_VALUE_EQUAL);

    g_value_unset (&test_fps);
256

Wim Taymans's avatar
Wim Taymans committed
257
    width_value = gst_structure_get_value (s1, "width");
258 259 260 261 262 263
    fail_unless (width_value != NULL);
    fail_unless (GST_VALUE_HOLDS_INT_RANGE (width_value));
    min_width = gst_value_get_int_range_min (width_value);
    max_width = gst_value_get_int_range_max (width_value);
    fail_unless (min_width == 16 && max_width == 4096);

Wim Taymans's avatar
Wim Taymans committed
264
    height_value = gst_structure_get_value (s1, "height");
265 266 267 268 269 270 271 272 273 274 275 276
    fail_unless (height_value != NULL);
    fail_unless (GST_VALUE_HOLDS_INT_RANGE (height_value));
    min_height = gst_value_get_int_range_min (height_value);
    max_height = gst_value_get_int_range_max (height_value);
    fail_unless (min_height == 16 && max_height == 4096);
  }

  gst_caps_unref (caps);
}

GST_END_TEST;

277 278 279 280 281 282 283 284
GST_START_TEST (test_truncate)
{
  GstCaps *caps;

  caps = gst_caps_from_string (non_simple_caps_string);
  fail_unless (caps != NULL,
      "gst_caps_from_string (non_simple_caps_string) failed");
  fail_unless_equals_int (gst_caps_get_size (caps), 4);
285
  caps = gst_caps_truncate (caps);
286 287 288 289 290 291
  fail_unless_equals_int (gst_caps_get_size (caps), 1);
  gst_caps_unref (caps);
}

GST_END_TEST;

292 293 294 295
GST_START_TEST (test_subset)
{
  GstCaps *c1, *c2;

Wim Taymans's avatar
Wim Taymans committed
296 297
  c1 = gst_caps_from_string ("video/x-raw; video/x-raw");
  c2 = gst_caps_from_string ("video/x-raw, format=(string)YUY2");
298 299 300 301 302 303
  fail_unless (gst_caps_is_subset (c2, c1));
  fail_if (gst_caps_is_subset (c1, c2));
  gst_caps_unref (c1);
  gst_caps_unref (c2);

  c1 = gst_caps_from_string
Wim Taymans's avatar
Wim Taymans committed
304 305
      ("audio/x-raw, channels=(int)[ 1, 2 ], rate=(int)44100");
  c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1, rate=(int)44100");
306 307 308 309
  fail_unless (gst_caps_is_subset (c2, c1));
  fail_if (gst_caps_is_subset (c1, c2));
  gst_caps_unref (c1);
  gst_caps_unref (c2);
310

Wim Taymans's avatar
Wim Taymans committed
311 312
  c1 = gst_caps_from_string ("audio/x-raw, channels=(int) {1}");
  c2 = gst_caps_from_string ("audio/x-raw, channels=(int)1");
313 314 315 316 317 318 319
  fail_unless (gst_caps_is_subset (c2, c1));
  fail_unless (gst_caps_is_subset (c1, c2));
  fail_unless (gst_caps_is_equal (c1, c2));
  gst_caps_unref (c1);
  gst_caps_unref (c2);

  c1 = gst_caps_from_string
Wim Taymans's avatar
Wim Taymans committed
320
      ("audio/x-raw, rate=(int)44100, channels=(int)3, format=(string)U16_LE");
321
  c2 = gst_caps_from_string
Wim Taymans's avatar
Wim Taymans committed
322
      ("audio/x-raw, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], format=(string){ S16_LE, U16_LE }");
323 324 325 326
  fail_unless (gst_caps_is_subset (c1, c2));
  fail_if (gst_caps_is_subset (c2, c1));
  gst_caps_unref (c1);
  gst_caps_unref (c2);
327 328 329 330 331 332 333 334 335

  c1 = gst_caps_from_string ("video/x-h264, parsed=(boolean)true");
  c2 = gst_caps_from_string
      ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)nal");
  fail_if (gst_caps_is_subset (c2, c1));
  fail_if (gst_caps_is_subset (c1, c2));
  fail_if (gst_caps_is_equal (c1, c2));
  gst_caps_unref (c1);
  gst_caps_unref (c2);
336 337 338 339
}

GST_END_TEST;

340 341 342 343 344
GST_START_TEST (test_subset_duplication)
{
  GstCaps *c1, *c2;

  c1 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");
345 346 347 348 349 350 351 352 353 354 355 356
  c2 = gst_caps_from_string ("audio/x-raw, format=(string)F32LE");

  fail_unless (gst_caps_is_subset (c1, c2));
  fail_unless (gst_caps_is_subset (c2, c1));

  gst_caps_unref (c2);
  c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE }");

  fail_unless (gst_caps_is_subset (c1, c2));
  fail_unless (gst_caps_is_subset (c2, c1));

  gst_caps_unref (c2);
357 358 359
  c2 = gst_caps_from_string ("audio/x-raw, format=(string){ F32LE, F32LE }");

  fail_unless (gst_caps_is_subset (c1, c2));
360 361 362 363 364 365 366 367 368
  fail_unless (gst_caps_is_subset (c2, c1));

  gst_caps_unref (c2);
  c2 = gst_caps_from_string
      ("audio/x-raw, format=(string){ F32LE, F32LE, F32LE }");

  fail_unless (gst_caps_is_subset (c1, c2));
  fail_unless (gst_caps_is_subset (c2, c1));

369 370 371 372 373 374
  gst_caps_unref (c1);
  gst_caps_unref (c2);
}

GST_END_TEST;

375 376 377 378 379
GST_START_TEST (test_merge_fundamental)
{
  GstCaps *c1, *c2;

  /* ANY + specific = ANY */
Wim Taymans's avatar
Wim Taymans committed
380
  c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
381
  c2 = gst_caps_new_any ();
382
  c2 = gst_caps_merge (c2, c1);
383 384 385 386 387 388
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 0, NULL);
  fail_unless (gst_caps_is_any (c2), NULL);
  gst_caps_unref (c2);

  /* specific + ANY = ANY */
Wim Taymans's avatar
Wim Taymans committed
389
  c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
390
  c1 = gst_caps_new_any ();
391
  c2 = gst_caps_merge (c2, c1);
392 393 394 395 396 397
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 0, NULL);
  fail_unless (gst_caps_is_any (c2), NULL);
  gst_caps_unref (c2);

  /* EMPTY + specific = specific */
Wim Taymans's avatar
Wim Taymans committed
398
  c1 = gst_caps_from_string ("audio/x-raw,rate=44100");
399
  c2 = gst_caps_new_empty ();
400
  c2 = gst_caps_merge (c2, c1);
401 402 403 404 405 406
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
  fail_if (gst_caps_is_empty (c2), NULL);
  gst_caps_unref (c2);

  /* specific + EMPTY = specific */
Wim Taymans's avatar
Wim Taymans committed
407
  c2 = gst_caps_from_string ("audio/x-raw,rate=44100");
408
  c1 = gst_caps_new_empty ();
409
  c2 = gst_caps_merge (c2, c1);
410 411 412 413 414 415 416 417 418 419
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
  fail_if (gst_caps_is_empty (c2), NULL);
  gst_caps_unref (c2);
}

GST_END_TEST;

GST_START_TEST (test_merge_same)
{
420
  GstCaps *c1, *c2, *test;
421 422

  /* this is the same */
Wim Taymans's avatar
Wim Taymans committed
423 424
  c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
  c2 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
425
  c2 = gst_caps_merge (c2, c1);
426 427
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
Wim Taymans's avatar
Wim Taymans committed
428
  test = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
429 430
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (test);
431
  gst_caps_unref (c2);
432 433

  /* and so is this */
Wim Taymans's avatar
Wim Taymans committed
434 435
  c1 = gst_caps_from_string ("audio/x-raw,rate=44100,channels=1");
  c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
436
  c2 = gst_caps_merge (c2, c1);
437 438
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
439 440 441 442
  gst_caps_unref (c2);

  c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
  c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
443
  c2 = gst_caps_merge (c2, c1);
444 445 446 447 448 449
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
  gst_caps_unref (c2);

  c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AABB");
  c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
450
  c2 = gst_caps_merge (c2, c1);
451 452 453 454 455 456
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
  gst_caps_unref (c2);

  c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
  c2 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
457
  c2 = gst_caps_merge (c2, c1);
458 459 460
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
  gst_caps_unref (c2);
461

462 463
  c1 = gst_caps_from_string ("video/x-foo, data=(buffer)AA");
  c2 = gst_caps_from_string ("video/x-bar, data=(buffer)AA");
464
  c2 = gst_caps_merge (c2, c1);
465 466
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
467 468 469 470 471 472 473
  gst_caps_unref (c2);
}

GST_END_TEST;

GST_START_TEST (test_merge_subset)
{
474
  GstCaps *c1, *c2, *test;
475 476

  /* the 2nd is already covered */
Wim Taymans's avatar
Wim Taymans committed
477 478
  c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
  c1 = gst_caps_from_string ("audio/x-raw,channels=1");
479
  c2 = gst_caps_merge (c2, c1);
480 481
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
Wim Taymans's avatar
Wim Taymans committed
482
  test = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
483
  fail_unless (gst_caps_is_equal (c2, test));
484
  gst_caps_unref (c2);
485
  gst_caps_unref (test);
486 487

  /* here it is not */
Wim Taymans's avatar
Wim Taymans committed
488 489
  c2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
  c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
490
  c2 = gst_caps_merge (c2, c1);
491 492
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
Wim Taymans's avatar
Wim Taymans committed
493
  test = gst_caps_from_string ("audio/x-raw,channels=[1,2],rate=44100");
494 495 496 497 498
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* second one was already contained in the first one */
Wim Taymans's avatar
Wim Taymans committed
499 500
  c2 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
  c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
501
  c2 = gst_caps_merge (c2, c1);
502 503
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
Wim Taymans's avatar
Wim Taymans committed
504
  test = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
505 506 507 508 509
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* second one was already contained in the first one */
Wim Taymans's avatar
Wim Taymans committed
510 511
  c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
  c1 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
512
  c2 = gst_caps_merge (c2, c1);
513 514
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
Wim Taymans's avatar
Wim Taymans committed
515
  test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
516 517 518 519 520
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* second one was already contained in the first one */
Wim Taymans's avatar
Wim Taymans committed
521 522
  c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
  c1 = gst_caps_from_string ("audio/x-raw,channels=[2,4]");
523
  c2 = gst_caps_merge (c2, c1);
524 525
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
Wim Taymans's avatar
Wim Taymans committed
526
  test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
527 528 529 530 531
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* second one was already contained in the first one */
Wim Taymans's avatar
Wim Taymans committed
532 533
  c2 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
  c1 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
534
  c2 = gst_caps_merge (c2, c1);
535 536
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
Wim Taymans's avatar
Wim Taymans committed
537
  test = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
538 539 540 541 542
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* these caps cannot be merged */
Wim Taymans's avatar
Wim Taymans committed
543 544
  c2 = gst_caps_from_string ("audio/x-raw,channels=[2,3]");
  c1 = gst_caps_from_string ("audio/x-raw,channels=[1,4]");
545
  c2 = gst_caps_merge (c2, c1);
546 547 548 549
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
  test =
      gst_caps_from_string
Wim Taymans's avatar
Wim Taymans committed
550
      ("audio/x-raw,channels=[2,3];audio/x-raw,channels=[1,4]");
551 552 553 554 555
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* these caps cannot be merged */
Wim Taymans's avatar
Wim Taymans committed
556 557
  c2 = gst_caps_from_string ("audio/x-raw,channels=[1,2]");
  c1 = gst_caps_from_string ("audio/x-raw,channels=[1,3]");
558
  c2 = gst_caps_merge (c2, c1);
559 560 561 562
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
  test =
      gst_caps_from_string
Wim Taymans's avatar
Wim Taymans committed
563
      ("audio/x-raw,channels=[1,2];audio/x-raw,channels=[1,3]");
564 565 566
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);
567

Wim Taymans's avatar
Wim Taymans committed
568 569
  c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
  c1 = gst_caps_from_string ("audio/x-raw,channels={1,2,3,4}");
570
  c2 = gst_caps_merge (c2, c1);
571 572
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
Wim Taymans's avatar
Wim Taymans committed
573 574
  test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
      "audio/x-raw,channels={1,2,3,4}");
575 576 577 578
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

Wim Taymans's avatar
Wim Taymans committed
579 580
  c2 = gst_caps_from_string ("audio/x-raw,channels={1,2}");
  c1 = gst_caps_from_string ("audio/x-raw,channels={1,3}");
581
  c2 = gst_caps_merge (c2, c1);
582 583
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
Wim Taymans's avatar
Wim Taymans committed
584 585
  test = gst_caps_from_string ("audio/x-raw,channels={1,2};"
      "audio/x-raw,channels={1,3}");
586 587 588 589
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

Wim Taymans's avatar
Wim Taymans committed
590 591
  c2 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/2, 5/1 }");
  c1 = gst_caps_from_string ("video/x-raw, framerate=(fraction){ 15/1, 5/1 }");
592
  test = gst_caps_copy (c1);
593
  c2 = gst_caps_merge (c2, c1);
594 595 596
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_is_subset (test, c2));
  gst_caps_unref (test);
597
  gst_caps_unref (c2);
598

Wim Taymans's avatar
Wim Taymans committed
599 600
  c2 = gst_caps_from_string ("audio/x-raw");
  c1 = gst_caps_from_string ("audio/x-raw,channels=1");
601
  c2 = gst_caps_merge (c2, c1);
602 603
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 1, NULL);
Wim Taymans's avatar
Wim Taymans committed
604
  test = gst_caps_from_string ("audio/x-raw");
605 606 607 608
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);

Wim Taymans's avatar
Wim Taymans committed
609 610
  c2 = gst_caps_from_string ("audio/x-raw,channels=1");
  c1 = gst_caps_from_string ("audio/x-raw");
611
  c2 = gst_caps_merge (c2, c1);
612 613
  GST_DEBUG ("merged: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (c2), c2);
  fail_unless (gst_caps_get_size (c2) == 2, NULL);
Wim Taymans's avatar
Wim Taymans committed
614
  test = gst_caps_from_string ("audio/x-raw,channels=1; audio/x-raw");
615 616 617
  fail_unless (gst_caps_is_equal (c2, test));
  gst_caps_unref (c2);
  gst_caps_unref (test);
618 619 620 621
}

GST_END_TEST;

622 623 624 625 626 627 628
GST_START_TEST (test_intersect)
{
  GstStructure *s;
  GstCaps *c1, *c2, *ci1, *ci2;

  /* field not specified = any value possible, so the intersection
   * should keep fields which are only part of one set of caps */
Wim Taymans's avatar
Wim Taymans committed
629 630
  c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
  c1 = gst_caps_from_string ("video/x-raw,format=(string)I420");
631 632 633 634 635

  ci1 = gst_caps_intersect (c2, c1);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
  fail_unless (gst_caps_get_size (ci1) == 1, NULL);
  s = gst_caps_get_structure (ci1, 0);
Wim Taymans's avatar
Wim Taymans committed
636
  fail_unless (gst_structure_has_name (s, "video/x-raw"));
637 638 639 640 641 642 643 644
  fail_unless (gst_structure_get_value (s, "format") != NULL);
  fail_unless (gst_structure_get_value (s, "width") != NULL);

  /* with changed order */
  ci2 = gst_caps_intersect (c1, c2);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
  fail_unless (gst_caps_get_size (ci2) == 1, NULL);
  s = gst_caps_get_structure (ci2, 0);
Wim Taymans's avatar
Wim Taymans committed
645
  fail_unless (gst_structure_has_name (s, "video/x-raw"));
646 647 648 649 650 651 652 653 654 655 656 657 658
  fail_unless (gst_structure_get_value (s, "format") != NULL);
  fail_unless (gst_structure_get_value (s, "width") != NULL);

  fail_unless (gst_caps_is_equal (ci1, ci2));

  gst_caps_unref (ci1);
  gst_caps_unref (ci2);

  gst_caps_unref (c1);
  gst_caps_unref (c2);

  /* ========== */

Wim Taymans's avatar
Wim Taymans committed
659 660
  c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
  c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=30");
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

  ci1 = gst_caps_intersect (c2, c1);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
  fail_unless (gst_caps_is_empty (ci1), NULL);

  /* with changed order */
  ci2 = gst_caps_intersect (c1, c2);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
  fail_unless (gst_caps_is_empty (ci2), NULL);

  fail_unless (gst_caps_is_equal (ci1, ci2));

  gst_caps_unref (ci1);
  gst_caps_unref (ci2);

  gst_caps_unref (c1);
  gst_caps_unref (c2);

  /* ========== */

Wim Taymans's avatar
Wim Taymans committed
681 682
  c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
  c1 = gst_caps_from_string ("video/x-raw2,format=(string)I420,width=20");
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

  ci1 = gst_caps_intersect (c2, c1);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
  fail_unless (gst_caps_is_empty (ci1), NULL);

  /* with changed order */
  ci2 = gst_caps_intersect (c1, c2);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
  fail_unless (gst_caps_is_empty (ci2), NULL);

  fail_unless (gst_caps_is_equal (ci1, ci2));

  gst_caps_unref (ci1);
  gst_caps_unref (ci2);

  gst_caps_unref (c1);
  gst_caps_unref (c2);

  /* ========== */

Wim Taymans's avatar
Wim Taymans committed
703 704
  c2 = gst_caps_from_string ("video/x-raw,format=(string)I420,width=20");
  c1 = gst_caps_from_string ("video/x-raw,format=(string)I420,height=30");
705 706 707 708 709

  ci1 = gst_caps_intersect (c2, c1);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci1);
  fail_unless (gst_caps_get_size (ci1) == 1, NULL);
  s = gst_caps_get_structure (ci1, 0);
Wim Taymans's avatar
Wim Taymans committed
710
  fail_unless (gst_structure_has_name (s, "video/x-raw"));
711 712 713 714 715 716 717 718 719
  fail_unless (gst_structure_get_value (s, "format") != NULL);
  fail_unless (gst_structure_get_value (s, "width") != NULL);
  fail_unless (gst_structure_get_value (s, "height") != NULL);

  /* with changed order */
  ci2 = gst_caps_intersect (c1, c2);
  GST_DEBUG ("intersected: %" GST_PTR_FORMAT, ci2);
  fail_unless (gst_caps_get_size (ci2) == 1, NULL);
  s = gst_caps_get_structure (ci2, 0);
Wim Taymans's avatar
Wim Taymans committed
720
  fail_unless (gst_structure_has_name (s, "video/x-raw"));
721 722 723 724 725 726 727 728 729 730 731 732 733 734
  fail_unless (gst_structure_get_value (s, "format") != NULL);
  fail_unless (gst_structure_get_value (s, "height") != NULL);
  fail_unless (gst_structure_get_value (s, "width") != NULL);

  fail_unless (gst_caps_is_equal (ci1, ci2));

  gst_caps_unref (ci1);
  gst_caps_unref (ci2);

  gst_caps_unref (c1);
  gst_caps_unref (c2);
}

GST_END_TEST;
735

736 737 738 739 740
GST_START_TEST (test_intersect2)
{
  GstCaps *caps1, *caps2, *icaps;

  /* tests array subtraction */
Wim Taymans's avatar
Wim Taymans committed
741
  caps1 = gst_caps_from_string ("audio/x-raw, "
742 743 744 745 746 747
      "channel-positions=(int)<                      "
      "{ 1, 2, 3, 4, 5, 6 },                         "
      "{ 1, 2, 3, 4, 5, 6 },                         "
      "{ 1, 2, 3, 4, 5, 6 },                         "
      "{ 1, 2, 3, 4, 5, 6 },                         "
      "{ 1, 2, 3, 4, 5, 6 },                         " "{ 1, 2, 3, 4, 5, 6 }>");
Wim Taymans's avatar
Wim Taymans committed
748
  caps2 = gst_caps_from_string ("audio/x-raw, "
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
      "channel-positions=(int)< 1, 2, 3, 4, 5, 6 >");
  icaps = gst_caps_intersect (caps1, caps2);
  GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
  fail_if (gst_caps_is_empty (icaps));
  fail_unless (gst_caps_is_equal (icaps, caps2));
  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
  gst_caps_unref (icaps);

  /* ===== */

  caps1 = gst_caps_from_string ("some/type, foo=(int)< { 1, 2 }, { 3, 4} >");
  caps2 = gst_caps_from_string ("some/type, foo=(int)< 1, 3 >");
  icaps = gst_caps_intersect (caps1, caps2);
  GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
  fail_if (gst_caps_is_empty (icaps));
  fail_unless (gst_caps_is_equal (icaps, caps2));
  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
  gst_caps_unref (icaps);
}

GST_END_TEST;

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
GST_START_TEST (test_intersect_list_duplicate)
{
  GstCaps *caps1, *caps2, *icaps;

  /* make sure we don't take too long to intersect these.. */
  caps1 = gst_caps_from_string ("video/x-raw, format=(string)YV12; "
      "video/x-raw, format=(string)I420; video/x-raw, format=(string)YUY2; "
      "video/x-raw, format=(string)UYVY; "
      "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx,"
      " xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, { ABGR, ABGR, "
      "{ ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
      "RGB, BGR, Y41B, Y42B, YVYU, Y444 }; "
      "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx, "
      "xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } } } } }, BGRA, ARGB, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } } } }, "
      "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }; "
      "video/x-raw, format=(string){ I420, YV12, YUY2, UYVY, AYUV, RGBx, "
      "BGRx, xRGB, xBGR, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } }, { RGBA, RGBA, { RGBA, RGBA }, "
      "{ RGBA, RGBA, { RGBA, RGBA } } } }, BGRA, ARGB, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } }, "
      "{ ABGR, ABGR, { ABGR, ABGR }, { ABGR, ABGR, { ABGR, ABGR } } } }, "
      "RGB, BGR, Y41B, Y42B, YVYU, Y444, NV12, NV21 }");

  caps2 = gst_caps_copy (caps1);

  icaps = gst_caps_intersect (caps1, caps2);

  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
  gst_caps_unref (icaps);
}

GST_END_TEST;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

GST_START_TEST (test_intersect_zigzag)
{
  GstCaps *caps1, *caps2, *icaps, *result;

  /* tests if caps order is maintained */
  caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
  caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");

  icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_ZIG_ZAG);
  result = gst_caps_from_string ("format/B; format/A; format/D; format/C");
  GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
  fail_if (gst_caps_is_empty (icaps));
  fail_unless (gst_caps_is_equal (icaps, result));
  gst_caps_unref (icaps);
  gst_caps_unref (result);

  icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
  result = gst_caps_from_string ("format/A; format/B; format/D; format/C");
  GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
  fail_if (gst_caps_is_empty (icaps));
  fail_unless (gst_caps_is_equal (icaps, result));
  gst_caps_unref (icaps);
  gst_caps_unref (result);

  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
}

GST_END_TEST;


GST_START_TEST (test_intersect_first)
{
  GstCaps *caps1, *caps2, *icaps, *result;

  /* tests if caps order is maintained */
  caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
  caps2 = gst_caps_from_string ("format/C; format/D; format/A");
  icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
  result = gst_caps_from_string ("format/A; format/C; format/D");
  GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
  fail_if (gst_caps_is_empty (icaps));
  fail_unless (gst_caps_is_equal (icaps, result));
  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
  gst_caps_unref (icaps);
  gst_caps_unref (result);
}

GST_END_TEST;


GST_START_TEST (test_intersect_first2)
{
  GstCaps *caps1, *caps2, *icaps, *result;

  /* tests if caps order is maintained */
  caps1 = gst_caps_from_string ("format/A; format/B; format/C; format/D");
  caps2 = gst_caps_from_string ("format/D; format/A; format/B; format/C");

  icaps = gst_caps_intersect_full (caps1, caps2, GST_CAPS_INTERSECT_FIRST);
  result = gst_caps_from_string ("format/A; format/B; format/C; format/D");
  GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
  fail_if (gst_caps_is_empty (icaps));
  fail_unless (gst_caps_is_equal (icaps, result));
  gst_caps_unref (icaps);
  gst_caps_unref (result);

  icaps = gst_caps_intersect_full (caps2, caps1, GST_CAPS_INTERSECT_FIRST);
  result = gst_caps_from_string ("format/D; format/A; format/B; format/C");
  GST_LOG ("intersected caps: %" GST_PTR_FORMAT, icaps);
  fail_if (gst_caps_is_empty (icaps));
  fail_unless (gst_caps_is_equal (icaps, result));
  gst_caps_unref (icaps);
  gst_caps_unref (result);

  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
}

GST_END_TEST;

920 921 922 923 924
GST_START_TEST (test_intersect_duplication)
{
  GstCaps *c1, *c2, *test;

  c1 = gst_caps_from_string
Wim Taymans's avatar
Wim Taymans committed
925
      ("audio/x-raw, format=(string)S16_LE, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]");
926
  c2 = gst_caps_from_string
Wim Taymans's avatar
Wim Taymans committed
927
      ("audio/x-raw, format=(string) { S16_LE, S16_BE, U16_LE, U16_BE }, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2 ]; audio/x-raw, format=(string) { S16_LE, S16_BE, U16_LE, U16_BE }, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 11 ]; audio/x-raw, format=(string) { S16_LE, S16_BE, U16_LE, U16_BE }, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 11 ]");
928 929 930 931 932 933 934 935 936 937

  test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
  fail_unless_equals_int (gst_caps_get_size (test), 1);
  fail_unless (gst_caps_is_equal (c1, test));
  gst_caps_unref (c1);
  gst_caps_unref (c2);
  gst_caps_unref (test);
}

GST_END_TEST;
938

Jan Schmidt's avatar
Jan Schmidt committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
GST_START_TEST (test_intersect_flagset)
{
  GstCaps *c1, *c2, *test;
  GType test_flagset_type;
  GstSeekFlags test_flags, test_mask;
  gchar *test_string;

  /* Test that matching bits inside the mask intersect,
   * and bits outside the mask don't matter */
  c1 = gst_caps_from_string ("test/x-caps,field=ffd81d:fffff0");
  c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");

  test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
  fail_unless_equals_int (gst_caps_get_size (test), 1);
  fail_unless (gst_caps_is_equal (c1, test));
  gst_caps_unref (c1);
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* Test that non-matching bits in the mask don't intersect */
  c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
  c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");

  test = gst_caps_intersect_full (c1, c2, GST_CAPS_INTERSECT_FIRST);
  fail_unless (gst_caps_is_empty (test));
  gst_caps_unref (c1);
  gst_caps_unref (c2);
  gst_caps_unref (test);

  /* Check custom flags type serialisation and de-serialisation */
  test_flagset_type = gst_flagset_register (GST_TYPE_SEEK_FLAGS);
  fail_unless (g_type_is_a (test_flagset_type, GST_TYPE_FLAG_SET));

  test_flags =
      GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_TRICKMODE |
      GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
  test_mask =
      GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_TRICKMODE |
      GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;

  c1 = gst_caps_new_simple ("test/x-caps", "field", test_flagset_type,
980
      test_flags, test_mask, NULL);
Jan Schmidt's avatar
Jan Schmidt committed
981 982 983 984 985 986

  test_string = gst_caps_to_string (c1);
  fail_if (test_string == NULL);

  GST_DEBUG ("Serialised caps to %s", test_string);
  c2 = gst_caps_from_string (test_string);
987
  g_free (test_string);
Jan Schmidt's avatar
Jan Schmidt committed
988

989 990
  fail_unless (gst_caps_is_equal (c1, c2), "Caps %s != %s",
      gst_caps_to_string (c1), gst_caps_to_string (c2));
Jan Schmidt's avatar
Jan Schmidt committed
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

  gst_caps_unref (c1);
  gst_caps_unref (c2);
}

GST_END_TEST;

GST_START_TEST (test_union)
{
  GstCaps *c1, *c2, *test, *expect;

  /* Test that matching bits inside the masks union OK, */
  c1 = gst_caps_from_string ("test/x-caps,field=ffd81d:0ffff0");
  c2 = gst_caps_from_string ("test/x-caps,field=0fd81f:0ffff0");

  test = gst_caps_merge (c1, c2);
  test = gst_caps_simplify (test);
  /* c1, c2 now invalid */
  fail_unless_equals_int (gst_caps_get_size (test), 1);
  gst_caps_unref (test);

  /* Test that non-intersecting sets of masked bits are OK */
  c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
  c2 = gst_caps_from_string ("test/x-caps,field=4fd81f:f00000");
  expect = gst_caps_from_string ("test/x-caps,field=4f001d:fffff0");
  test = gst_caps_simplify (gst_caps_merge (c1, c2));
  /* c1, c2 now invalid */
  GST_LOG ("Expected caps %" GST_PTR_FORMAT " got %" GST_PTR_FORMAT "\n",
      expect, test);
  fail_unless (gst_caps_is_equal (test, expect));
  gst_caps_unref (test);
  gst_caps_unref (expect);

  /* Test that partially-intersecting sets of masked bits that match are OK */
  c1 = gst_caps_from_string ("test/x-caps,field=ff001d:0ffff0");
  c2 = gst_caps_from_string ("test/x-caps,field=4fd81f:ff0000");
  expect = gst_caps_from_string ("test/x-caps,field=4f001d:fffff0");
  test = gst_caps_simplify (gst_caps_merge (c1, c2));
  /* c1, c2 now invalid */
  GST_LOG ("Expected caps %" GST_PTR_FORMAT " got %" GST_PTR_FORMAT "\n",
      expect, test);
  fail_unless (gst_caps_is_equal (test, expect));
  gst_caps_unref (test);
  gst_caps_unref (expect);
}

GST_END_TEST;

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
static gboolean
_caps_is_fixed_foreach (GQuark field_id, const GValue * value, gpointer unused)
{
  return gst_value_is_fixed (value);
}


GST_START_TEST (test_normalize)
{
  GstCaps *in, *norm, *out;
  guint i;

  in = gst_caps_from_string ("some/type, foo=(int){ 1 , 2 }");
  out = gst_caps_from_string ("some/type, foo=(int) 1; some/type, foo=(int) 2");
  norm = gst_caps_normalize (in);
  fail_if (gst_caps_is_empty (norm));
  fail_unless (gst_caps_is_equal (norm, out));
  for (i = 0; i < gst_caps_get_size (norm); i++) {
    GstStructure *st = gst_caps_get_structure (norm, i);
    /* Make sure all fields of all structures are fixed */
    fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
  }

  gst_caps_unref (out);
  gst_caps_unref (norm);

  in = gst_caps_from_string
      ("some/type, foo=(int){ 1 , 2 }, bar=(int){ 3, 4 }");
  out =
      gst_caps_from_string
      ("some/type, foo=(int) 1, bar=(int) 3; some/type, foo=(int) 2, bar=(int) 3;"
      "some/type, foo=(int) 1, bar=(int) 4; some/type, foo=(int) 2, bar=(int) 4;");
  norm = gst_caps_normalize (in);
  fail_if (gst_caps_is_empty (norm));
  fail_unless (gst_caps_is_equal (norm, out));
  for (i = 0; i < gst_caps_get_size (norm); i++) {
    GstStructure *st = gst_caps_get_structure (norm, i);
    /* Make sure all fields of all structures are fixed */
    fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
  }

  gst_caps_unref (out);
  gst_caps_unref (norm);

  in = gst_caps_from_string
      ("some/type, foo=(string){ 1 , 2 }, bar=(string) { 3 }");
  out =
      gst_caps_from_string
      ("some/type, foo=(string) 1, bar=(string) 3; some/type, foo=(string) 2, bar=(string) 3");
  norm = gst_caps_normalize (in);
  fail_if (gst_caps_is_empty (norm));
  fail_unless (gst_caps_is_equal (norm, out));
  for (i = 0; i < gst_caps_get_size (norm); i++) {
    GstStructure *st = gst_caps_get_structure (norm, i);
    /* Make sure all fields of all structures are fixed */
    fail_unless (gst_structure_foreach (st, _caps_is_fixed_foreach, NULL));
  }

  gst_caps_unref (out);
  gst_caps_unref (norm);
}

GST_END_TEST;
Stefan Kost's avatar
Stefan Kost committed
1102

1103 1104
GST_START_TEST (test_broken)
{
1105
  GstCaps *c1 = NULL;
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

  /* NULL is not valid for media_type */
  ASSERT_CRITICAL (c1 =
      gst_caps_new_simple (NULL, "field", G_TYPE_INT, 1, NULL));
  fail_if (c1);

  /* such a name is not valid, see gst_structure_validate_name() */
  ASSERT_CRITICAL (c1 =
      gst_caps_new_simple ("1#@abc", "field", G_TYPE_INT, 1, NULL));
  fail_if (c1);
1116 1117 1118 1119 1120 1121 1122

  c1 = gst_caps_new_empty_simple ("test/broken-fraction");
  fail_unless (c1);
  /* denominator must be non-zero value */
  ASSERT_CRITICAL (gst_caps_set_simple (c1, "foo", GST_TYPE_FRACTION, 1, 0,
          NULL));
  gst_caps_unref (c1);
1123 1124 1125 1126
}

GST_END_TEST;

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
GST_START_TEST (test_features)
{
  GstCaps *c1, *c2, *c3;
  GstStructure *s1, *s2;
  GstCapsFeatures *f1, *f2;
  gchar *str1;
  static GstStaticCaps scaps =
      GST_STATIC_CAPS
      ("video/x-raw(memory:EGLImage), width=320, height=[ 240, 260 ]");

  c1 = gst_caps_new_empty ();
  fail_unless (c1 != NULL);
  s1 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
      GST_TYPE_INT_RANGE, 240, 260, NULL);
  fail_unless (s1 != NULL);
  f1 = gst_caps_features_new ("memory:EGLImage", NULL);
  fail_unless (f1 != NULL);

  gst_caps_append_structure_full (c1, s1, f1);
  s2 = gst_caps_get_structure (c1, 0);
  fail_unless (s1 == s2);
  f2 = gst_caps_get_features (c1, 0);
  fail_unless (f1 == f2);

  str1 = gst_caps_to_string (c1);
  fail_unless (str1 != NULL);
  c2 = gst_caps_from_string (str1);
  fail_unless (c2 != NULL);
  g_free (str1);

  fail_unless (gst_caps_is_equal (c1, c2));
  fail_unless (gst_caps_is_subset (c1, c2));
  fail_unless (gst_caps_is_subset (c2, c1));
  fail_unless (gst_caps_can_intersect (c1, c2));

  gst_caps_unref (c2);

  c2 = gst_caps_new_empty ();
  fail_unless (c2 != NULL);
  s2 = gst_structure_new ("video/x-raw", "width", G_TYPE_INT, 320, "height",
      GST_TYPE_INT_RANGE, 240, 260, NULL);
  fail_unless (s2 != NULL);
  f2 = gst_caps_features_new ("memory:VASurface", "meta:VAMeta", NULL);
  fail_unless (f2 != NULL);
  gst_caps_append_structure_full (c2, s2, f2);

  fail_if (gst_caps_is_equal (c1, c2));
  fail_if (gst_caps_is_subset (c1, c2));
  fail_if (gst_caps_is_subset (c2, c1));
  fail_if (gst_caps_can_intersect (c1, c2));

  str1 = gst_caps_to_string (c2);
  fail_unless (str1 != NULL);
  c3 = gst_caps_from_string (str1);
  fail_unless (c3 != NULL);
  g_free (str1);

  fail_unless (gst_caps_is_equal (c2, c3));
  fail_unless (gst_caps_is_subset (c2, c3));
  fail_unless (gst_caps_is_subset (c3, c2));
  fail_unless (gst_caps_can_intersect (c2, c3));

  f1 = gst_caps_get_features (c3, 0);
  fail_unless (f1 != NULL);
  fail_if (f1 == f2);
  gst_caps_features_contains (f1, "memory:VASurface");
  gst_caps_features_remove (f1, "memory:VASurface");
  fail_if (gst_caps_is_equal (c2, c3));
  fail_if (gst_caps_is_subset (c2, c3));
  fail_if (gst_caps_is_subset (c3, c2));
  fail_if (gst_caps_can_intersect (c2, c3));

  gst_caps_unref (c3);
  gst_caps_unref (c2);

  c2 = gst_static_caps_get (&scaps);
  fail_unless (c2 != NULL);
  fail_unless (gst_caps_is_equal (c1, c2));
  fail_unless (gst_caps_is_subset (c1, c2));
  fail_unless (gst_caps_is_subset (c2, c1));
  fail_unless (gst_caps_can_intersect (c1, c2));
1208
  gst_caps_unref (c2);
1209

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  c2 = gst_caps_from_string
      ("video/x-raw(ANY), width=320, height=[ 240, 260 ]");
  fail_unless (c2 != NULL);
  fail_if (gst_caps_is_equal (c1, c2));
  fail_unless (gst_caps_is_subset (c1, c2));
  fail_if (gst_caps_is_subset (c2, c1));
  fail_unless (gst_caps_can_intersect (c1, c2));

  c3 = gst_caps_intersect (c1, c2);
  fail_unless (gst_caps_is_equal (c3, c1));

  gst_caps_unref (c3);
1222
  gst_caps_unref (c2);
1223
  gst_caps_unref (c1);
1224 1225 1226 1227 1228 1229 1230 1231

  c1 = gst_caps_from_string ("video/x-raw");
  c2 = gst_caps_from_string ("video/x-raw");

  f1 = gst_caps_get_features (c1, 0);
  gst_caps_features_add (f1, "memory:VASurface");

  fail_unless (gst_caps_features_is_equal (f1, gst_caps_get_features (c1, 0)));
1232
  fail_if (gst_caps_can_intersect (c1, c2));
1233 1234 1235 1236 1237 1238 1239

  f2 = gst_caps_get_features (c2, 0);
  fail_unless (gst_caps_features_is_equal
      (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY, f2));

  gst_caps_unref (c2);
  gst_caps_unref (c1);
1240 1241 1242 1243 1244 1245 1246

  c1 = gst_caps_from_string ("video/x-raw");
  f1 = gst_caps_get_features (c1, 0);
  f2 = gst_caps_features_new ("memory:dmabuf", NULL);
  gst_caps_set_features (c1, 0, f2);

  gst_caps_unref (c1);
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

  /* gst_caps_set_features_simple() */
  c1 = gst_caps_from_string
      ("video/x-raw, format=NV12; video/x-raw, format=NV16");
  fail_unless_equals_int (gst_caps_get_size (c1), 2);

  f1 = gst_caps_features_new ("memory:EGLImage", NULL);
  gst_caps_set_features_simple (c1, f1);

  f2 = gst_caps_get_features (c1, 0);
  fail_unless (gst_caps_features_is_equal (f1, f2));
  f2 = gst_caps_get_features (c1, 1);
  fail_unless (gst_caps_features_is_equal (f1, f2));

  gst_caps_unref (c1);
1262 1263 1264
}

GST_END_TEST;
1265

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
GST_START_TEST (test_special_caps)
{
  GstCaps *caps;

  caps = gst_caps_new_any ();
  fail_unless (gst_caps_is_any (caps));
  fail_unless (gst_caps_is_any (caps) == TRUE);
  fail_if (gst_caps_is_empty (caps));
  fail_unless (gst_caps_is_empty (caps) == FALSE);
  gst_caps_unref (caps);

  caps = gst_caps_new_empty ();
  fail_if (gst_caps_is_any (caps));
  fail_unless (gst_caps_is_any (caps) == FALSE);
  fail_unless (gst_caps_is_empty (caps));
  fail_unless (gst_caps_is_empty (caps) == TRUE);
  gst_caps_unref (caps);
}

GST_END_TEST;
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

static gboolean
foreach_append_function (GstCapsFeatures * features, GstStructure * structure,
    gpointer user_data)
{
  GstCaps *caps = user_data;

  gst_caps_append_structure_full (caps, gst_structure_copy (structure),
      features ? gst_caps_features_copy (features) : NULL);

  return TRUE;
}

GST_START_TEST (test_foreach)
{
  GstCaps *caps, *caps2;

  caps =
      gst_caps_from_string
      ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
  caps2 = gst_caps_new_empty ();
  fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
  fail_unless (gst_caps_is_strictly_equal (caps, caps2));
  gst_caps_unref (caps);
  gst_caps_unref (caps2);

  caps = gst_caps_new_empty ();
  caps2 = gst_caps_new_empty ();
  fail_unless (gst_caps_foreach (caps, foreach_append_function, caps2));
  fail_unless (gst_caps_is_strictly_equal (caps, caps2));
  gst_caps_unref (caps);
  gst_caps_unref (caps2);
}

GST_END_TEST;

static gboolean
map_function (GstCapsFeatures * features, GstStructure * structure,
    gpointer user_data)
{
  /* Remove caps features if there are any, otherwise add some dummy */
  if (gst_caps_features_contains (features, "foo:bar")) {
    gst_caps_features_remove (features, "foo:bar");
  } else {
    gst_caps_features_add (features, "foo:bar");
    gst_caps_features_remove (features, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
  }

  /* Set some dummy integer in the structure */
  gst_structure_set (structure, "foo", G_TYPE_INT, 123, NULL);

  return TRUE;
}

GST_START_TEST (test_map_in_place)
{
  GstCaps *caps, *caps2;

  caps =
      gst_caps_from_string
      ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
  caps2 =
      gst_caps_from_string
      ("video/x-raw(foo:bar), foo=(int)123, format=I420; video/x-raw, foo=(int)123; video/x-h264(foo:bar), foo=(int)123");
  fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
  fail_unless (gst_caps_is_strictly_equal (caps, caps2));
  gst_caps_unref (caps);
  gst_caps_unref (caps2);

  caps = gst_caps_new_empty ();
  caps2 = gst_caps_new_empty ();
  fail_unless (gst_caps_map_in_place (caps, map_function, NULL));
  fail_unless (gst_caps_is_strictly_equal (caps, caps2));
  gst_caps_unref (caps);
  gst_caps_unref (caps2);
}

GST_END_TEST;

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
static gboolean
filter_map_function (GstCapsFeatures * features, GstStructure * structure,
    gpointer user_data)
{
  if (!gst_structure_has_name (structure, "video/x-raw"))
    return FALSE;

  if (!gst_caps_features_contains (features, "foo:bar"))
    return FALSE;

  /* Set some dummy integer in the structure */
  gst_structure_set (structure, "foo", G_TYPE_INT, 123, NULL);

  return TRUE;
}

GST_START_TEST (test_filter_and_map_in_place)
{
  GstCaps *caps, *caps2;

  caps =
      gst_caps_from_string
      ("video/x-raw, format=I420; video/x-raw(foo:bar); video/x-h264");
  caps2 = gst_caps_from_string ("video/x-raw(foo:bar), foo=(int)123");
  gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
  fail_unless (gst_caps_is_strictly_equal (caps, caps2));
  gst_caps_unref (caps);
  gst_caps_unref (caps2);

  caps = gst_caps_from_string ("video/x-raw, format=I420; video/x-h264");
  caps2 = gst_caps_new_empty ();
  gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
  fail_unless (gst_caps_is_strictly_equal (caps, caps2));
  gst_caps_unref (caps);
  gst_caps_unref (caps2);

  caps = gst_caps_new_empty ();
  caps2 = gst_caps_new_empty ();
  gst_caps_filter_and_map_in_place (caps, filter_map_function, NULL);
  fail_unless (gst_caps_is_strictly_equal (caps, caps2));
  gst_caps_unref (caps);
  gst_caps_unref (caps2);
}

GST_END_TEST;

Henry Wilkes's avatar
Henry Wilkes committed
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
GST_START_TEST (test_equality)
{
  GstCaps *empty1, *empty2, *any1, *any2, *caps1, *caps2, *caps3, *caps4,
      *caps5, *caps6, *caps7, *caps8, *caps9;
  GstStructure *s;

  empty1 = gst_caps_new_empty ();

  empty2 = gst_caps_new_empty ();

  any1 = gst_caps_new_any ();

  any2 = gst_caps_new_any ();
  s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (s);
  gst_caps_append_structure (any2, s);

  caps1 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps1);

  caps2 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps2);
  /* append an identical structure */
  s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (s);
  gst_caps_append_structure (caps2, s);

  /* change field name */
  caps3 = gst_caps_new_simple ("structure", "intX", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps3);

  /* change field type */
  caps4 = gst_caps_new_simple ("structure", "int", G_TYPE_UINT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps4);

  /* change structure name */
  caps5 = gst_caps_new_simple ("structureX", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps5);

  /* change field value */
  caps6 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 3, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps6);

  /* change caps features */
  caps7 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps6);
  gst_caps_set_features (caps7, 0, gst_caps_features_new_any ());

  /* add structure */
  caps8 = gst_caps_new_simple ("structure", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (caps8);
  s = gst_structure_new ("structure2", "string", G_TYPE_STRING, "val", NULL);
  fail_unless (s);
  gst_caps_append_structure (caps8, s);

  /* reverse the order of the structures */
  caps9 = gst_caps_new_simple ("structure2", "string", G_TYPE_STRING, "val",
      NULL);
  fail_unless (caps9);
  s = gst_structure_new ("structure", "int", G_TYPE_INT, 4, "float",
      G_TYPE_FLOAT, 5.7, NULL);
  fail_unless (s);
  gst_caps_append_structure (caps9, s);

  fail_unless (gst_caps_is_equal (empty1, empty2) == TRUE);
  fail_unless (gst_caps_is_strictly_equal (empty1, empty2) == TRUE);
  fail_unless (gst_caps_is_equal (empty1, any1) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, any1) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, any2) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, any2) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps1) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps1) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps2) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps2) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps3) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps3) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps4) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps4) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps5) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps5) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps6) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps6) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps7) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps7) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps8) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps8) == FALSE);
  fail_unless (gst_caps_is_equal (empty1, caps9) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (empty1, caps9) == FALSE);

  fail_unless (gst_caps_is_equal (any1, any2) == TRUE);
  fail_unless (gst_caps_is_strictly_equal (any1, any2) == TRUE);
  fail_unless (gst_caps_is_equal (any1, caps1) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps1) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps2) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps2) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps3) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps3) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps4) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps4) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps5) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps5) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps6) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps6) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps7) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps7) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps8) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps8) == FALSE);
  fail_unless (gst_caps_is_equal (any1, caps9) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any1, caps9) == FALSE);

  fail_unless (gst_caps_is_equal (any2, caps1) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps1) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps2) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps2) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps3) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps3) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps4) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps4) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps5) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps5) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps6) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps6) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps7) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps7) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps8) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps8) == FALSE);
  fail_unless (gst_caps_is_equal (any2, caps9) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (any2, caps9) == FALSE);

  /* caps1 and caps2 are equal, but not strictly equal because one has
   * two copies of the same structure */
  fail_unless (gst_caps_is_equal (caps1, caps2) == TRUE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps2) == FALSE);
  fail_unless (gst_caps_is_equal (caps1, caps3) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps3) == FALSE);
  fail_unless (gst_caps_is_equal (caps1, caps4) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps4) == FALSE);
  fail_unless (gst_caps_is_equal (caps1, caps5) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps5) == FALSE);
  fail_unless (gst_caps_is_equal (caps1, caps6) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps6) == FALSE);
  fail_unless (gst_caps_is_equal (caps1, caps7) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps7) == FALSE);
  fail_unless (gst_caps_is_equal (caps1, caps8) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps8) == FALSE);
  fail_unless (gst_caps_is_equal (caps1, caps9) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps1, caps9) == FALSE);

  fail_unless (gst_caps_is_equal (caps2, caps3) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps2, caps3) == FALSE);
  fail_unless (gst_caps_is_equal (caps2, caps4) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps2, caps4) == FALSE);
  fail_unless (gst_caps_is_equal (caps2, caps5) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps2, caps5) == FALSE);
  fail_unless (gst_caps_is_equal (caps2, caps6) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps2, caps6) == FALSE);
  fail_unless (gst_caps_is_equal (caps2, caps7) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps2, caps7) == FALSE);
  fail_unless (gst_caps_is_equal (caps2, caps8) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps2, caps8) == FALSE);
  fail_unless (gst_caps_is_equal (caps2, caps9) == FALSE);
  fail_unless (gst_caps_is_strictly_equal (caps2, caps9) == FALSE);

  /* caps8 and caps9 are equal, but not strictly equal because their
   * order of structures is different */
  fail_unless (gst_caps_is_equal (caps8, caps9) == TRUE);
  fail_unless (gst_caps_is_strictly_equal (caps8, caps9) == FALSE);

  gst_caps_unref (empty1);
  gst_caps_unref (empty2);
  gst_caps_unref (any1);
  gst_caps_unref (any2);
  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
  gst_caps_unref (caps3);
  gst_caps_unref (caps4);
  gst_caps_unref (caps5);
  gst_caps_unref (caps6);
  gst_caps_unref (caps7);
  gst_caps_unref (caps8);
  gst_caps_unref (caps9);
}

GST_END_TEST;


1607
static Suite *
Wim Taymans's avatar
Wim Taymans committed
1608 1609 1610
gst_caps_suite (void)
{
  Suite *s = suite_create ("GstCaps");
Stefan Kost's avatar
Stefan Kost committed
1611
  TCase *tc_chain = tcase_create ("operations");
Wim Taymans's avatar
Wim Taymans committed
1612 1613

  suite_add_tcase (s, tc_chain);
1614
  tcase_add_test (tc_chain, test_from_string);
Wim Taymans's avatar
Wim Taymans committed
1615 1616
  tcase_add_test (tc_chain, test_double_append);
  tcase_add_test (tc_chain, test_mutability);
1617
  tcase_add_test (tc_chain, test_static_caps);
1618
  tcase_add_test (tc_chain, test_simplify);
1619
  tcase_add_test (tc_chain, test_truncate);
1620
  tcase_add_test (tc_chain, test_subset);
1621
  tcase_add_test (tc_chain, test_subset_duplication);
1622 1623 1624
  tcase_add_test (tc_chain, test_merge_fundamental);
  tcase_add_test (tc_chain, test_merge_same);
  tcase_add_test (tc_chain, test_merge_subset);
1625
  tcase_add_test (tc_chain, test_intersect);
1626
  tcase_add_test (tc_chain, test_intersect2);
1627
  tcase_add_test (tc_chain, test_intersect_list_duplicate);
1628 1629 1630
  tcase_add_test (tc_chain, test_intersect_zigzag);
  tcase_add_test (tc_chain, test_intersect_first);
  tcase_add_test (tc_chain, test_intersect_first2);
1631
  tcase_add_test (tc_chain, test_intersect_duplication);
Jan Schmidt's avatar
Jan Schmidt committed
1632 1633
  tcase_add_test (tc_chain, test_intersect_flagset);
  tcase_add_test (tc_chain, test_union);
1634
  tcase_add_test (tc_chain, test_normalize);
1635
  tcase_add_test (tc_chain, test_broken);
1636
  tcase_add_test (tc_chain, test_features);