Formatting automated-tests
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-FixedSizeMemoryPool.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/fixed-size-memory-pool.h>
24
25 using namespace Dali;
26
27 void utc_dali_internal_fixedsizememorypool_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_internal_fixedsizememorypool_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 namespace
38 {
39 unsigned int gTestObjectConstructed = 0;
40 unsigned int gTestObjectDestructed  = 0;
41 unsigned int gTestObjectMethod      = 0;
42 unsigned int gTestObjectDataAccess  = 0;
43
44 } // namespace
45
46 class TestObject
47 {
48 public:
49   TestObject()
50   : mData1(0),
51     mData2(false)
52   {
53     gTestObjectConstructed++;
54   }
55
56   ~TestObject()
57   {
58     gTestObjectDestructed++;
59   }
60
61   void Method()
62   {
63     gTestObjectMethod++;
64   }
65
66   void DataAccess()
67   {
68     mData1++;
69     mData2 = true;
70
71     gTestObjectDataAccess++;
72   }
73
74 private:
75   unsigned int mData1;
76   bool         mData2;
77 };
78
79 int UtcDaliFixedSizeMemoryPoolCreate(void)
80 {
81   gTestObjectConstructed = 0;
82   gTestObjectDestructed  = 0;
83   gTestObjectMethod      = 0;
84   gTestObjectDataAccess  = 0;
85
86   Internal::FixedSizeMemoryPool memoryPool(Internal::TypeSizeWithAlignment<TestObject>::size);
87
88   TestObject* testObject1 = new(memoryPool.Allocate()) TestObject();
89   DALI_TEST_CHECK(testObject1);
90   DALI_TEST_EQUALS(gTestObjectConstructed, 1U, TEST_LOCATION);
91
92   testObject1->Method();
93   DALI_TEST_EQUALS(gTestObjectMethod, 1U, TEST_LOCATION);
94
95   testObject1->DataAccess();
96   DALI_TEST_EQUALS(gTestObjectDataAccess, 1U, TEST_LOCATION);
97
98   testObject1->~TestObject();
99   memoryPool.Free(testObject1);
100   DALI_TEST_EQUALS(gTestObjectDestructed, 1U, TEST_LOCATION);
101
102   END_TEST;
103 }
104
105 int UtcDaliFixedSizeMemoryPoolStressTest(void)
106 {
107   gTestObjectConstructed = 0;
108   gTestObjectDestructed  = 0;
109   gTestObjectMethod      = 0;
110   gTestObjectDataAccess  = 0;
111
112   const size_t initialCapacity = 32;
113   const size_t maximumCapacity = 1024;
114
115   const unsigned int numObjects = 1024 * 1024;
116
117   Internal::FixedSizeMemoryPool memoryPool(Internal::TypeSizeWithAlignment<TestObject>::size, initialCapacity, maximumCapacity);
118
119   Dali::Vector<TestObject*> objects;
120   objects.Reserve(numObjects);
121
122   for(unsigned int i = 0; i < numObjects; ++i)
123   {
124     TestObject* testObject = new(memoryPool.Allocate()) TestObject();
125     DALI_TEST_CHECK(testObject);
126
127     objects.PushBack(testObject);
128   }
129
130   DALI_TEST_EQUALS(gTestObjectConstructed, numObjects, TEST_LOCATION);
131
132   for(unsigned int i = 0; i < numObjects; ++i)
133   {
134     objects[i]->~TestObject();
135     memoryPool.Free(objects[i]);
136   }
137
138   DALI_TEST_EQUALS(gTestObjectDestructed, numObjects, TEST_LOCATION);
139
140   END_TEST;
141 }