Remove RenderSurface from Core
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-MemoryPoolObjectAllocator.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20
21 // Internal headers are allowed here
22
23 #include <dali/internal/common/memory-pool-object-allocator.h>
24
25 using namespace Dali;
26
27 void utc_dali_internal_memorypoolobjectallocator_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_internal_memorypoolobjectallocator_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 struct MemoryPoolObjectAllocatorTestObjectTracking
38 {
39   int testObjectDestructed;
40   int testObjectMethod;
41   int testObjectDataAccess;
42
43   MemoryPoolObjectAllocatorTestObjectTracking()
44   : testObjectDestructed( 0 ),
45     testObjectMethod( 0 ),
46     testObjectDataAccess( 0 )
47   {
48   }
49 };
50
51 class MemoryPoolObjectAllocatorTestObject
52 {
53 public:
54
55   MemoryPoolObjectAllocatorTestObject()
56   : mData1( 0 ),
57     mData2( false ),
58     mTracking( NULL )
59   {
60   }
61
62   ~MemoryPoolObjectAllocatorTestObject()
63   {
64     if( mTracking )
65     {
66       mTracking->testObjectDestructed++;
67     }
68   }
69
70   void Method()
71   {
72     if( mTracking )
73     {
74       mTracking->testObjectMethod++;
75     }
76   }
77
78   void DataAccess()
79   {
80     mData1++;
81     mData2 = true;
82
83     if( mTracking )
84     {
85       mTracking->testObjectDataAccess++;
86     }
87   }
88
89   void SetTracking( MemoryPoolObjectAllocatorTestObjectTracking* tracking )
90   {
91     mTracking = tracking;
92   }
93
94 private:
95
96   int mData1;
97   bool mData2;
98
99   MemoryPoolObjectAllocatorTestObjectTracking* mTracking;
100 };
101
102 int UtcDaliMemoryPoolObjectAllocatorObjectAllocation(void)
103 {
104
105   Internal::MemoryPoolObjectAllocator< MemoryPoolObjectAllocatorTestObject > allocator;
106
107   // Allocate an object
108   MemoryPoolObjectAllocatorTestObject* testObject1 = allocator.Allocate();
109   DALI_TEST_CHECK( testObject1 );
110
111   MemoryPoolObjectAllocatorTestObjectTracking tracking1;
112   testObject1->SetTracking( &tracking1 );
113
114   testObject1->Method();
115   DALI_TEST_EQUALS( tracking1.testObjectMethod, 1, TEST_LOCATION );
116
117   testObject1->DataAccess();
118   DALI_TEST_EQUALS( tracking1.testObjectDataAccess, 1, TEST_LOCATION );
119
120   allocator.Destroy( testObject1 );
121   DALI_TEST_EQUALS( tracking1.testObjectDestructed, 1, TEST_LOCATION );
122
123   // Reset and allocate another object
124   allocator.ResetMemoryPool();
125
126   MemoryPoolObjectAllocatorTestObject* testObject2 = allocator.Allocate();
127   DALI_TEST_CHECK( testObject2 );
128
129   MemoryPoolObjectAllocatorTestObjectTracking tracking2;
130   testObject2->SetTracking( &tracking2 );
131
132   testObject2->Method();
133   DALI_TEST_EQUALS( tracking2.testObjectMethod, 1, TEST_LOCATION );
134
135   testObject2->DataAccess();
136   DALI_TEST_EQUALS( tracking2.testObjectDataAccess, 1, TEST_LOCATION );
137
138   allocator.Destroy( testObject2 );
139   DALI_TEST_EQUALS( tracking2.testObjectDestructed, 1, TEST_LOCATION );
140
141   END_TEST;
142 }
143
144 int UtcDaliMemoryPoolObjectAllocatorObjectRawAllocation(void)
145 {
146   Internal::MemoryPoolObjectAllocator< MemoryPoolObjectAllocatorTestObject > allocator;
147
148   MemoryPoolObjectAllocatorTestObject* testObject = new ( allocator.AllocateRaw() ) MemoryPoolObjectAllocatorTestObject();
149   DALI_TEST_CHECK( testObject );
150
151   MemoryPoolObjectAllocatorTestObjectTracking tracking;
152   testObject->SetTracking( &tracking );
153
154   testObject->Method();
155   DALI_TEST_EQUALS( tracking.testObjectMethod, 1, TEST_LOCATION );
156
157   testObject->DataAccess();
158   DALI_TEST_EQUALS( tracking.testObjectDataAccess, 1, TEST_LOCATION );
159
160   allocator.Destroy( testObject );
161   DALI_TEST_EQUALS( tracking.testObjectDestructed, 1, TEST_LOCATION );
162
163   END_TEST;
164 }
165
166 int UtcDaliMemoryPoolObjectAllocatorObjectAllocationPOD(void)
167 {
168   Internal::MemoryPoolObjectAllocator< bool > allocator;
169
170   bool* testObject1 = allocator.Allocate();
171   DALI_TEST_CHECK( testObject1 );
172
173   allocator.Destroy( testObject1 );
174
175   allocator.ResetMemoryPool();
176
177   bool* testObject2 = allocator.Allocate();
178   DALI_TEST_CHECK( testObject2 );
179
180   allocator.Destroy( testObject2 );
181
182   END_TEST;
183 }