gstdata.c 5.01 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* GStreamer
 *
 * unit test for GstData
 *
 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "../gstcheck.h"

START_TEST (test_copy)
{
  GstBuffer *buffer, *copy;

  buffer = gst_buffer_new_and_alloc (4);

  copy = GST_BUFFER (gst_data_copy (GST_DATA (buffer)));

  fail_if (copy == NULL, "Copy of buffer returned NULL");
  fail_unless (GST_BUFFER_SIZE (copy) == 4,
      "Copy of buffer has different size");
}

END_TEST
START_TEST (test_is_writable)
{
  GstBuffer *buffer;
  GstData *data;

  buffer = gst_buffer_new_and_alloc (4);
  data = GST_DATA (buffer);

  fail_unless (gst_data_is_writable (data),
      "A buffer with one ref should be writable");

  GST_DATA_FLAG_SET (data, GST_DATA_READONLY);
  fail_if (gst_data_is_writable (data),
      "A buffer with READONLY set should not be writable");
  GST_DATA_FLAG_UNSET (data, GST_DATA_READONLY);
  fail_unless (gst_data_is_writable (data),
      "A buffer with one ref and READONLY not set should be writable");

  fail_if (gst_data_ref (data) == NULL, "Could not ref the data");

  fail_if (gst_data_is_writable (data),
      "A buffer with two refs should not be writable");
}

END_TEST
START_TEST (test_copy_on_write)
{
  GstBuffer *buffer;
  GstData *data, *data2, *data3;

  buffer = gst_buffer_new_and_alloc (4);
  data = GST_DATA (buffer);

  data2 = gst_data_copy_on_write (data);
  fail_unless (GST_IS_BUFFER (data2), "copy_on_write did not return a buffer");
  fail_unless (data == data2,
      "copy_on_write returned a copy for a buffer with refcount 1");

  data2 = gst_data_ref (data);
  data3 = gst_data_copy_on_write (data);
  fail_unless (GST_IS_BUFFER (data3), "copy_on_write did not return a buffer");
  fail_if (data == data3,
      "copy_on_write returned same object for a buffer with refcount > 1");

  fail_unless (GST_DATA_REFCOUNT_VALUE (data) == 1,
      "refcount of original data object should be back to 1");

  data2 = gst_data_copy_on_write (data);
  fail_unless (GST_IS_BUFFER (data2), "copy_on_write did not return a buffer");
  fail_unless (data == data2,
      "copy_on_write returned a copy for a buffer with refcount 1");

}

END_TEST gint num_threads = 10;
gint refs_per_thread = 10000;

/* test thread-safe refcounting of GstData */
void
thread_ref (GstData * data)
{
  int j;

  THREAD_START ();

  for (j = 0; j < refs_per_thread; ++j) {
    fail_if (gst_data_ref (data) == NULL, "Could not ref data from thread");

    if (j % num_threads == 0)
      THREAD_SWITCH ();
  }
110
  g_message ("thread stopped\n");
Wim Taymans's avatar
Wim Taymans committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

START_TEST (test_ref_threaded)
{
  GstBuffer *buffer;
  GstData *data;
  gint expected;

  buffer = gst_buffer_new_and_alloc (4);

  data = GST_DATA (buffer);

  MAIN_START_THREADS (num_threads, thread_ref, data);

  MAIN_STOP_THREADS ();

  expected = num_threads * refs_per_thread + 1;
  fail_unless (GST_DATA_REFCOUNT_VALUE (data) == expected,
      "Refcount of data is %d != %d", GST_DATA_REFCOUNT_VALUE (data), expected);
}
END_TEST void
thread_unref (GstData * data)
{
  int j;

  THREAD_START ();

  for (j = 0; j < refs_per_thread; ++j) {
    gst_data_unref (data);

    if (j % num_threads == 0)
      THREAD_SWITCH ();
  }
}

START_TEST (test_unref_threaded)
{
  GstBuffer *buffer;
  GstData *data;

  buffer = gst_buffer_new_and_alloc (4);

  data = GST_DATA (buffer);

  gst_data_ref_by_count (data, num_threads * refs_per_thread);

  MAIN_START_THREADS (num_threads, thread_unref, data);

  MAIN_STOP_THREADS ();

  fail_unless (GST_DATA_REFCOUNT_VALUE (data) == 1,
      "Refcount of data is %d != %d", GST_DATA_REFCOUNT_VALUE (data), 1);

  /* final unref */
  gst_data_unref (data);
}
END_TEST Suite *
gst_data_suite (void)
{
  Suite *s = suite_create ("GstData");
  TCase *tc_chain = tcase_create ("general");

173
  /* turn off timeout */
Wim Taymans's avatar
Wim Taymans committed
174
  tcase_set_timeout (tc_chain, 60);
175

Wim Taymans's avatar
Wim Taymans committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_copy);
  tcase_add_test (tc_chain, test_is_writable);
  tcase_add_test (tc_chain, test_copy_on_write);
  tcase_add_test (tc_chain, test_ref_threaded);
  tcase_add_test (tc_chain, test_unref_threaded);
  return s;
}

int
main (int argc, char **argv)
{
  int nf;

  Suite *s = gst_data_suite ();
  SRunner *sr = srunner_create (s);

  gst_check_init (&argc, &argv);

  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}