check/gst/gstpad.c (test_get_allowed_caps, test_refcount): Fix borken refcounting.
[platform/upstream/gstreamer.git] / tests / check / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * gstpad.c: Unit test for GstPad
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "../gstcheck.h"
23
24 START_TEST (test_link)
25 {
26   GstPad *src, *sink;
27   GstPadTemplate *srct;
28
29   GstPadLinkReturn ret;
30   gchar *name;
31
32   src = gst_pad_new ("source", GST_PAD_SRC);
33   fail_if (src == NULL);
34
35   name = gst_pad_get_name (src);
36   fail_unless (strcmp (name, "source") == 0);
37
38   sink = gst_pad_new ("sink", GST_PAD_SINK);
39   fail_if (sink == NULL);
40
41   /* linking without templates or caps should fail */
42   ret = gst_pad_link (src, sink);
43   fail_unless (ret == GST_PAD_LINK_NOFORMAT);
44
45   ASSERT_CRITICAL (gst_pad_get_pad_template (NULL));
46
47   srct = gst_pad_get_pad_template (src);
48   fail_unless (srct == NULL);
49 }
50
51 END_TEST
52 /* threaded link/unlink */
53 /* use globals */
54     GstPad * src, *sink;
55
56 void
57 thread_link_unlink (gpointer data)
58 {
59   THREAD_START ();
60
61   while (THREAD_TEST_RUNNING ()) {
62     gst_pad_link (src, sink);
63     gst_pad_unlink (src, sink);
64     THREAD_SWITCH ();
65   }
66 }
67
68 START_TEST (test_link_unlink_threaded)
69 {
70   GstCaps *caps;
71   int i;
72
73   src = gst_pad_new ("source", GST_PAD_SRC);
74   fail_if (src == NULL);
75   sink = gst_pad_new ("sink", GST_PAD_SINK);
76   fail_if (sink == NULL);
77
78   caps = gst_caps_new_any ();
79   gst_pad_set_caps (src, caps);
80   gst_pad_set_caps (sink, caps);
81
82   MAIN_START_THREADS (5, thread_link_unlink, NULL);
83   for (i = 0; i < 1000; ++i) {
84     gst_pad_is_linked (src);
85     gst_pad_is_linked (sink);
86     THREAD_SWITCH ();
87   }
88   MAIN_STOP_THREADS ();
89 }
90
91 END_TEST
92 START_TEST (test_refcount)
93 {
94   GstPad *src, *sink;
95   GstCaps *caps;
96   GstPadLinkReturn plr;
97
98   sink = gst_pad_new ("sink", GST_PAD_SINK);
99   fail_if (sink == NULL);
100
101   src = gst_pad_new ("src", GST_PAD_SRC);
102   fail_if (src == NULL);
103
104   caps = gst_caps_new_any ();
105   /* one for me */
106   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
107
108   gst_pad_set_caps (src, caps);
109   gst_pad_set_caps (sink, caps);
110   /* one for me and one for each set_caps */
111   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
112
113   plr = gst_pad_link (src, sink);
114   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
115   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
116
117   gst_pad_unlink (src, sink);
118   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
119
120   /* cleanup */
121   gst_object_unref (GST_OBJECT (src));
122   gst_object_unref (GST_OBJECT (sink));
123   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
124
125   gst_caps_unref (caps);
126 }
127
128 END_TEST
129 START_TEST (test_get_allowed_caps)
130 {
131   GstPad *src, *sink;
132   GstCaps *caps, *gotcaps;
133   GstBuffer *buffer;
134   GstPadLinkReturn plr;
135
136   ASSERT_CRITICAL (gst_pad_get_allowed_caps (NULL));
137
138   buffer = gst_buffer_new ();
139   ASSERT_CRITICAL (gst_pad_get_allowed_caps ((GstPad *) buffer));
140   gst_buffer_unref (buffer);
141
142   sink = gst_pad_new ("sink", GST_PAD_SINK);
143   ASSERT_CRITICAL (gst_pad_get_allowed_caps (sink));
144
145   src = gst_pad_new ("src", GST_PAD_SRC);
146   fail_if (src == NULL);
147   caps = gst_pad_get_allowed_caps (src);
148   fail_unless (caps == NULL);
149
150   caps = gst_caps_new_any ();
151
152   gst_pad_set_caps (src, caps);
153   gst_pad_set_caps (sink, caps);
154   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
155
156   plr = gst_pad_link (src, sink);
157   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
158
159   gotcaps = gst_pad_get_allowed_caps (src);
160   fail_if (gotcaps == NULL);
161   fail_unless (gst_caps_is_equal (gotcaps, caps));
162
163   ASSERT_CAPS_REFCOUNT (gotcaps, "gotcaps", 1);
164   gst_caps_unref (gotcaps);
165
166   gst_pad_unlink (src, sink);
167
168   /* cleanup */
169   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
170   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
171   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
172
173   gst_object_unref (GST_OBJECT (src));
174   gst_object_unref (GST_OBJECT (sink));
175
176   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
177   gst_caps_unref (caps);
178 }
179
180 END_TEST Suite *
181 gst_pad_suite (void)
182 {
183   Suite *s = suite_create ("GstPad");
184   TCase *tc_chain = tcase_create ("general");
185
186   /* turn off timeout */
187   tcase_set_timeout (tc_chain, 60);
188
189   suite_add_tcase (s, tc_chain);
190   tcase_add_test (tc_chain, test_link);
191   tcase_add_test (tc_chain, test_refcount);
192   tcase_add_test (tc_chain, test_get_allowed_caps);
193   tcase_add_test (tc_chain, test_link_unlink_threaded);
194   return s;
195 }
196
197 int
198 main (int argc, char **argv)
199 {
200   int nf;
201
202   Suite *s = gst_pad_suite ();
203   SRunner *sr = srunner_create (s);
204
205   gst_check_init (&argc, &argv);
206
207   srunner_run_all (sr, CK_NORMAL);
208   nf = srunner_ntests_failed (sr);
209   srunner_free (sr);
210
211   return nf;
212 }