Formatting automated-tests
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-MemoryPoolObjectAllocator.cpp
1 /*
2  * Copyright (c) 2020 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-test-suite-utils.h>
19 #include <dali/public-api/dali-core.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   MemoryPoolObjectAllocatorTestObject()
55   : mData1(0),
56     mData2(false),
57     mTracking(NULL)
58   {
59   }
60
61   ~MemoryPoolObjectAllocatorTestObject()
62   {
63     if(mTracking)
64     {
65       mTracking->testObjectDestructed++;
66     }
67   }
68
69   void Method()
70   {
71     if(mTracking)
72     {
73       mTracking->testObjectMethod++;
74     }
75   }
76
77   void DataAccess()
78   {
79     mData1++;
80     mData2 = true;
81
82     if(mTracking)
83     {
84       mTracking->testObjectDataAccess++;
85     }
86   }
87
88   void SetTracking(MemoryPoolObjectAllocatorTestObjectTracking* tracking)
89   {
90     mTracking = tracking;
91   }
92
93 private:
94   int  mData1;
95   bool mData2;
96
97   MemoryPoolObjectAllocatorTestObjectTracking* mTracking;
98 };
99
100 int UtcDaliMemoryPoolObjectAllocatorObjectAllocation(void)
101 {
102   Internal::MemoryPoolObjectAllocator<MemoryPoolObjectAllocatorTestObject> allocator;
103
104   // Allocate an object
105   MemoryPoolObjectAllocatorTestObject* testObject1 = allocator.Allocate();
106   DALI_TEST_CHECK(testObject1);
107
108   MemoryPoolObjectAllocatorTestObjectTracking tracking1;
109   testObject1->SetTracking(&tracking1);
110
111   testObject1->Method();
112   DALI_TEST_EQUALS(tracking1.testObjectMethod, 1, TEST_LOCATION);
113
114   testObject1->DataAccess();
115   DALI_TEST_EQUALS(tracking1.testObjectDataAccess, 1, TEST_LOCATION);
116
117   allocator.Destroy(testObject1);
118   DALI_TEST_EQUALS(tracking1.testObjectDestructed, 1, TEST_LOCATION);
119
120   // Reset and allocate another object
121   allocator.ResetMemoryPool();
122
123   MemoryPoolObjectAllocatorTestObject* testObject2 = allocator.Allocate();
124   DALI_TEST_CHECK(testObject2);
125
126   MemoryPoolObjectAllocatorTestObjectTracking tracking2;
127   testObject2->SetTracking(&tracking2);
128
129   testObject2->Method();
130   DALI_TEST_EQUALS(tracking2.testObjectMethod, 1, TEST_LOCATION);
131
132   testObject2->DataAccess();
133   DALI_TEST_EQUALS(tracking2.testObjectDataAccess, 1, TEST_LOCATION);
134
135   allocator.Destroy(testObject2);
136   DALI_TEST_EQUALS(tracking2.testObjectDestructed, 1, TEST_LOCATION);
137
138   END_TEST;
139 }
140
141 int UtcDaliMemoryPoolObjectAllocatorObjectRawAllocation(void)
142 {
143   Internal::MemoryPoolObjectAllocator<MemoryPoolObjectAllocatorTestObject> allocator;
144
145   MemoryPoolObjectAllocatorTestObject* testObject = new(allocator.AllocateRaw()) MemoryPoolObjectAllocatorTestObject();
146   DALI_TEST_CHECK(testObject);
147
148   MemoryPoolObjectAllocatorTestObjectTracking tracking;
149   testObject->SetTracking(&tracking);
150
151   testObject->Method();
152   DALI_TEST_EQUALS(tracking.testObjectMethod, 1, TEST_LOCATION);
153
154   testObject->DataAccess();
155   DALI_TEST_EQUALS(tracking.testObjectDataAccess, 1, TEST_LOCATION);
156
157   allocator.Destroy(testObject);
158   DALI_TEST_EQUALS(tracking.testObjectDestructed, 1, TEST_LOCATION);
159
160   END_TEST;
161 }
162
163 int UtcDaliMemoryPoolObjectAllocatorObjectAllocationPOD(void)
164 {
165   Internal::MemoryPoolObjectAllocator<bool> allocator;
166
167   bool* testObject1 = allocator.Allocate();
168   DALI_TEST_CHECK(testObject1);
169
170   allocator.Destroy(testObject1);
171
172   allocator.ResetMemoryPool();
173
174   bool* testObject2 = allocator.Allocate();
175   DALI_TEST_CHECK(testObject2);
176
177   allocator.Destroy(testObject2);
178
179   END_TEST;
180 }