-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathobject_pool_tests.cpp
More file actions
106 lines (86 loc) · 2.46 KB
/
Copy pathobject_pool_tests.cpp
File metadata and controls
106 lines (86 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "core/object_pool.h"
#include <gtest/gtest.h>
#include "core/thread.h"
#include "core/vector3.h"
TEST(object_pool, single)
{
iris::ObjectPool<iris::Vector3> pool{};
const auto *v = pool.next(1.0f, 2.0f, 3.0f);
ASSERT_EQ(*v, iris::Vector3(1.0f, 2.0f, 3.0f));
}
TEST(object_pool, multiple)
{
iris::ObjectPool<iris::Vector3> pool{};
const auto *v1 = pool.next(1.0f, 2.0f, 3.0f);
const auto *v2 = pool.next(1.1f, 2.1f, 3.1f);
const auto *v3 = pool.next(1.2f, 2.2f, 3.2f);
ASSERT_NE(v1, v2);
ASSERT_NE(v1, v3);
ASSERT_NE(v2, v3);
ASSERT_EQ(*v1, iris::Vector3(1.0f, 2.0f, 3.0f));
ASSERT_EQ(*v2, iris::Vector3(1.1f, 2.1f, 3.1f));
ASSERT_EQ(*v3, iris::Vector3(1.2f, 2.2f, 3.2f));
}
TEST(object_pool, multiple_of_order)
{
iris::ObjectPool<iris::Vector3> pool{};
const auto *v1 = pool.next(1.0f, 2.0f, 3.0f);
const auto *v2 = pool.next(1.1f, 2.1f, 3.1f);
pool.release(v1);
const auto *v3 = pool.next(1.2f, 2.2f, 3.2f);
ASSERT_NE(v2, v3);
ASSERT_EQ(*v2, iris::Vector3(1.1f, 2.1f, 3.1f));
ASSERT_EQ(*v3, iris::Vector3(1.2f, 2.2f, 3.2f));
}
TEST(object_pool, drained_pool)
{
iris::ObjectPool<int, 3> pool{};
pool.next();
pool.next();
pool.next();
ASSERT_THROW(pool.next(), iris::Exception);
}
TEST(object_pool, release)
{
iris::ObjectPool<int> pool{};
auto i1 = pool.next();
pool.release(i1);
auto i2 = pool.next();
ASSERT_EQ(i1, i2);
}
TEST(object_pool, thread_safe_next)
{
iris::ObjectPool<iris::Vector3> pool{};
std::vector<iris::Vector3> vec1;
std::vector<iris::Vector3> vec2;
std::atomic<bool> start = false;
iris::Thread t1 = [&start, &vec1, &pool]()
{
while (!start)
{
}
for (auto i = 0u; i < 500u; ++i)
{
vec1.emplace_back(*pool.next(1.0f, 1.0f, 1.0f));
}
};
iris::Thread t2 = [&start, &vec2, &pool]()
{
while (!start)
{
}
for (auto i = 0u; i < 500u; ++i)
{
vec2.emplace_back(*pool.next(2.0f, 2.0f, 2.0f));
}
};
start = true;
t1.join();
t2.join();
ASSERT_EQ(vec1.size(), 500u);
ASSERT_EQ(vec2.size(), 500u);
ASSERT_TRUE(std::all_of(
std::cbegin(vec1), std::cend(vec1), [&vec1](const auto &element) { return vec1.front() == element; }));
ASSERT_TRUE(std::all_of(
std::cbegin(vec2), std::cend(vec2), [&vec2](const auto &element) { return vec2.front() == element; }));
}