Debian Codegraph @

tests/test-allocator.cc

raw symbols SCIP
1 /*
2 * test-allocator.cc
3 *
4 * Created on: Aug 4, 2014
5 * Author: loganek
6 */
7
8 #include "mmtest.h"
9 #include <gstreamermm.h>
10
11 using namespace Gst;
12
13 TEST(AllocatorTest, ShouldFindSystemAllocator)
14 {
15 Glib::RefPtr<Allocator> allocator = Allocator::find(GST_ALLOCATOR_SYSMEM);
16
17 MM_ASSERT_TRUE(allocator);
18 }
19
20 TEST(AllocatorTest, DefaultAllocatorTheSameAsSystemAllocator)
21 {
22 Glib::RefPtr<Allocator> allocator1 = Allocator::find(GST_ALLOCATOR_SYSMEM),
23 allocator2 = Allocator::get_default_allocator();
24
25 MM_ASSERT_TRUE(allocator1);
26 ASSERT_EQ(allocator1, allocator2);
27 }
28
29 TEST(AllocatorTest, ShouldCorrectAllocateMemory)
30 {
31 AllocationParams params;
32 params.set_align(7);
33 MemoryFlags flags = MEMORY_FLAG_NO_SHARE | MEMORY_FLAG_LAST;
34 params.set_flags(flags);
35 Glib::RefPtr<Allocator> allocator = Allocator::get_default_allocator();
36 Glib::RefPtr<Memory> mem = allocator->alloc(10, params);
37
38 MM_ASSERT_TRUE(mem);
39 EXPECT_EQ(10ul, mem->get_size());
40 EXPECT_EQ(7ul, mem->get_align());
41 EXPECT_TRUE(flags & mem->get_flags());
42
43 allocator->free(std::move(mem));
44 }
45
46 class DerivedFromAllocator : public Gst::Allocator
47 {
48 Glib::RefPtr<Gst::Allocator> the_allocator;
49
50 public:
51 explicit DerivedFromAllocator(GstAllocator *gobj)
52 : Glib::ObjectBase(typeid (DerivedFromAllocator)),
53 Gst::Allocator(gobj),
54 the_allocator(Allocator::get_default_allocator())
55 {
56 }
57
58 Glib::RefPtr<Gst::Memory> alloc_vfunc(gsize size, const AllocationParams& params) override
59 {
60 Glib::RefPtr<Gst::Memory> r = the_allocator->alloc(size, params);
61 r->gobj()->allocator = gobj(); // pretend that it was us who allocated this memory
62 return r;
63 }
64
65 void free_vfunc(Glib::RefPtr<Gst::Memory>&& memory) override
66 {
67 memory->gobj()->allocator = the_allocator->gobj(); // pretend that it was the_allocator who allocated this memory
68 the_allocator->free(std::move(memory));
69 }
70
71 static Glib::RefPtr<Allocator> create()
72 {
73 return Glib::RefPtr<Allocator>(new DerivedFromAllocator((GstAllocator*)g_object_new(Allocator::get_type(), NULL)));
74 }
75 };
76
77 TEST(AllocatorTest, DerivedFromAllocatorShouldReturnProperlyRefcountedWrappedGstMemory)
78 {
79 Glib::RefPtr<Allocator> allocator = DerivedFromAllocator::create();
80 MM_ASSERT_TRUE(allocator);
81 MM_ASSERT_TRUE(allocator->gobj());
82 MM_ASSERT_TRUE(GST_IS_ALLOCATOR(allocator->gobj()));
83
84 AllocationParams params;
85 params.set_align(7);
86 MemoryFlags flags = MEMORY_FLAG_NO_SHARE | MEMORY_FLAG_LAST;
87 params.set_flags(flags);
88
89 Glib::RefPtr<Memory> mem = allocator->alloc(10, params);
90 MM_ASSERT_TRUE(mem);
91 MM_ASSERT_TRUE(mem->gobj());
92 MM_ASSERT_TRUE(GST_IS_MINI_OBJECT_TYPE(mem->gobj(), GST_TYPE_MEMORY));
93 ASSERT_EQ(1, mem->get_refcount());
94 EXPECT_EQ(10ul, mem->get_size());
95 EXPECT_EQ(7ul, mem->get_align());
96 EXPECT_TRUE(flags & mem->get_flags());
97
98 allocator->free(std::move(mem));
99 }
100
101 TEST(AllocatorTest, DerivedFromAllocatorShouldReturnProperlyRefcountedGstMemory)
102 {
103 Glib::RefPtr<Allocator> allocator = DerivedFromAllocator::create();
104 MM_ASSERT_TRUE(allocator);
105 MM_ASSERT_TRUE(allocator->gobj());
106 MM_ASSERT_TRUE(GST_IS_ALLOCATOR(allocator->gobj()));
107
108 AllocationParams params;
109 params.set_align(7);
110 MemoryFlags flags = MEMORY_FLAG_NO_SHARE | MEMORY_FLAG_LAST;
111 params.set_flags(flags);
112
113 GstMemory *mem = gst_allocator_alloc(allocator->gobj(), 10, params.gobj());
114 MM_ASSERT_TRUE(mem);
115 MM_ASSERT_TRUE(GST_IS_MINI_OBJECT_TYPE(mem, GST_TYPE_MEMORY));
116 ASSERT_EQ(1, mem->mini_object.refcount);
117 EXPECT_EQ(10ul, mem->size);
118 EXPECT_EQ(7ul, mem->align);
119 EXPECT_TRUE(flags & mem->mini_object.flags);
120 gst_allocator_free(allocator->gobj(), mem);
121 }