1 #include <lib/pool.h>
2
3 #include <gtest/gtest.h>
4
5 #define DEFINE_DOUBLE_POOL_STORAGE(name, count) DEFINE_TYPED_POOL_STORAGE(double, name, count)
6 #define DOUBLE_POOL_INIT(pool, count, storage) TYPED_POOL_INIT(double, pool, count, storage)
7 #define DOUBLE_POOL_ALLOC(pool) TYPED_POOL_ALLOC(double, pool)
8 #define DOUBLE_POOL_FREE(pool, object) TYPED_POOL_FREE(double, pool, object)
9
TEST(Pool,Basic)10 TEST(Pool, Basic) {
11 pool_t pool;
12 DEFINE_DOUBLE_POOL_STORAGE(storage, 5);
13 DOUBLE_POOL_INIT(&pool, 3, storage);
14
15 // First 3 allocations should succeed.
16 double *d0 = DOUBLE_POOL_ALLOC(&pool);
17 EXPECT_NE(nullptr, d0);
18 EXPECT_EQ(0, (intptr_t) d0 % __alignof(double));
19
20 double *d1 = DOUBLE_POOL_ALLOC(&pool);
21 EXPECT_NE(nullptr, d1);
22 EXPECT_EQ(0, (intptr_t) d1 % __alignof(double));
23
24 double *d2 = DOUBLE_POOL_ALLOC(&pool);
25 EXPECT_NE(nullptr, d2);
26 EXPECT_EQ(0, (intptr_t) d2 % __alignof(double));
27
28 // All objects need to be different.
29 EXPECT_NE(d1, d0);
30 EXPECT_NE(d2, d0);
31 EXPECT_NE(d2, d1);
32
33 // Next allocation should fail.
34 double *d3 = DOUBLE_POOL_ALLOC(&pool);
35 EXPECT_EQ(nullptr, d3);
36
37 // But after we free something it should succeed.
38 DOUBLE_POOL_FREE(&pool, d0);
39 d3 = DOUBLE_POOL_ALLOC(&pool);
40 EXPECT_NE(nullptr, d3);
41 EXPECT_EQ(0, (intptr_t) d3 % __alignof(double));
42 }
43