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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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 *
length_get_spec(const GstTestInfo * info,gboolean compare_value)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
length_new(const GstTestInfo * info)57 length_new (const GstTestInfo * info)
58 {
59 return g_new0 (LengthTest, 1);
60 }
61
62 static void
length_add(gpointer test,GstBuffer * buffer)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
length_finish(gpointer test,GValue * value)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
length_get_value(gpointer test,GValue * value)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 *
buffer_count_get_spec(const GstTestInfo * info,gboolean compare_value)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
buffer_count_add(gpointer test,GstBuffer * buffer)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 *
timedur_get_spec(const GstTestInfo * info,gboolean compare_value)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
timedur_new(const GstTestInfo * info)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
timedur_add(gpointer test,GstBuffer * buffer)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 +=
162 ABS (GST_CLOCK_DIFF (t->expected, GST_BUFFER_TIMESTAMP (buffer)));
163 t->count++;
164 }
165 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
166 GST_BUFFER_DURATION_IS_VALID (buffer)) {
167 t->expected = GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
168 } else {
169 t->expected = GST_CLOCK_TIME_NONE;
170 }
171 }
172
173 static gboolean
timedur_finish(gpointer test,GValue * value)174 timedur_finish (gpointer test, GValue * value)
175 {
176 TimeDurTest *t = test;
177
178 if (g_value_get_int64 (value) == -1)
179 return TRUE;
180
181 return (t->diff / MAX (1, t->count)) <= g_value_get_int64 (value);
182 }
183
184 static void
timedur_get_value(gpointer test,GValue * value)185 timedur_get_value (gpointer test, GValue * value)
186 {
187 TimeDurTest *t = test;
188
189 g_value_set_int64 (value, t ? (t->diff / MAX (1, t->count)) : -1);
190 }
191
192 /*
193 *** MD5 ***
194 */
195
196 static GParamSpec *
md5_get_spec(const GstTestInfo * info,gboolean compare_value)197 md5_get_spec (const GstTestInfo * info, gboolean compare_value)
198 {
199 if (compare_value) {
200 return g_param_spec_string ("expected-md5", "expected md5",
201 "expected md5 of processing the whole data",
202 "---", G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
203 } else {
204 return g_param_spec_string ("md5", "md5",
205 "md5 of processing the whole data", "---", G_PARAM_READABLE);
206 }
207 }
208
209 static gpointer
md5_new(const GstTestInfo * info)210 md5_new (const GstTestInfo * info)
211 {
212 return g_checksum_new (G_CHECKSUM_MD5);
213 }
214
215 static void
md5_add(gpointer checksum,GstBuffer * buffer)216 md5_add (gpointer checksum, GstBuffer * buffer)
217 {
218 GstMapInfo map;
219
220 gst_buffer_map (buffer, &map, GST_MAP_READ);
221 g_checksum_update (checksum, map.data, map.size);
222 gst_buffer_unmap (buffer, &map);
223 }
224
225 static gboolean
md5_finish(gpointer checksum,GValue * value)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
md5_get_value(gpointer checksum,GValue * value)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
md5_free(gpointer checksum)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 };
270