mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-06-06 07:28:53 +00:00
adding gstdata tests, cleaning up source files
Original commit message from CVS: adding gstdata tests, cleaning up source files
This commit is contained in:
parent
8f8da12228
commit
1fece171ea
9 changed files with 456 additions and 33 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
|||
2004-12-21 Thomas Vander Stichele <thomas at apestaart dot org>
|
||||
|
||||
* check/Makefile.am:
|
||||
* check/gst/gstdata.c:
|
||||
* check/gst/gstobject.c:
|
||||
* check/gstcheck.h:
|
||||
* gst/gstdata.c:
|
||||
* gst/gstdata.h:
|
||||
adding gstdata tests, cleaning up source files
|
||||
|
||||
2004-12-21 Thomas Vander Stichele <thomas at apestaart dot org>
|
||||
|
||||
* check/Makefile.am:
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
TESTS = gst/gstobject gst-libs/gdp
|
||||
TESTS = \
|
||||
gst/gstdata \
|
||||
gst/gstobject \
|
||||
gst-libs/gdp
|
||||
|
||||
check_PROGRAMS = $(TESTS)
|
||||
|
||||
|
|
196
check/gst/gstdata.c
Normal file
196
check/gst/gstdata.c
Normal file
|
@ -0,0 +1,196 @@
|
|||
/* 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 ();
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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_init (&argc, &argv);
|
||||
gst_check_init ();
|
||||
|
||||
srunner_run_all (sr, CK_NORMAL);
|
||||
nf = srunner_ntests_failed (sr);
|
||||
srunner_free (sr);
|
||||
|
||||
return nf;
|
||||
}
|
|
@ -125,6 +125,12 @@ G_STMT_START { \
|
|||
g_mutex_unlock (mutex); \
|
||||
} G_STMT_END;
|
||||
|
||||
#define THREAD_SWITCH() \
|
||||
G_STMT_START { \
|
||||
/* a minimal sleep is a context switch */ \
|
||||
g_usleep (1); \
|
||||
} G_STMT_END;
|
||||
|
||||
#define THREAD_TEST_RUNNING() (_gst_check_threads_running == TRUE)
|
||||
|
||||
#endif /* __GST_CHECK_H__ */
|
||||
|
|
|
@ -46,8 +46,9 @@ gst_data_get_type (void)
|
|||
* @free: a free function
|
||||
* @copy: a copy function
|
||||
*
|
||||
* Initialize the given data structure with the given parameters. The free and copy
|
||||
* function will be called when this data is freed or copied respectively.
|
||||
* Initialize the given data structure with the given parameters.
|
||||
* The free and copy function will be called when this data is freed
|
||||
* or copied, respectively.
|
||||
*/
|
||||
void
|
||||
gst_data_init (GstData * data, GType type, guint16 flags,
|
||||
|
@ -64,7 +65,7 @@ gst_data_init (GstData * data, GType type, guint16 flags,
|
|||
* @target: the target #GstData to copy into
|
||||
*
|
||||
* Copy the GstData into the specified target GstData structure.
|
||||
* Thos method is mainly used by subclasses when they want to copy
|
||||
* This method is mainly used by subclasses when they want to copy
|
||||
* the relevant GstData info.
|
||||
*/
|
||||
void
|
||||
|
@ -95,9 +96,9 @@ gst_data_dispose (GstData * data)
|
|||
* Copies the given #GstData. This function will call the custom subclass
|
||||
* copy function or return NULL if no function was provided by the subclass.
|
||||
*
|
||||
* Returns: a copy of the data or NULL if the data cannot be copied. The refcount
|
||||
* of the original buffer is not changed so you should unref it when you don't
|
||||
* need it anymore.
|
||||
* Returns: a copy of the data or NULL if the data cannot be copied.
|
||||
* The refcount of the original buffer is not changed so you should unref it
|
||||
* when you don't need it anymore.
|
||||
*/
|
||||
GstData *
|
||||
gst_data_copy (const GstData * data)
|
||||
|
@ -112,9 +113,9 @@ gst_data_copy (const GstData * data)
|
|||
|
||||
/**
|
||||
* gst_data_is_writable:
|
||||
* @data: a #GstData to copy
|
||||
* @data: a #GstData to check
|
||||
*
|
||||
* Query if the gstdata needs to be copied before it can safely be modified.
|
||||
* Query if the data needs to be copied before it can safely be modified.
|
||||
*
|
||||
* Returns: FALSE if the given #GstData is potentially shared and needs to
|
||||
* be copied before it can be modified safely.
|
||||
|
@ -145,9 +146,10 @@ gst_data_is_writable (GstData * data)
|
|||
*
|
||||
* Returns: a copy of the data if the refcount is > 1 or the buffer is
|
||||
* marked READONLY, data if the refcount == 1,
|
||||
* or NULL if the data could not be copied. The refcount of the original buffer
|
||||
* is decreased when a copy is made, so you are not supposed to use it after a
|
||||
* call to this function.
|
||||
* or NULL if the data could not be copied.
|
||||
*
|
||||
* The refcount of the passed @data is decreased when a copy is made, so
|
||||
* you are not supposed to use it anymore after a call to this function.
|
||||
*/
|
||||
GstData *
|
||||
gst_data_copy_on_write (GstData * data)
|
||||
|
@ -224,9 +226,10 @@ gst_data_ref_by_count (GstData * data, gint count)
|
|||
* Decrements the refcount of this data. If the refcount is
|
||||
* zero, the data will be freed.
|
||||
*
|
||||
* When you add data to a pipeline, the pipeline takes ownership of the
|
||||
* data. When the data has been used by some plugin, it must unref()s it.
|
||||
* Applications usually don't need to unref() anything.
|
||||
* When you move data out of your element into the pipeline,
|
||||
* the pipeline takes ownership of the
|
||||
* data. When the data has been consumed by some element, it must unref() it.
|
||||
* Applications usually don't need to unref() @data.
|
||||
*/
|
||||
void
|
||||
gst_data_unref (GstData * data)
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
TESTS = gst/gstobject gst-libs/gdp
|
||||
TESTS = \
|
||||
gst/gstdata \
|
||||
gst/gstobject \
|
||||
gst-libs/gdp
|
||||
|
||||
check_PROGRAMS = $(TESTS)
|
||||
|
||||
|
|
196
tests/check/gst/gstdata.c
Normal file
196
tests/check/gst/gstdata.c
Normal file
|
@ -0,0 +1,196 @@
|
|||
/* 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 ();
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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_init (&argc, &argv);
|
||||
gst_check_init ();
|
||||
|
||||
srunner_run_all (sr, CK_NORMAL);
|
||||
nf = srunner_ntests_failed (sr);
|
||||
srunner_free (sr);
|
||||
|
||||
return nf;
|
||||
}
|
|
@ -125,6 +125,12 @@ G_STMT_START { \
|
|||
g_mutex_unlock (mutex); \
|
||||
} G_STMT_END;
|
||||
|
||||
#define THREAD_SWITCH() \
|
||||
G_STMT_START { \
|
||||
/* a minimal sleep is a context switch */ \
|
||||
g_usleep (1); \
|
||||
} G_STMT_END;
|
||||
|
||||
#define THREAD_TEST_RUNNING() (_gst_check_threads_running == TRUE)
|
||||
|
||||
#endif /* __GST_CHECK_H__ */
|
||||
|
|
Loading…
Reference in a new issue