adding gstdata tests, cleaning up source files

Original commit message from CVS:
adding gstdata tests, cleaning up source files
This commit is contained in:
Thomas Vander Stichele 2004-12-21 15:51:09 +00:00
parent 8f8da12228
commit 1fece171ea
9 changed files with 456 additions and 33 deletions

View file

@ -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:

View file

@ -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
View 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;
}

View file

@ -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__ */

View file

@ -43,11 +43,12 @@ gst_data_get_type (void)
* @data: a #GstData to initialize
* @type: the type of this data
* @flags: flags for this data
* @free: a free function
* @copy: a copy function
* @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
@ -77,7 +78,7 @@ gst_data_copy_into (const GstData * data, GstData * target)
* gst_data_dispose:
* @data: a #GstData to dispose
*
* Free all the resources allocated in the gst_data_init() function,
* Free all the resources allocated in the gst_data_init() function,
* mainly used by subclass implementors.
*/
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.
@ -143,11 +144,12 @@ gst_data_is_writable (GstData * data)
* Copies the given #GstData if the refcount is greater than 1 so that the
* #GstData object can be written to safely.
*
* Returns: a copy of the data if the refcount is > 1 or the buffer is
* 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)

View file

@ -39,22 +39,22 @@ G_BEGIN_DECLS
#define GST_DATA_FLAG_SHIFT(flag) (1<<(flag))
#define GST_DATA_FLAG_IS_SET(data,flag) (GST_DATA_FLAGS(data) & (1<<(flag)))
#define GST_DATA_FLAG_SET(data,flag) G_STMT_START{ (GST_DATA_FLAGS(data) |= (1<<(flag))); }G_STMT_END
#define GST_DATA_FLAG_UNSET(data,flag) G_STMT_START{ (GST_DATA_FLAGS(data) &= ~(1<<(flag))); }G_STMT_END
#define GST_DATA_FLAG_UNSET(data,flag) G_STMT_START{ (GST_DATA_FLAGS(data) &= ~(1<<(flag))); }G_STMT_END
/* Macros for the GType */
#define GST_TYPE_DATA (gst_data_get_type ())
typedef struct _GstData GstData;
typedef void (*GstDataFreeFunction) (GstData *data);
typedef void (*GstDataFreeFunction) (GstData *data);
typedef GstData* (*GstDataCopyFunction) (const GstData *data);
typedef enum
{
GST_DATA_READONLY = 1,
GST_DATA_READONLY = 1,
/* insert more */
GST_DATA_FLAG_LAST = 8
GST_DATA_FLAG_LAST = 8
} GstDataFlags;
/* refcount */
@ -62,23 +62,23 @@ typedef enum
#define GST_DATA_REFCOUNT_VALUE(data) (gst_atomic_int_read (&(GST_DATA(data))->refcount))
/* copy/free functions */
#define GST_DATA_COPY_FUNC(data) (GST_DATA(data)->copy)
#define GST_DATA_FREE_FUNC(data) (GST_DATA(data)->free)
#define GST_DATA_COPY_FUNC(data) (GST_DATA(data)->copy)
#define GST_DATA_FREE_FUNC(data) (GST_DATA(data)->free)
struct _GstData {
GType type;
GType type;
/*< public >*/ /* with COW */
/* refcounting */
GstAtomicInt refcount;
guint16 flags;
/*< protected >*/
/* utility function pointers, can override default */
GstDataFreeFunction free; /* free the data */
GstDataCopyFunction copy; /* copy the data */
GstDataFreeFunction free; /* free the data */
GstDataCopyFunction copy; /* copy the data */
/*< private >*/
gpointer _gst_reserved[GST_PADDING];
@ -92,9 +92,9 @@ void gst_data_dispose (GstData *data);
void gst_data_copy_into (const GstData *data, GstData *target);
/* basic operations on data */
GstData* gst_data_copy (const GstData *data);
gboolean gst_data_is_writable (GstData *data);
GstData* gst_data_copy_on_write (GstData *data);
GstData* gst_data_copy (const GstData *data);
gboolean gst_data_is_writable (GstData *data);
GstData* gst_data_copy_on_write (GstData *data);
/* reference counting */
GstData* gst_data_ref (GstData* data);

View file

@ -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
View 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;
}

View file

@ -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__ */