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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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 GstMapInfo info, sinfo;
40 memory = gst_allocator_alloc (NULL, 4, NULL);
42 /* check sizes, memory starts out empty */
43 fail_unless (gst_memory_map (memory, &info, GST_MAP_WRITE));
44 fail_unless (info.size == 4, "memory has wrong size");
45 fail_unless (info.maxsize >= 4, "memory has wrong size");
46 memset (info.data, 0, 4);
47 gst_memory_unmap (memory, &info);
49 fail_unless (gst_memory_map (memory, &info, GST_MAP_READ));
51 sub = gst_memory_share (memory, 1, 2);
52 fail_if (sub == NULL, "share of memory returned NULL");
54 fail_unless (gst_memory_map (sub, &sinfo, GST_MAP_READ));
55 fail_unless (sinfo.size == 2, "submemory has wrong size");
56 fail_unless (memcmp (info.data + 1, sinfo.data, 2) == 0,
57 "submemory contains the wrong data");
58 ASSERT_MINI_OBJECT_REFCOUNT (sub, "submemory", 1);
59 gst_memory_unmap (sub, &sinfo);
60 gst_memory_unref (sub);
62 /* create a submemory of size 0 */
63 sub = gst_memory_share (memory, 1, 0);
64 fail_if (sub == NULL, "share memory returned NULL");
65 fail_unless (gst_memory_map (sub, &sinfo, GST_MAP_READ));
66 fail_unless (sinfo.size == 0, "submemory has wrong size");
67 fail_unless (memcmp (info.data + 1, sinfo.data, 0) == 0,
68 "submemory contains the wrong data");
69 ASSERT_MINI_OBJECT_REFCOUNT (sub, "submemory", 1);
70 gst_memory_unmap (sub, &sinfo);
71 gst_memory_unref (sub);
73 /* test if metadata is coppied, not a complete memory copy so only the
74 * timestamp and offset fields are copied. */
75 sub = gst_memory_share (memory, 0, 1);
76 fail_if (sub == NULL, "share of memory returned NULL");
77 fail_unless (gst_memory_get_sizes (sub, NULL, NULL) == 1,
78 "submemory has wrong size");
79 gst_memory_unref (sub);
81 /* test if metadata is coppied, a complete memory is copied so all the timing
82 * fields should be copied. */
83 sub = gst_memory_share (memory, 0, 4);
84 fail_if (sub == NULL, "share of memory returned NULL");
85 fail_unless (gst_memory_get_sizes (sub, NULL, NULL) == 4,
86 "submemory has wrong size");
89 gst_memory_unref (sub);
91 gst_memory_unmap (memory, &info);
93 /* test write map + share failure */
94 fail_unless (gst_memory_map (memory, &info, GST_MAP_WRITE));
95 sub = gst_memory_share (memory, 0, 4);
96 fail_unless (sub == NULL, "share with a write map succeeded");
98 gst_memory_unmap (memory, &info);
99 gst_memory_unref (memory);
104 GST_START_TEST (test_is_span)
106 GstMemory *memory, *sub1, *sub2;
108 memory = gst_allocator_alloc (NULL, 4, NULL);
110 sub1 = gst_memory_share (memory, 0, 2);
111 fail_if (sub1 == NULL, "share of memory returned NULL");
113 sub2 = gst_memory_share (memory, 2, 2);
114 fail_if (sub2 == NULL, "share of memory returned NULL");
116 fail_if (gst_memory_is_span (memory, sub2, NULL) == TRUE,
117 "a parent memory can't be span");
119 fail_if (gst_memory_is_span (sub1, memory, NULL) == TRUE,
120 "a parent memory can't be span");
122 fail_if (gst_memory_is_span (sub1, sub2, NULL) == FALSE,
123 "two submemorys next to each other should be span");
126 gst_memory_unref (sub1);
127 gst_memory_unref (sub2);
128 gst_memory_unref (memory);
133 static const char ro_memory[] = "abcdefghijklmnopqrstuvwxyz";
136 create_read_only_memory (void)
140 /* assign some read-only data to the new memory */
141 mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
142 (gpointer) ro_memory, sizeof (ro_memory), 0, sizeof (ro_memory), NULL,
144 fail_unless (GST_MEMORY_IS_READONLY (mem));
149 GST_START_TEST (test_writable)
151 GstMemory *mem, *mem2;
154 /* create read-only memory and try to write */
155 mem = create_read_only_memory ();
157 fail_if (gst_memory_map (mem, &info, GST_MAP_WRITE));
159 /* Make sure mapping anxd unmapping it doesn't change it's locking state */
160 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
161 gst_memory_unmap (mem, &info);
163 fail_if (gst_memory_map (mem, &info, GST_MAP_WRITE));
165 mem2 = gst_memory_copy (mem, 0, -1);
166 fail_unless (GST_MEMORY_IS_READONLY (mem));
167 fail_if (GST_MEMORY_IS_READONLY (mem2));
169 fail_unless (gst_memory_map (mem2, &info, GST_MAP_WRITE));
171 gst_memory_unmap (mem2, &info);
173 gst_memory_ref (mem2);
174 fail_if (gst_memory_map (mem, &info, GST_MAP_WRITE));
175 gst_memory_unref (mem2);
177 fail_unless (gst_memory_map (mem2, &info, GST_MAP_WRITE));
179 gst_memory_unmap (mem2, &info);
180 gst_memory_unref (mem2);
182 gst_memory_unref (mem);
187 GST_START_TEST (test_submemory_writable)
189 GstMemory *mem, *sub_mem;
192 /* create sub-memory of read-only memory and try to write */
193 mem = create_read_only_memory ();
195 sub_mem = gst_memory_share (mem, 0, 8);
196 fail_unless (GST_MEMORY_IS_READONLY (sub_mem));
198 fail_if (gst_memory_map (mem, &info, GST_MAP_WRITE));
199 fail_if (gst_memory_map (sub_mem, &info, GST_MAP_WRITE));
201 gst_memory_unref (sub_mem);
202 gst_memory_unref (mem);
207 GST_START_TEST (test_copy)
209 GstMemory *memory, *copy;
210 GstMapInfo info, sinfo;
212 memory = gst_allocator_alloc (NULL, 4, NULL);
213 ASSERT_MINI_OBJECT_REFCOUNT (memory, "memory", 1);
215 copy = gst_memory_copy (memory, 0, -1);
216 ASSERT_MINI_OBJECT_REFCOUNT (memory, "memory", 1);
217 ASSERT_MINI_OBJECT_REFCOUNT (copy, "copy", 1);
218 /* memorys are copied and must point to different memory */
219 fail_if (memory == copy);
221 fail_unless (gst_memory_map (memory, &info, GST_MAP_READ));
222 fail_unless (gst_memory_map (copy, &sinfo, GST_MAP_READ));
224 /* NOTE that data is refcounted */
225 fail_unless (info.size == sinfo.size);
227 gst_memory_unmap (copy, &sinfo);
228 gst_memory_unmap (memory, &info);
230 gst_memory_unref (copy);
231 gst_memory_unref (memory);
233 memory = gst_allocator_alloc (NULL, 0, NULL);
234 fail_unless (gst_memory_map (memory, &info, GST_MAP_READ));
235 fail_unless (info.size == 0);
236 gst_memory_unmap (memory, &info);
238 /* copying a 0-sized memory should not crash */
239 copy = gst_memory_copy (memory, 0, -1);
240 fail_unless (gst_memory_map (copy, &info, GST_MAP_READ));
241 fail_unless (info.size == 0);
242 gst_memory_unmap (copy, &info);
244 gst_memory_unref (copy);
245 gst_memory_unref (memory);
250 GST_START_TEST (test_try_new_and_alloc)
256 mem = gst_allocator_alloc (NULL, 0, NULL);
257 fail_unless (mem != NULL);
258 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
259 fail_unless (info.size == 0);
260 gst_memory_unmap (mem, &info);
261 gst_memory_unref (mem);
263 /* normal alloc should still work */
264 size = 640 * 480 * 4;
265 mem = gst_allocator_alloc (NULL, size, NULL);
266 fail_unless (mem != NULL);
267 fail_unless (gst_memory_map (mem, &info, GST_MAP_WRITE));
268 fail_unless (info.data != NULL);
269 fail_unless (info.size == (640 * 480 * 4));
270 info.data[640 * 479 * 4 + 479] = 0xff;
271 gst_memory_unmap (mem, &info);
273 gst_memory_unref (mem);
276 /* Disabled this part of the test, because it happily succeeds on 64-bit
277 * machines that have enough memory+swap, because the address space is large
278 * enough. There's not really any way to test the failure case except by
279 * allocating chunks of memory until it fails, which would suck. */
281 /* now this better fail (don't run in valgrind, it will abort
282 * or warn when passing silly arguments to malloc) */
283 if (!RUNNING_ON_VALGRIND) {
284 mem = gst_allocator_alloc (NULL, (guint) - 1, 0);
285 fail_unless (mem == NULL);
292 GST_START_TEST (test_resize)
296 gsize size, maxsize, offset;
298 /* one memory block */
299 mem = gst_allocator_alloc (NULL, 100, NULL);
301 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
302 fail_unless (size == 100);
303 fail_unless (offset == 0);
304 fail_unless (maxalloc >= 100);
306 ASSERT_CRITICAL (gst_memory_resize (mem, 200, 50));
307 ASSERT_CRITICAL (gst_memory_resize (mem, 0, 150));
308 ASSERT_CRITICAL (gst_memory_resize (mem, 1, maxalloc));
309 ASSERT_CRITICAL (gst_memory_resize (mem, maxalloc, 1));
311 /* this does nothing */
312 gst_memory_resize (mem, 0, 100);
314 /* nothing should have changed */
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, 0, 50);
321 size = gst_memory_get_sizes (mem, &offset, &maxsize);
322 fail_unless (size == 50);
323 fail_unless (offset == 0);
324 fail_unless (maxsize == maxalloc);
326 gst_memory_resize (mem, 0, 100);
327 size = gst_memory_get_sizes (mem, &offset, &maxsize);
328 fail_unless (size == 100);
329 fail_unless (offset == 0);
330 fail_unless (maxsize == maxalloc);
332 gst_memory_resize (mem, 1, 99);
333 size = gst_memory_get_sizes (mem, &offset, &maxsize);
334 fail_unless (size == 99);
335 fail_unless (offset == 1);
336 fail_unless (maxsize == maxalloc);
338 ASSERT_CRITICAL (gst_memory_resize (mem, 1, maxalloc - 1));
340 gst_memory_resize (mem, 0, 99);
341 size = gst_memory_get_sizes (mem, &offset, &maxsize);
342 fail_unless (size == 99);
343 fail_unless (offset == 1);
344 fail_unless (maxsize == maxalloc);
346 gst_memory_resize (mem, -1, 100);
347 size = gst_memory_get_sizes (mem, &offset, &maxsize);
348 fail_unless (size == 100);
349 fail_unless (offset == 0);
350 fail_unless (maxsize == maxalloc);
352 /* can't set offset below 0 */
353 ASSERT_CRITICAL (gst_memory_resize (mem, -1, 100));
355 gst_memory_resize (mem, 50, 40);
356 size = gst_memory_get_sizes (mem, &offset, &maxsize);
357 fail_unless (size == 40);
358 fail_unless (offset == 50);
359 fail_unless (maxsize == maxalloc);
361 gst_memory_resize (mem, -50, 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, 0);
368 size = gst_memory_get_sizes (mem, &offset, &maxsize);
369 fail_unless (size == 0);
370 fail_unless (offset == 0);
371 fail_unless (maxsize == maxalloc);
373 gst_memory_resize (mem, 0, 100);
374 size = gst_memory_get_sizes (mem, &offset, &maxsize);
375 fail_unless (size == 100);
376 fail_unless (offset == 0);
377 fail_unless (maxsize == maxalloc);
379 gst_memory_resize (mem, 0, 100);
380 size = gst_memory_get_sizes (mem, &offset, &maxsize);
381 fail_unless (size == 100);
382 fail_unless (offset == 0);
383 fail_unless (maxsize == maxalloc);
385 gst_memory_unref (mem);
390 GST_START_TEST (test_map)
397 /* one memory block */
398 mem = gst_allocator_alloc (NULL, 100, NULL);
400 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
401 fail_unless (size == 100);
402 fail_unless (offset == 0);
403 fail_unless (maxalloc >= 100);
405 /* see if simply mapping works */
406 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
407 fail_unless (info.data != NULL);
408 fail_unless (info.size == 100);
409 fail_unless (info.maxsize == maxalloc);
411 gst_memory_unmap (mem, &info);
412 gst_memory_unref (mem);
417 GST_START_TEST (test_map_nested)
420 GstMapInfo info1, info2;
422 mem = gst_allocator_alloc (NULL, 100, NULL);
425 fail_unless (gst_memory_map (mem, &info1, GST_MAP_READ));
426 fail_unless (info1.data != NULL);
427 fail_unless (info1.size == 100);
429 fail_unless (gst_memory_map (mem, &info2, GST_MAP_READ));
430 fail_unless (info2.data == info1.data);
431 fail_unless (info2.size == 100);
434 gst_memory_unmap (mem, &info2);
435 gst_memory_unmap (mem, &info1);
437 fail_unless (gst_memory_map (mem, &info1, GST_MAP_READ));
439 fail_if (gst_memory_map (mem, &info2, GST_MAP_WRITE));
440 fail_if (gst_memory_map (mem, &info2, GST_MAP_READWRITE));
441 fail_unless (gst_memory_map (mem, &info2, GST_MAP_READ));
442 gst_memory_unmap (mem, &info2);
443 gst_memory_unmap (mem, &info1);
445 fail_unless (gst_memory_map (mem, &info1, GST_MAP_WRITE));
447 fail_if (gst_memory_map (mem, &info2, GST_MAP_READ));
448 fail_if (gst_memory_map (mem, &info2, GST_MAP_READWRITE));
449 fail_unless (gst_memory_map (mem, &info2, GST_MAP_WRITE));
450 gst_memory_unmap (mem, &info1);
451 gst_memory_unmap (mem, &info2);
452 /* nothing was mapped */
453 ASSERT_CRITICAL (gst_memory_unmap (mem, &info2));
455 fail_unless (gst_memory_map (mem, &info1, GST_MAP_READWRITE));
456 fail_unless (gst_memory_map (mem, &info2, GST_MAP_READ));
457 gst_memory_unmap (mem, &info2);
458 fail_unless (gst_memory_map (mem, &info2, GST_MAP_WRITE));
459 gst_memory_unmap (mem, &info2);
460 gst_memory_unmap (mem, &info1);
461 /* nothing was mapped */
462 ASSERT_CRITICAL (gst_memory_unmap (mem, &info1));
464 gst_memory_unref (mem);
469 GST_START_TEST (test_map_resize)
473 gsize size, maxalloc, offset;
475 mem = gst_allocator_alloc (NULL, 100, NULL);
478 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
479 fail_unless (info.data != NULL);
480 fail_unless (info.size == 100);
482 /* resize the buffer */
483 gst_memory_resize (mem, 1, info.size - 1);
484 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
485 fail_unless (size == 99);
486 fail_unless (offset == 1);
487 fail_unless (maxalloc >= 100);
488 gst_memory_unmap (mem, &info);
490 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
491 fail_unless (size == 99);
492 fail_unless (offset == 1);
493 fail_unless (maxalloc >= 100);
495 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
496 fail_unless (info.data != NULL);
497 fail_unless (info.size == 99);
498 fail_unless (info.maxsize >= 100);
499 gst_memory_unmap (mem, &info);
502 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
503 gst_memory_resize (mem, -1, 100);
504 gst_memory_unmap (mem, &info);
506 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
507 fail_unless (size == 100);
508 fail_unless (offset == 0);
509 fail_unless (maxalloc >= 100);
511 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
512 gst_memory_unmap (mem, &info);
513 gst_memory_unref (mem);
518 GST_START_TEST (test_alloc_params)
522 gsize size, offset, maxalloc;
523 GstAllocationParams params;
528 gst_allocation_params_init (¶ms);
531 params.flags = GST_MEMORY_FLAG_ZERO_PREFIXED | GST_MEMORY_FLAG_ZERO_PADDED;
532 mem = gst_allocator_alloc (NULL, 100, ¶ms);
534 /*Checking size and offset */
535 size = gst_memory_get_sizes (mem, &offset, &maxalloc);
536 fail_unless (size == 100);
537 fail_unless (offset == 10);
538 fail_unless (maxalloc >= 120);
540 fail_unless (GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED));
541 fail_unless (GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_ZERO_PADDED));
543 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
544 fail_unless (info.data != NULL);
545 fail_unless (info.size == 100);
548 fail_unless (memcmp (info.data - 10, arr, 10) == 0);
550 /*Checking padding */
551 fail_unless (memcmp (info.data + 100, arr, 10) == 0);
554 gst_memory_unmap (mem, &info);
555 gst_memory_unref (mem);
560 GST_START_TEST (test_lock)
564 mem = gst_allocator_alloc (NULL, 10, NULL);
565 fail_unless (mem != NULL);
567 /* test exclusivity */
568 fail_unless (gst_memory_lock (mem, GST_MAP_WRITE | GST_LOCK_FLAG_EXCLUSIVE));
569 fail_if (gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE));
570 fail_unless (gst_memory_lock (mem, GST_MAP_WRITE));
571 gst_memory_unlock (mem, GST_MAP_WRITE | GST_LOCK_FLAG_EXCLUSIVE);
572 gst_memory_unlock (mem, GST_MAP_WRITE);
576 fail_unless (gst_memory_lock (mem, GST_MAP_READ | GST_LOCK_FLAG_EXCLUSIVE));
577 fail_unless (gst_memory_lock (mem, GST_MAP_READ | GST_LOCK_FLAG_EXCLUSIVE));
578 gst_memory_unlock (mem, GST_MAP_READ | GST_LOCK_FLAG_EXCLUSIVE);
579 gst_memory_unlock (mem, GST_MAP_READ | GST_LOCK_FLAG_EXCLUSIVE);
583 fail_unless (gst_memory_lock (mem,
584 GST_MAP_READWRITE | GST_LOCK_FLAG_EXCLUSIVE));
585 fail_unless (gst_memory_lock (mem, GST_MAP_READ));
586 fail_if (gst_memory_lock (mem, GST_MAP_READ | GST_LOCK_FLAG_EXCLUSIVE));
587 fail_if (gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE));
588 fail_unless (gst_memory_lock (mem, GST_MAP_WRITE));
589 gst_memory_unlock (mem, GST_MAP_WRITE);
590 gst_memory_unlock (mem, GST_MAP_READ);
591 gst_memory_unlock (mem, GST_MAP_READWRITE | GST_LOCK_FLAG_EXCLUSIVE);
593 gst_memory_unref (mem);
599 gst_memory_suite (void)
601 Suite *s = suite_create ("GstMemory");
602 TCase *tc_chain = tcase_create ("general");
604 suite_add_tcase (s, tc_chain);
605 tcase_add_test (tc_chain, test_submemory);
606 tcase_add_test (tc_chain, test_submemory_writable);
607 tcase_add_test (tc_chain, test_writable);
608 tcase_add_test (tc_chain, test_is_span);
609 tcase_add_test (tc_chain, test_copy);
610 tcase_add_test (tc_chain, test_try_new_and_alloc);
611 tcase_add_test (tc_chain, test_resize);
612 tcase_add_test (tc_chain, test_map);
613 tcase_add_test (tc_chain, test_map_nested);
614 tcase_add_test (tc_chain, test_map_resize);
615 tcase_add_test (tc_chain, test_alloc_params);
616 tcase_add_test (tc_chain, test_lock);
621 GST_CHECK_MAIN (gst_memory);