3 * unit test for GstMemory
5 * Copyright (C) <2012> Wim Taymans <wim.taymans at gmail.com>
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.
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.
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.
27 #ifdef HAVE_VALGRIND_H
28 # include <valgrind/valgrind.h>
30 # define RUNNING_ON_VALGRIND FALSE
33 #include <gst/check/gstcheck.h>
35 GST_START_TEST (test_submemory)
37 GstMemory *memory, *sub;
38 gsize size, maxsize, ssize;
41 memory = gst_allocator_alloc (NULL, 4, 0);
43 /* check sizes, memory starts out empty */
44 data = gst_memory_map (memory, &size, &maxsize, GST_MAP_WRITE);
45 fail_unless (size == 4, "memory has wrong size");
46 fail_unless (maxsize >= 4, "memory has wrong size");
48 gst_memory_unmap (memory);
50 data = gst_memory_map (memory, &size, NULL, GST_MAP_READ);
52 sub = gst_memory_share (memory, 1, 2);
53 fail_if (sub == NULL, "share of memory returned NULL");
55 sdata = gst_memory_map (sub, &ssize, NULL, GST_MAP_READ);
56 fail_unless (ssize == 2, "submemory has wrong size");
57 fail_unless (memcmp (data + 1, sdata, 2) == 0,
58 "submemory contains the wrong data");
59 ASSERT_MEMORY_REFCOUNT (sub, "submemory", 1);
60 gst_memory_unmap (sub);
61 gst_memory_unref (sub);
63 /* create a submemory of size 0 */
64 sub = gst_memory_share (memory, 1, 0);
65 fail_if (sub == NULL, "share memory returned NULL");
66 sdata = gst_memory_map (sub, &ssize, NULL, GST_MAP_READ);
67 fail_unless (ssize == 0, "submemory has wrong size");
68 fail_unless (memcmp (data + 1, sdata, 0) == 0,
69 "submemory contains the wrong data");
70 ASSERT_MEMORY_REFCOUNT (sub, "submemory", 1);
71 gst_memory_unmap (sub);
72 gst_memory_unref (sub);
74 /* test if metadata is coppied, not a complete memory copy so only the
75 * timestamp and offset fields are copied. */
76 sub = gst_memory_share (memory, 0, 1);
77 fail_if (sub == NULL, "share of memory returned NULL");
78 fail_unless (gst_memory_get_sizes (sub, NULL, NULL) == 1,
79 "submemory has wrong size");
80 gst_memory_unref (sub);
82 /* test if metadata is coppied, a complete memory is copied so all the timing
83 * fields should be copied. */
84 sub = gst_memory_share (memory, 0, 4);
85 fail_if (sub == NULL, "share of memory returned NULL");
86 fail_unless (gst_memory_get_sizes (sub, NULL, NULL) == 4,
87 "submemory has wrong size");
90 gst_memory_unref (sub);
92 gst_memory_unmap (memory);
93 gst_memory_unref (memory);
98 GST_START_TEST (test_is_span)
100 GstMemory *memory, *sub1, *sub2;
102 memory = gst_allocator_alloc (NULL, 4, 0);
104 sub1 = gst_memory_share (memory, 0, 2);
105 fail_if (sub1 == NULL, "share of memory returned NULL");
107 sub2 = gst_memory_share (memory, 2, 2);
108 fail_if (sub2 == NULL, "share of memory returned NULL");
110 fail_if (gst_memory_is_span (memory, sub2, NULL) == TRUE,
111 "a parent memory can't be span");
113 fail_if (gst_memory_is_span (sub1, memory, NULL) == TRUE,
114 "a parent memory can't be span");
116 fail_if (gst_memory_is_span (sub1, sub2, NULL) == FALSE,
117 "two submemorys next to each other should be span");
120 gst_memory_unref (sub1);
121 gst_memory_unref (sub2);
122 gst_memory_unref (memory);
127 static const char ro_memory[] = "abcdefghijklmnopqrstuvwxyz";
130 create_read_only_memory (void)
134 /* assign some read-only data to the new memory */
135 mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
136 (gpointer) ro_memory, NULL, sizeof (ro_memory), 0, sizeof (ro_memory));
137 fail_if (gst_memory_is_writable (mem));
142 GST_START_TEST (test_writable)
144 GstMemory *mem, *mem2;
148 /* create read-only memory and try to write */
149 mem = create_read_only_memory ();
151 ASSERT_CRITICAL (gst_memory_map (mem, &size, NULL, GST_MAP_WRITE));
152 fail_if (gst_memory_is_writable (mem));
154 mem2 = gst_memory_copy (mem, 0, -1);
155 fail_if (gst_memory_is_writable (mem));
156 fail_unless (gst_memory_is_writable (mem2));
158 data = gst_memory_map (mem2, &size, NULL, GST_MAP_WRITE);
160 gst_memory_unmap (mem2);
162 gst_memory_ref (mem2);
163 ASSERT_CRITICAL (gst_memory_map (mem, &size, NULL, GST_MAP_WRITE));
164 gst_memory_unref (mem2);
166 data = gst_memory_map (mem2, &size, NULL, GST_MAP_WRITE);
168 gst_memory_unmap (mem2);
169 gst_memory_unref (mem2);
171 gst_memory_unref (mem);
176 GST_START_TEST (test_submemory_writable)
178 GstMemory *mem, *sub_mem;
181 /* create sub-memory of read-only memory and try to write */
182 mem = create_read_only_memory ();
184 sub_mem = gst_memory_share (mem, 0, 8);
185 fail_if (gst_memory_is_writable (sub_mem));
187 ASSERT_CRITICAL (gst_memory_map (mem, &size, NULL, GST_MAP_WRITE));
188 ASSERT_CRITICAL (gst_memory_map (sub_mem, &size, NULL, GST_MAP_WRITE));
190 gst_memory_unref (sub_mem);
191 gst_memory_unref (mem);
196 GST_START_TEST (test_copy)
198 GstMemory *memory, *copy;
201 memory = gst_allocator_alloc (NULL, 4, 0);
202 ASSERT_MEMORY_REFCOUNT (memory, "memory", 1);
204 copy = gst_memory_copy (memory, 0, -1);
205 ASSERT_MEMORY_REFCOUNT (memory, "memory", 1);
206 ASSERT_MEMORY_REFCOUNT (copy, "copy", 1);
207 /* memorys are copied and must point to different memory */
208 fail_if (memory == copy);
210 gst_memory_map (memory, &size, NULL, GST_MAP_READ);
211 gst_memory_map (copy, &ssize, NULL, GST_MAP_READ);
213 /* NOTE that data is refcounted */
214 fail_unless (size == ssize);
216 gst_memory_unmap (copy);
217 gst_memory_unmap (memory);
219 gst_memory_unref (copy);
220 gst_memory_unref (memory);
222 memory = gst_allocator_alloc (NULL, 0, 0);
223 gst_memory_map (memory, &size, NULL, GST_MAP_READ);
224 fail_unless (size == 0);
225 gst_memory_unmap (memory);
227 /* copying a 0-sized memory should not crash */
228 copy = gst_memory_copy (memory, 0, -1);
229 gst_memory_map (copy, &size, NULL, GST_MAP_READ);
230 fail_unless (size == 0);
231 gst_memory_unmap (copy);
233 gst_memory_unref (copy);
234 gst_memory_unref (memory);
239 GST_START_TEST (test_try_new_and_alloc)
245 mem = gst_allocator_alloc (NULL, 0, 0);
246 fail_unless (mem != NULL);
247 data = gst_memory_map (mem, &size, NULL, GST_MAP_READ);
248 fail_unless (size == 0);
249 gst_memory_unmap (mem);
250 gst_memory_unref (mem);
252 /* normal alloc should still work */
253 mem = gst_allocator_alloc (NULL, 640 * 480 * 4, 0);
254 fail_unless (mem != NULL);
255 data = gst_memory_map (mem, &size, NULL, GST_MAP_WRITE);
256 fail_unless (data != NULL);
257 fail_unless (size == (640 * 480 * 4));
258 data[640 * 479 * 4 + 479] = 0xff;
259 gst_memory_unmap (mem);
261 gst_memory_unref (mem);
264 /* Disabled this part of the test, because it happily succeeds on 64-bit
265 * machines that have enough memory+swap, because the address space is large
266 * enough. There's not really any way to test the failure case except by
267 * allocating chunks of memory until it fails, which would suck. */
269 /* now this better fail (don't run in valgrind, it will abort
270 * or warn when passing silly arguments to malloc) */
271 if (!RUNNING_ON_VALGRIND) {
272 mem = gst_allocator_alloc (NULL, (guint) - 1, 0);
273 fail_unless (mem == NULL);
280 GST_START_TEST (test_resize)
284 gsize size, maxsize, offset;
286 /* one memory block */
287 mem = gst_allocator_alloc (NULL, 100, 0);
289 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
290 fail_unless (size == 100);
291 fail_unless (offset == 0);
292 fail_unless (maxalloc >= 100);
294 ASSERT_CRITICAL (gst_memory_resize (mem, 200, 50));
295 ASSERT_CRITICAL (gst_memory_resize (mem, 0, 150));
296 ASSERT_CRITICAL (gst_memory_resize (mem, 1, maxalloc));
297 ASSERT_CRITICAL (gst_memory_resize (mem, maxalloc, 1));
299 /* this does nothing */
300 gst_memory_resize (mem, 0, 100);
302 /* nothing should have changed */
303 size = gst_memory_get_sizes (mem, &offset, &maxsize);
304 fail_unless (size == 100);
305 fail_unless (offset == 0);
306 fail_unless (maxsize == maxalloc);
308 gst_memory_resize (mem, 0, 50);
309 size = gst_memory_get_sizes (mem, &offset, &maxsize);
310 fail_unless (size == 50);
311 fail_unless (offset == 0);
312 fail_unless (maxsize == maxalloc);
314 gst_memory_resize (mem, 0, 100);
315 size = gst_memory_get_sizes (mem, &offset, &maxsize);
316 fail_unless (size == 100);
317 fail_unless (offset == 0);
318 fail_unless (maxsize == maxalloc);
320 gst_memory_resize (mem, 1, 99);
321 size = gst_memory_get_sizes (mem, &offset, &maxsize);
322 fail_unless (size == 99);
323 fail_unless (offset == 1);
324 fail_unless (maxsize == maxalloc);
326 ASSERT_CRITICAL (gst_memory_resize (mem, 1, maxalloc - 1));
328 gst_memory_resize (mem, 0, 99);
329 size = gst_memory_get_sizes (mem, &offset, &maxsize);
330 fail_unless (size == 99);
331 fail_unless (offset == 1);
332 fail_unless (maxsize == maxalloc);
334 gst_memory_resize (mem, -1, 100);
335 size = gst_memory_get_sizes (mem, &offset, &maxsize);
336 fail_unless (size == 100);
337 fail_unless (offset == 0);
338 fail_unless (maxsize == maxalloc);
340 /* can't set offset below 0 */
341 ASSERT_CRITICAL (gst_memory_resize (mem, -1, 100));
343 gst_memory_resize (mem, 50, 40);
344 size = gst_memory_get_sizes (mem, &offset, &maxsize);
345 fail_unless (size == 40);
346 fail_unless (offset == 50);
347 fail_unless (maxsize == maxalloc);
349 gst_memory_resize (mem, -50, 100);
350 size = gst_memory_get_sizes (mem, &offset, &maxsize);
351 fail_unless (size == 100);
352 fail_unless (offset == 0);
353 fail_unless (maxsize == maxalloc);
355 gst_memory_resize (mem, 0, 0);
356 size = gst_memory_get_sizes (mem, &offset, &maxsize);
357 fail_unless (size == 0);
358 fail_unless (offset == 0);
359 fail_unless (maxsize == maxalloc);
361 gst_memory_resize (mem, 0, 100);
362 size = gst_memory_get_sizes (mem, &offset, &maxsize);
363 fail_unless (size == 100);
364 fail_unless (offset == 0);
365 fail_unless (maxsize == maxalloc);
367 gst_memory_resize (mem, 0, 100);
368 size = gst_memory_get_sizes (mem, &offset, &maxsize);
369 fail_unless (size == 100);
370 fail_unless (offset == 0);
371 fail_unless (maxsize == maxalloc);
373 gst_memory_unref (mem);
378 GST_START_TEST (test_map)
382 gsize size, maxsize, offset;
385 /* one memory block */
386 mem = gst_allocator_alloc (NULL, 100, 0);
388 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
389 fail_unless (size == 100);
390 fail_unless (offset == 0);
391 fail_unless (maxalloc >= 100);
393 /* see if simply mapping works */
394 data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
395 fail_unless (data != NULL);
396 fail_unless (size == 100);
397 fail_unless (maxsize == maxalloc);
399 gst_memory_unmap (mem);
400 gst_memory_unref (mem);
405 GST_START_TEST (test_map_nested)
408 gsize size1, maxsize1, size2, maxsize2;
409 gpointer data1, data2;
411 mem = gst_allocator_alloc (NULL, 100, 0);
414 data1 = gst_memory_map (mem, &size1, &maxsize1, GST_MAP_READ);
415 fail_unless (data1 != NULL);
416 fail_unless (size1 == 100);
418 data2 = gst_memory_map (mem, &size2, &maxsize2, GST_MAP_READ);
419 fail_unless (data2 == data1);
420 fail_unless (size2 == 100);
423 gst_memory_unmap (mem);
424 gst_memory_unmap (mem);
426 data1 = gst_memory_map (mem, &size1, &maxsize1, GST_MAP_READ);
428 ASSERT_CRITICAL (gst_memory_map (mem, &size2, &maxsize2, GST_MAP_WRITE));
429 ASSERT_CRITICAL (gst_memory_map (mem, &size2, &maxsize2, GST_MAP_READWRITE));
430 data2 = gst_memory_map (mem, &size2, &maxsize2, GST_MAP_READ);
431 gst_memory_unmap (mem);
432 gst_memory_unmap (mem);
433 fail_unless (mem->state == 0);
435 data1 = gst_memory_map (mem, &size1, &maxsize1, GST_MAP_WRITE);
437 ASSERT_CRITICAL (gst_memory_map (mem, &size2, &maxsize2, GST_MAP_READ));
438 ASSERT_CRITICAL (gst_memory_map (mem, &size2, &maxsize2, GST_MAP_READWRITE));
439 data2 = gst_memory_map (mem, &size2, &maxsize2, GST_MAP_WRITE);
440 gst_memory_unmap (mem);
441 gst_memory_unmap (mem);
442 /* nothing was mapped */
443 ASSERT_CRITICAL (gst_memory_unmap (mem));
445 data1 = gst_memory_map (mem, &size1, &maxsize1, GST_MAP_READWRITE);
446 data2 = gst_memory_map (mem, &size2, &maxsize2, GST_MAP_READ);
447 gst_memory_unmap (mem);
448 data2 = gst_memory_map (mem, &size2, &maxsize2, GST_MAP_WRITE);
449 gst_memory_unmap (mem);
450 gst_memory_unmap (mem);
451 /* nothing was mapped */
452 ASSERT_CRITICAL (gst_memory_unmap (mem));
454 gst_memory_unref (mem);
459 GST_START_TEST (test_map_resize)
462 gsize size, maxsize, maxalloc, offset;
465 mem = gst_allocator_alloc (NULL, 100, 0);
468 data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
469 fail_unless (data != NULL);
470 fail_unless (size == 100);
472 /* resize the buffer */
473 gst_memory_resize (mem, 1, size - 1);
474 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
475 fail_unless (size == 99);
476 fail_unless (offset == 1);
477 fail_unless (maxalloc >= 100);
478 gst_memory_unmap (mem);
480 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
481 fail_unless (size == 99);
482 fail_unless (offset == 1);
483 fail_unless (maxalloc >= 100);
485 data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
486 fail_unless (data != NULL);
487 fail_unless (size == 99);
488 fail_unless (offset == 1);
489 fail_unless (maxsize >= 100);
490 gst_memory_unmap (mem);
493 data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
494 gst_memory_resize (mem, -1, 100);
495 gst_memory_unmap (mem);
497 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
498 fail_unless (size == 100);
499 fail_unless (offset == 0);
500 fail_unless (maxalloc >= 100);
502 data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
503 gst_memory_unmap (mem);
504 gst_memory_unref (mem);
511 gst_memory_suite (void)
513 Suite *s = suite_create ("GstMemory");
514 TCase *tc_chain = tcase_create ("general");
516 suite_add_tcase (s, tc_chain);
517 tcase_add_test (tc_chain, test_submemory);
518 tcase_add_test (tc_chain, test_submemory_writable);
519 tcase_add_test (tc_chain, test_writable);
520 tcase_add_test (tc_chain, test_is_span);
521 tcase_add_test (tc_chain, test_copy);
522 tcase_add_test (tc_chain, test_try_new_and_alloc);
523 tcase_add_test (tc_chain, test_resize);
524 tcase_add_test (tc_chain, test_map);
525 tcase_add_test (tc_chain, test_map_nested);
526 tcase_add_test (tc_chain, test_map_resize);
531 GST_CHECK_MAIN (gst_memory);