[12177] | 1 | #include <gtest/gtest.h> |
---|
| 2 | #include "util/SmallObjectAllocator.h" |
---|
| 3 | |
---|
| 4 | namespace orxonox |
---|
| 5 | { |
---|
| 6 | ////////////////////////////////////////// |
---|
| 7 | // Allocate objects with primitive type // |
---|
| 8 | ////////////////////////////////////////// |
---|
| 9 | |
---|
| 10 | class SmallObjectAllocatorTestInt : public testing::Test |
---|
| 11 | { |
---|
| 12 | public: |
---|
| 13 | SmallObjectAllocatorTestInt() : allocator_(sizeof(size_t), 10) |
---|
| 14 | { |
---|
| 15 | SmallObjectAllocatorTestInt::count++; |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | protected: |
---|
| 19 | size_t* create(size_t value) |
---|
| 20 | { |
---|
| 21 | void* chunk = this->allocator_.alloc(); |
---|
| 22 | size_t* pointer = new (chunk) size_t; |
---|
| 23 | *pointer = value; |
---|
| 24 | |
---|
| 25 | return pointer; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | void destroy(size_t* pointer) |
---|
| 29 | { |
---|
| 30 | this->allocator_.free(pointer); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | SmallObjectAllocator allocator_; |
---|
| 34 | static size_t count; |
---|
| 35 | }; |
---|
| 36 | |
---|
| 37 | size_t SmallObjectAllocatorTestInt::count = 0; |
---|
| 38 | |
---|
| 39 | TEST_F(SmallObjectAllocatorTestInt, Default) |
---|
| 40 | { |
---|
| 41 | // Test if the fixture was created |
---|
| 42 | EXPECT_EQ(1u, SmallObjectAllocatorTestInt::count); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | TEST_F(SmallObjectAllocatorTestInt, Create) |
---|
| 46 | { |
---|
| 47 | // create an integer |
---|
| 48 | size_t* pointer = this->create(5); |
---|
| 49 | ASSERT_NE((void*)nullptr, pointer); |
---|
| 50 | EXPECT_EQ(5u, *pointer); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | TEST_F(SmallObjectAllocatorTestInt, CreateAndDestroy) |
---|
| 54 | { |
---|
| 55 | // create an integer |
---|
| 56 | size_t* pointer = this->create(5); |
---|
| 57 | ASSERT_NE((void*)nullptr, pointer); |
---|
| 58 | EXPECT_EQ(5u, *pointer); |
---|
| 59 | |
---|
| 60 | // destroy it again |
---|
| 61 | this->destroy(pointer); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | TEST_F(SmallObjectAllocatorTestInt, Create100) |
---|
| 65 | { |
---|
| 66 | std::vector<size_t*> pointers; |
---|
| 67 | |
---|
| 68 | // create 100 integers. this is greater than the amount of chunks per block, hence the allocator has to allocate multiple blocks |
---|
| 69 | static const size_t LIMIT = 100; |
---|
| 70 | for (size_t i = 0; i < LIMIT; ++i) |
---|
| 71 | { |
---|
| 72 | size_t* pointer = this->create(i); |
---|
| 73 | EXPECT_EQ(i, *pointer); |
---|
| 74 | pointers.push_back(pointer); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | EXPECT_EQ(LIMIT, pointers.size()); |
---|
| 78 | |
---|
| 79 | // check if all integers are still valid |
---|
| 80 | for (size_t i = 0; i < LIMIT; ++i) |
---|
| 81 | { |
---|
| 82 | EXPECT_EQ(i, *pointers[i]); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | TEST_F(SmallObjectAllocatorTestInt, Create100Destroy50) |
---|
| 87 | { |
---|
| 88 | std::vector<size_t*> pointers; |
---|
| 89 | |
---|
| 90 | // create 100 integers |
---|
| 91 | static const size_t LIMIT = 100; |
---|
| 92 | for (size_t i = 0; i < LIMIT; ++i) |
---|
| 93 | { |
---|
| 94 | size_t* pointer = this->create(i); |
---|
| 95 | EXPECT_EQ(i, *pointer); |
---|
| 96 | pointers.push_back(pointer); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | EXPECT_EQ(LIMIT, pointers.size()); |
---|
| 100 | |
---|
| 101 | // destroy the first 50 |
---|
| 102 | for (size_t i = 0; i < LIMIT / 2; ++i) |
---|
| 103 | { |
---|
| 104 | this->destroy(pointers[i]); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // check if the remaining integers are still valid |
---|
| 108 | for (size_t i = LIMIT / 2; i < LIMIT; ++i) |
---|
| 109 | { |
---|
| 110 | EXPECT_EQ(i, *pointers[i]); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | TEST_F(SmallObjectAllocatorTestInt, Create100Destroy50Create100) |
---|
| 115 | { |
---|
| 116 | std::vector<size_t*> pointers; |
---|
| 117 | |
---|
| 118 | // create 100 integers |
---|
| 119 | static const size_t LIMIT = 100; |
---|
| 120 | for (size_t i = 0; i < LIMIT; ++i) |
---|
| 121 | { |
---|
| 122 | size_t* pointer = this->create(i); |
---|
| 123 | EXPECT_EQ(i, *pointer); |
---|
| 124 | pointers.push_back(pointer); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | EXPECT_EQ(LIMIT, pointers.size()); |
---|
| 128 | |
---|
| 129 | // destroy the first 50 |
---|
| 130 | for (size_t i = 0; i < LIMIT / 2; ++i) |
---|
| 131 | { |
---|
| 132 | this->destroy(pointers[i]); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | // create 100 more integers |
---|
| 136 | for (size_t i = LIMIT; i < LIMIT * 2; ++i) |
---|
| 137 | { |
---|
| 138 | size_t* pointer = this->create(i); |
---|
| 139 | EXPECT_EQ(i, *pointer); |
---|
| 140 | pointers.push_back(pointer); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | EXPECT_EQ(LIMIT * 2, pointers.size()); |
---|
| 144 | |
---|
| 145 | // check if the remaining and the new integers are valid |
---|
| 146 | for (size_t i = LIMIT / 2; i < LIMIT * 2; ++i) |
---|
| 147 | { |
---|
| 148 | EXPECT_EQ(i, *pointers[i]); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | ///////////////////////////////////////////////////// |
---|
| 154 | // Allocate objects which are instances of a class // |
---|
| 155 | ///////////////////////////////////////////////////// |
---|
| 156 | |
---|
| 157 | class SmallObjectAllocatorTestObject : public testing::Test |
---|
| 158 | { |
---|
| 159 | protected: |
---|
| 160 | class SmallObject |
---|
| 161 | { |
---|
| 162 | public: |
---|
| 163 | static size_t total_s; |
---|
| 164 | |
---|
| 165 | SmallObject(size_t value) |
---|
| 166 | { |
---|
| 167 | this->value_ = value; |
---|
| 168 | SmallObject::total_s += this->value_; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | ~SmallObject() |
---|
| 172 | { |
---|
| 173 | SmallObject::total_s -= this->value_; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | size_t getValue() const |
---|
| 177 | { |
---|
| 178 | return this->value_; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | private: |
---|
| 182 | size_t value_; |
---|
| 183 | }; |
---|
| 184 | |
---|
| 185 | public: |
---|
| 186 | SmallObjectAllocatorTestObject() : allocator_(sizeof(SmallObject), 10) |
---|
| 187 | { |
---|
| 188 | SmallObjectAllocatorTestObject::count++; |
---|
| 189 | SmallObject::total_s = 0; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | protected: |
---|
| 193 | SmallObject* create(size_t value) |
---|
| 194 | { |
---|
| 195 | // allocate memory |
---|
| 196 | void* chunk = this->allocator_.alloc(); |
---|
| 197 | // execute placement new operator |
---|
| 198 | SmallObject* pointer = new (chunk) SmallObject(value); |
---|
| 199 | |
---|
| 200 | return pointer; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void destroy(SmallObject* pointer) |
---|
| 204 | { |
---|
| 205 | // call destructor |
---|
| 206 | pointer->~SmallObject(); |
---|
| 207 | // free memory |
---|
| 208 | this->allocator_.free(pointer); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | SmallObjectAllocator allocator_; |
---|
| 212 | static size_t count; |
---|
| 213 | }; |
---|
| 214 | |
---|
| 215 | size_t SmallObjectAllocatorTestObject::count = 0; |
---|
| 216 | size_t SmallObjectAllocatorTestObject::SmallObject::total_s = 0; |
---|
| 217 | |
---|
| 218 | TEST_F(SmallObjectAllocatorTestObject, Default) |
---|
| 219 | { |
---|
| 220 | // Test if the fixture was created |
---|
| 221 | EXPECT_EQ(1u, SmallObjectAllocatorTestObject::count); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | TEST_F(SmallObjectAllocatorTestObject, Create) |
---|
| 225 | { |
---|
| 226 | EXPECT_EQ(0u, SmallObject::total_s); |
---|
| 227 | |
---|
| 228 | // create an object |
---|
| 229 | SmallObject* pointer = this->create(5); |
---|
| 230 | ASSERT_NE((void*)nullptr, pointer); |
---|
| 231 | EXPECT_EQ(5u, pointer->getValue()); |
---|
| 232 | EXPECT_EQ(5u, SmallObject::total_s); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | TEST_F(SmallObjectAllocatorTestObject, CreateAndDestroy) |
---|
| 236 | { |
---|
| 237 | EXPECT_EQ(0u, SmallObject::total_s); |
---|
| 238 | |
---|
| 239 | // create an object |
---|
| 240 | SmallObject* pointer = this->create(5); |
---|
| 241 | ASSERT_NE((void*)nullptr, pointer); |
---|
| 242 | EXPECT_EQ(5u, pointer->getValue()); |
---|
| 243 | EXPECT_EQ(5u, SmallObject::total_s); |
---|
| 244 | |
---|
| 245 | // destroy it again |
---|
| 246 | this->destroy(pointer); |
---|
| 247 | EXPECT_EQ(0u, SmallObject::total_s); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | TEST_F(SmallObjectAllocatorTestObject, CreateAndDestroy100) |
---|
| 251 | { |
---|
| 252 | std::vector<SmallObject*> pointers; |
---|
| 253 | |
---|
| 254 | // create 100 objects. this is greater than the amount of chunks per block, hence the allocator has to allocate multiple blocks |
---|
| 255 | static const size_t LIMIT = 100; |
---|
| 256 | size_t total = 0; |
---|
| 257 | for (size_t i = 0; i < LIMIT; ++i) |
---|
| 258 | { |
---|
| 259 | SmallObject* pointer = this->create(i); |
---|
| 260 | EXPECT_EQ(i, pointer->getValue()); |
---|
| 261 | pointers.push_back(pointer); |
---|
| 262 | total += i; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | EXPECT_EQ(LIMIT, pointers.size()); |
---|
| 266 | EXPECT_EQ(total, SmallObject::total_s); |
---|
| 267 | |
---|
| 268 | // check if all objects are still valid |
---|
| 269 | for (size_t i = 0; i < LIMIT; ++i) |
---|
| 270 | { |
---|
| 271 | EXPECT_EQ(i, pointers[i]->getValue()); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | // destroy all objects |
---|
| 275 | for (size_t i = 0; i < LIMIT; ++i) |
---|
| 276 | { |
---|
| 277 | this->destroy(pointers[i]); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | EXPECT_EQ(0u, SmallObject::total_s); |
---|
| 281 | } |
---|
| 282 | } |
---|