Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / debugutils / tests.c
1 /* GStreamer
2  * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
3  *
4  * includes code based on glibc 2.2.3's crypt/md5.c,
5  * Copyright (C) 1995, 1996, 1997, 1999, 2000 Free Software Foundation, Inc. 
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "tests.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32
33 /*
34  *** LENGTH ***
35  */
36
37 typedef struct
38 {
39   gint64 value;
40 }
41 LengthTest;
42
43 static GParamSpec *
44 length_get_spec (const GstTestInfo * info, gboolean compare_value)
45 {
46   if (compare_value) {
47     return g_param_spec_int64 ("expected-length", "expected length",
48         "expected length of stream", -1, G_MAXINT64, -1,
49         G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
50   } else {
51     return g_param_spec_int64 ("length", "length", "length of stream",
52         -1, G_MAXINT64, -1, G_PARAM_READABLE);
53   }
54 }
55
56 static gpointer
57 length_new (const GstTestInfo * info)
58 {
59   return g_new0 (LengthTest, 1);
60 }
61
62 static void
63 length_add (gpointer test, GstBuffer * buffer)
64 {
65   LengthTest *t = test;
66
67   t->value += gst_buffer_get_size (buffer);
68 }
69
70 static gboolean
71 length_finish (gpointer test, GValue * value)
72 {
73   LengthTest *t = test;
74
75   if (g_value_get_int64 (value) == -1)
76     return TRUE;
77
78   return t->value == g_value_get_int64 (value);
79 }
80
81 static void
82 length_get_value (gpointer test, GValue * value)
83 {
84   LengthTest *t = test;
85
86   g_value_set_int64 (value, t ? t->value : -1);
87 }
88
89 /*
90  *** BUFFER COUNT ***
91  */
92
93 static GParamSpec *
94 buffer_count_get_spec (const GstTestInfo * info, gboolean compare_value)
95 {
96   if (compare_value) {
97     return g_param_spec_int64 ("expected-buffer-count", "expected buffer count",
98         "expected number of buffers in stream",
99         -1, G_MAXINT64, -1, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
100   } else {
101     return g_param_spec_int64 ("buffer-count", "buffer count",
102         "number of buffers in stream", -1, G_MAXINT64, -1, G_PARAM_READABLE);
103   }
104 }
105
106 static void
107 buffer_count_add (gpointer test, GstBuffer * buffer)
108 {
109   LengthTest *t = test;
110
111   t->value++;
112 }
113
114 /*
115  *** TIMESTAMP / DURATION MATCHING ***
116  */
117
118 typedef struct
119 {
120   guint64 diff;
121   guint count;
122   GstClockTime expected;
123 }
124 TimeDurTest;
125
126 static GParamSpec *
127 timedur_get_spec (const GstTestInfo * info, gboolean compare_value)
128 {
129   if (compare_value) {
130     return g_param_spec_int64 ("allowed-timestamp-deviation",
131         "allowed timestamp deviation",
132         "allowed average difference in usec between timestamp of next buffer "
133         "and expected timestamp from analyzing last buffer",
134         -1, G_MAXINT64, -1, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
135   } else {
136     return g_param_spec_int64 ("timestamp-deviation",
137         "timestamp deviation",
138         "average difference in usec between timestamp of next buffer "
139         "and expected timestamp from analyzing last buffer",
140         -1, G_MAXINT64, -1, G_PARAM_READABLE);
141   }
142 }
143
144 static gpointer
145 timedur_new (const GstTestInfo * info)
146 {
147   TimeDurTest *ret = g_new0 (TimeDurTest, 1);
148
149   ret->expected = GST_CLOCK_TIME_NONE;
150
151   return ret;
152 }
153
154 static void
155 timedur_add (gpointer test, GstBuffer * buffer)
156 {
157   TimeDurTest *t = test;
158
159   if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
160       GST_CLOCK_TIME_IS_VALID (t->expected)) {
161     t->diff += labs (GST_BUFFER_TIMESTAMP (buffer) - t->expected);
162     t->count++;
163   }
164   if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
165       GST_BUFFER_DURATION_IS_VALID (buffer)) {
166     t->expected = GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
167   } else {
168     t->expected = GST_CLOCK_TIME_NONE;
169   }
170 }
171
172 static gboolean
173 timedur_finish (gpointer test, GValue * value)
174 {
175   TimeDurTest *t = test;
176
177   if (g_value_get_int64 (value) == -1)
178     return TRUE;
179
180   return (t->diff / MAX (1, t->count)) <= g_value_get_int64 (value);
181 }
182
183 static void
184 timedur_get_value (gpointer test, GValue * value)
185 {
186   TimeDurTest *t = test;
187
188   g_value_set_int64 (value, t ? (t->diff / MAX (1, t->count)) : -1);
189 }
190
191 /*
192  *** MD5 ***
193  */
194
195 static GParamSpec *
196 md5_get_spec (const GstTestInfo * info, gboolean compare_value)
197 {
198   if (compare_value) {
199     return g_param_spec_string ("expected-md5", "expected md5",
200         "expected md5 of processing the whole data",
201         "---", G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
202   } else {
203     return g_param_spec_string ("md5", "md5",
204         "md5 of processing the whole data", "---", G_PARAM_READABLE);
205   }
206 }
207
208 static gpointer
209 md5_new (const GstTestInfo * info)
210 {
211   return g_checksum_new (G_CHECKSUM_MD5);
212 }
213
214 static void
215 md5_add (gpointer checksum, GstBuffer * buffer)
216 {
217   guint8 *data;
218   gsize size;
219
220   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
221   g_checksum_update (checksum, data, size);
222   gst_buffer_unmap (buffer, data, size);
223 }
224
225 static gboolean
226 md5_finish (gpointer checksum, GValue * value)
227 {
228   const gchar *expected, *result;
229
230   expected = g_value_get_string (value);
231   result = g_checksum_get_string (checksum);
232
233   if (g_str_equal (expected, "---"))
234     return TRUE;
235   if (g_str_equal (expected, result))
236     return TRUE;
237   return FALSE;
238 }
239
240 static void
241 md5_get_value (gpointer checksum, GValue * value)
242 {
243   if (!checksum) {
244     g_value_set_string (value, "---");
245   } else {
246     g_value_set_string (value, g_checksum_get_string (checksum));
247   }
248 }
249
250 static void
251 md5_free (gpointer checksum)
252 {
253   g_checksum_free (checksum);
254 }
255
256 /*
257  *** TESTINFO ***
258  */
259
260 const GstTestInfo tests[] = {
261   {length_get_spec, length_new, length_add,
262       length_finish, length_get_value, g_free},
263   {buffer_count_get_spec, length_new, buffer_count_add,
264       length_finish, length_get_value, g_free},
265   {timedur_get_spec, timedur_new, timedur_add,
266       timedur_finish, timedur_get_value, g_free},
267   {md5_get_spec, md5_new, md5_add,
268       md5_finish, md5_get_value, md5_free}
269 };