From 6ffbb5ed61733b8dbef39b1a9d437e899e9359d7 Mon Sep 17 00:00:00 2001 From: "Enrico Loparco (eloparco)" Date: Sat, 3 Apr 2021 00:03:57 +0200 Subject: [HICN-558] Fix bug in pool_put and add tests for several pool_get / pool_put Signed-off-by: Enrico Loparco (eloparco) Change-Id: Ie0ee6d17ab87586143ecf07e648d22b3af72bbcd --- hicn-light/src/hicn/base/test/test-pool.cc | 40 +++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'hicn-light/src/hicn/base/test/test-pool.cc') diff --git a/hicn-light/src/hicn/base/test/test-pool.cc b/hicn-light/src/hicn/base/test/test-pool.cc index e5df191ea..fd0d5988b 100644 --- a/hicn-light/src/hicn/base/test/test-pool.cc +++ b/hicn-light/src/hicn/base/test/test-pool.cc @@ -140,7 +140,6 @@ TEST_F(PoolTest, PoolAllocation) pool_free(pool); } -// XXX todo : check state after several get and put TEST_F(PoolTest, PoolPut) { pool_init(pool, DEFAULT_SIZE, 0); @@ -148,9 +147,7 @@ TEST_F(PoolTest, PoolPut) int* elt; pool_get(pool, elt); *elt = 10; - printf("2\n"); pool_put(pool, elt); - printf("3\n"); pool_free(pool); } @@ -169,6 +166,43 @@ TEST_F(PoolTest, PoolGetForceBitmapRealloc) pool_free(pool); } +TEST_F(PoolTest, PoolGetAfterReleasing) +{ + int *elt1 = NULL, *elt2 = NULL, *tmp = NULL; + pool_init(pool, DEFAULT_SIZE, 0); + + // If two elements are requested... + off_t id1 = pool_get(pool, elt1); + pool_get(pool, tmp); + + // ...and the first one is released... + pool_put(pool, elt1); + + // ...requesting a new one should return + // the first one (that was freed) + off_t id2 = pool_get(pool, elt2); + EXPECT_EQ(id1, id2); + EXPECT_EQ(elt1, elt2); + + pool_free(pool); +} + +TEST_F(PoolTest, PoolGetMultipleElementsAfterReleasing) +{ + const int N = 2; + int *elts[N]; + pool_init(pool, N, 0); + + for (int i = 0; i < N; i++) + pool_get(pool, elts[i]); + for (int i = 0; i < N; i++) + pool_put(pool, elts[i]); + for (int i = 0; i < N; i++) + pool_get(pool, elts[i]); + + pool_free(pool); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); -- cgit 1.2.3-korg