16 #include <gtest/gtest.h>
26 TEST(FreePoolTest, Basic) {
45 for (
int i = 0; i < 10; ++i) {
58 EXPECT_TRUE(p1 == p2);
59 EXPECT_TRUE(p1 != p3);
68 EXPECT_TRUE(p4 != p1);
69 EXPECT_TRUE(p4 != p2);
70 EXPECT_TRUE(p4 != p3);
99 map<int, pair<uint8_t*, uint8_t*> > primed_allocations;
100 vector<int> allocation_sizes;
102 int64_t expected_pool_size = 0;
105 for (
int size = 3; size < 1024 * 1024 * 1024; size *= 3) {
108 EXPECT_TRUE(p1 != NULL);
109 EXPECT_TRUE(p2 != NULL);
110 EXPECT_TRUE(p1 != p2);
113 primed_allocations[size] = make_pair(p1, p2);
116 allocation_sizes.push_back(size);
120 for (
int iters = 1; iters <= 5; ++iters) {
121 std::random_shuffle(allocation_sizes.begin(), allocation_sizes.end());
122 for (
int i = 0; i < allocation_sizes.size(); ++i) {
123 uint8_t* p1 = pool.
Allocate(allocation_sizes[i]);
124 uint8_t* p2 = pool.
Allocate(allocation_sizes[i]);
125 EXPECT_TRUE(p1 != p2);
126 EXPECT_TRUE(p1 == primed_allocations[allocation_sizes[i]].first ||
127 p1 == primed_allocations[allocation_sizes[i]].second);
128 EXPECT_TRUE(p2 == primed_allocations[allocation_sizes[i]].first ||
129 p2 == primed_allocations[allocation_sizes[i]].second);
130 EXPECT_EQ(*p1, iters);
131 EXPECT_EQ(*p2, iters);
155 EXPECT_TRUE(ptr == ptr2);
160 EXPECT_TRUE(ptr2 != ptr3);
171 int main(
int argc,
char **argv) {
172 ::testing::InitGoogleTest(&argc, argv);
173 return RUN_ALL_TESTS();
int64_t total_allocated_bytes() const
uint8_t * Reallocate(uint8_t *ptr, int size)
int main(int argc, char **argv)
uint8_t * Allocate(int size)
Allocates a buffer of size.
This class is thread-safe.