aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/base/test
diff options
context:
space:
mode:
Diffstat (limited to 'hicn-light/src/hicn/base/test')
-rw-r--r--hicn-light/src/hicn/base/test/test-bitmap.cc122
-rw-r--r--hicn-light/src/hicn/base/test/test-pool.cc140
-rw-r--r--hicn-light/src/hicn/base/test/test-vector.cc96
3 files changed, 264 insertions, 94 deletions
diff --git a/hicn-light/src/hicn/base/test/test-bitmap.cc b/hicn-light/src/hicn/base/test/test-bitmap.cc
index d6ab94f3e..1a62edba3 100644
--- a/hicn-light/src/hicn/base/test/test-bitmap.cc
+++ b/hicn-light/src/hicn/base/test/test-bitmap.cc
@@ -24,60 +24,114 @@
#include <netinet/in.h>
extern "C" {
-#include <hicn/base/vector.h>
+#define WITH_TESTS
#include <hicn/base/bitmap.h>
}
+#define DEFAULT_SIZE 10
+
class BitmapTest : public ::testing::Test {
- protected:
- BitmapTest() {
- }
+protected:
+ BitmapTest() { }
- virtual ~BitmapTest() {
+ virtual ~BitmapTest() { }
- // You can do clean-up work that doesn't throw exceptions here.
- }
+ bitmap_t * bitmap;
+};
- // If the constructor and destructor are not enough for setting up
- // and cleaning up each test, you can define the following methods:
+/*
+ * TEST: bitmap allocation
+ */
+TEST_F(BitmapTest, BitmapAllocation)
+{
+ int rc;
- virtual void SetUp() {
- bitmap_init(bitmap, 1024);
- }
+ /*
+ * We take a value < 32 on purpose to avoid confusion on the choice of a 32
+ * or 64 bit integer for storage
+ */
+ size_t size_not_pow2 = DEFAULT_SIZE;
+ bitmap_init(bitmap, size_not_pow2, 0);
- virtual void TearDown() {
- free(bitmap);
- }
- uint32_t *bitmap;
-};
+ /*
+ * Bitmap should have been allocated with a size rounded to the next power
+ * of 2
+ */
+ EXPECT_EQ(bitmap_get_alloc_size(bitmap), 1);
+
+ /* By default, no element should be set */
+ EXPECT_FALSE(bitmap_is_set(bitmap, 0));
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 0));
+
+ EXPECT_EQ(bitmap_get_alloc_size(bitmap), 1);
+
+ EXPECT_FALSE(bitmap_is_set(bitmap, size_not_pow2 - 1));
+ EXPECT_TRUE(bitmap_is_unset(bitmap, size_not_pow2 - 1));
+
+ /* Bitmap should not have been reallocated */
+ EXPECT_EQ(bitmap_get_alloc_size(bitmap), 1);
+
+ /* After setting a bit after the end, bitmap should have been reallocated */
+ bitmap_set(bitmap, sizeof(bitmap[0]) * 8 - 1);
+ EXPECT_EQ(bitmap_get_alloc_size(bitmap), 1);
+
+ /* After setting a bit after the end, bitmap should have been reallocated */
+ rc = bitmap_set(bitmap, sizeof(bitmap[0]) * 8);
+ EXPECT_GE(rc, 0);
+ EXPECT_EQ(bitmap_get_alloc_size(bitmap), 2);
+
+ rc = bitmap_set(bitmap, sizeof(bitmap[0]) * 8 + 1);
+ EXPECT_GE(rc, 0);
+ EXPECT_EQ(bitmap_get_alloc_size(bitmap), 2);
+
+ bitmap_free(bitmap);
+
+ size_t size_pow2 = 16;
+
+ /* Limiting test for allocation size */
+ bitmap_init(bitmap, size_pow2, 0);
+ EXPECT_EQ(bitmap_get_alloc_size(bitmap), 1);
+
+ bitmap_free(bitmap);
+}
TEST_F(BitmapTest, BitmapSet)
{
- bitmap_set(bitmap, 20);
- EXPECT_TRUE(bitmap_is_set(bitmap, 20));
- EXPECT_FALSE(bitmap_is_unset(bitmap, 20));
- EXPECT_FALSE(bitmap_is_set(bitmap, 19));
- EXPECT_TRUE(bitmap_is_unset(bitmap, 19));
+ bitmap_init(bitmap, DEFAULT_SIZE, 0);
+
+ bitmap_set(bitmap, 20);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 20));
+ EXPECT_FALSE(bitmap_is_unset(bitmap, 20));
+ EXPECT_FALSE(bitmap_is_set(bitmap, 19));
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 19));
+ bitmap_free(bitmap);
}
TEST_F(BitmapTest, BitmapUnSet) {
- bitmap_set(bitmap, 20);
- bitmap_set(bitmap, 19);
- bitmap_unset(bitmap, 20);
- EXPECT_FALSE(bitmap_is_set(bitmap, 20));
- EXPECT_TRUE(bitmap_is_unset(bitmap, 20));
- EXPECT_TRUE(bitmap_is_set(bitmap, 19));
- EXPECT_FALSE(bitmap_is_unset(bitmap, 19));
+ bitmap_init(bitmap, DEFAULT_SIZE, 0);
+
+ bitmap_set(bitmap, 20);
+ bitmap_set(bitmap, 19);
+ bitmap_unset(bitmap, 20);
+ EXPECT_FALSE(bitmap_is_set(bitmap, 20));
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 20));
+ EXPECT_TRUE(bitmap_is_set(bitmap, 19));
+ EXPECT_FALSE(bitmap_is_unset(bitmap, 19));
+ bitmap_free(bitmap);
}
TEST_F(BitmapTest, BitmapSetTo) {
- bitmap_set_to(bitmap, 40);
- EXPECT_TRUE(bitmap_is_set(bitmap, 20));
- EXPECT_TRUE(bitmap_is_set(bitmap, 21));
- EXPECT_TRUE(bitmap_is_unset(bitmap, 41));
- EXPECT_TRUE(bitmap_is_unset(bitmap, 42));
+ bitmap_init(bitmap, DEFAULT_SIZE, 0);
+
+ bitmap_set_to(bitmap, 40);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 20));
+ EXPECT_TRUE(bitmap_is_set(bitmap, 21));
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 41));
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 42));
+
+ bitmap_free(bitmap);
}
int main(int argc, char **argv)
diff --git a/hicn-light/src/hicn/base/test/test-pool.cc b/hicn-light/src/hicn/base/test/test-pool.cc
index 86c1c1270..1146ef2b7 100644
--- a/hicn-light/src/hicn/base/test/test-pool.cc
+++ b/hicn-light/src/hicn/base/test/test-pool.cc
@@ -24,45 +24,135 @@
#include <netinet/in.h>
extern "C" {
+#define WITH_TESTS
#include <hicn/base/pool.h>
}
+/*
+ * TODO
+ * - test max_size
+ */
+
+#define DEFAULT_SIZE 10
+
class PoolTest : public ::testing::Test {
- protected:
- PoolTest() {
- }
+protected:
+ PoolTest() { }
+ virtual ~PoolTest() { }
- virtual ~PoolTest() {
- // You can do clean-up work that doesn't throw exceptions here.
- }
+ int *pool;
+};
- // If the constructor and destructor are not enough for setting up
- // and cleaning up each test, you can define the following methods:
+TEST_F(PoolTest, PoolAllocation)
+{
+ int rc;
- virtual void SetUp() {
-
- }
+ pool_init(pool, DEFAULT_SIZE, 0);
- virtual void TearDown() {
- pool_free(pool);
- }
+ size_t pool_size = next_pow2(DEFAULT_SIZE);
- int *pool;
-};
+ EXPECT_EQ(pool_get_alloc_size(pool), pool_size);
+
+ /* Check that free indices and bitmaps are correctly initialize */
+ off_t * fi = pool_get_free_indices(pool);
+ EXPECT_EQ(vector_len(fi), pool_size);
+ EXPECT_EQ(fi[0], pool_size - 1);
+ EXPECT_EQ(fi[pool_size - 1], 0);
+
+ /* The allocated size of the underlying vector should be the next power of two */
+ EXPECT_EQ(vector_get_alloc_size(fi), pool_size);
+
+ bitmap_t * fb = pool_get_free_bitmap(pool);
+ EXPECT_TRUE(bitmap_is_set(fb, 0));
+ EXPECT_TRUE(bitmap_is_set(fb, pool_size - 2));
+ EXPECT_TRUE(bitmap_is_set(fb, pool_size - 1));
+ EXPECT_TRUE(bitmap_is_unset(fb, pool_size));
+
+ /* Getting elements from the pool should correctly update the free indices
+ * and bitmap */
+ int * elt;
+
+ rc = pool_get(pool, elt);
+ EXPECT_GE(rc, 0);
+ EXPECT_EQ(vector_len(fi), pool_size - 1);
+ EXPECT_TRUE(bitmap_is_unset(fb, 0));
+
+ rc = pool_get(pool, elt);
+ EXPECT_GE(rc, 0);
+ EXPECT_EQ(vector_len(fi), pool_size - 2);
+ EXPECT_TRUE(bitmap_is_unset(fb, 1));
+
+ for (unsigned i = 0; i < pool_size - 4; i++) {
+ rc = pool_get(pool, elt);
+ EXPECT_GE(rc, 0);
+ }
+
+ rc = pool_get(pool, elt);
+ EXPECT_GE(rc, 0);
+ EXPECT_EQ(vector_len(fi), 1);
+ EXPECT_TRUE(bitmap_is_unset(fb, pool_size - 2));
+
+ rc = pool_get(pool, elt);
+ EXPECT_GE(rc, 0);
+ EXPECT_EQ(vector_len(fi), 0);
+ EXPECT_TRUE(bitmap_is_unset(fb, pool_size - 1));
+ /*
+ * Getting elements within the allocated range should not have triggered a
+ * resize
+ */
+ EXPECT_EQ(pool_len(pool), pool_size);
+
+ /*
+ * Getting elements once the allocated range has been exceeded should
+ * trigger a resize
+ */
+ rc = pool_get(pool, elt);
+ EXPECT_GE(rc, 0);
+
+ EXPECT_EQ(pool_get_alloc_size(pool), pool_size * 2);
+
+ EXPECT_EQ(pool_len(pool), pool_size + 1);
+
+ /*
+ * Doubling the size, we should have again pool_size elements free, minus 1
+ */
+ EXPECT_EQ(pool_get_free_indices_size(pool), pool_size - 1);
+
+ /*
+ * NOTE: this is wrong as there has been a realloc and the old fi
+ * pointer is now invalid
+ */
+ //EXPECT_EQ(vector_len(fi), pool_size - 1);
+
+ /* And the bitmap should also be correctly modified */
+ fb = pool_get_free_bitmap(pool);
+ EXPECT_TRUE(bitmap_is_unset(fb, pool_size));
+
+ /* Check that surrounding values are also correct */
+ EXPECT_TRUE(bitmap_is_unset(fb, pool_size - 1));
+ EXPECT_TRUE(bitmap_is_set(fb, pool_size + 1));
+
+ /* Setting elements after should through */
+
+ /* Check that free indices and bitmaps are correctly updated */
+
+ pool_free(pool);
+}
+
+// XXX todo : check state after several get and put
TEST_F(PoolTest, PoolPut)
{
- pool_init(pool, 1024);
- int* elt;
- pool_get(pool, elt);
- *elt = 10;
+ pool_init(pool, DEFAULT_SIZE, 0);
+
+ int* elt;
+ pool_get(pool, elt);
+ *elt = 10;
printf("2\n");
- pool_put(pool, elt);
+ pool_put(pool, elt);
printf("3\n");
-
- //pool_get(pool)
- //loop_ = loop_create();
- //EXPECT_TRUE(loop_ != NULL);
+
+ pool_free(pool);
}
diff --git a/hicn-light/src/hicn/base/test/test-vector.cc b/hicn-light/src/hicn/base/test/test-vector.cc
index e5eeff910..59571b053 100644
--- a/hicn-light/src/hicn/base/test/test-vector.cc
+++ b/hicn-light/src/hicn/base/test/test-vector.cc
@@ -24,64 +24,90 @@
#include <netinet/in.h>
extern "C" {
+#define WITH_TESTS
#include <hicn/base/vector.h>
}
+/*
+ * TODO
+ * - test max_size
+ */
+
+#define DEFAULT_SIZE 10
+
class VectorTest : public ::testing::Test {
- protected:
- VectorTest() {
- }
+protected:
+ VectorTest() { }
+ virtual ~VectorTest() { }
- virtual ~VectorTest() {
- // You can do clean-up work that doesn't throw exceptions here.
- }
+ int *vector = NULL;
+};
- // If the constructor and destructor are not enough for setting up
- // and cleaning up each test, you can define the following methods:
+/* TEST: Vector allocation and initialization */
+TEST_F(VectorTest, VectorAllocate)
+{
+ vector_init(vector, DEFAULT_SIZE, 0);
- virtual void SetUp() {;
- vector_init(vector, 1024);
- }
+ /* Allocated size should be the next power of two */
+ EXPECT_EQ(vector_get_alloc_size(vector), 16);
- virtual void TearDown() {
- vector_free(vector);
- }
+ /* Setting elements within the allocated size should not trigger a resize */
+ vector_ensure_pos(vector, 15);
+ EXPECT_EQ(vector_get_alloc_size(vector), 16);
- int *vector = NULL;
+ /* Setting elements after should through */
+ vector_ensure_pos(vector, 16);
+ EXPECT_EQ(vector_get_alloc_size(vector), 32);
-};
+ /* Check that free indices and bitmaps are correctly updated */
+
+ vector_free(vector);
+}
TEST_F(VectorTest, VectorSize)
{
- vector_push(vector, 109);
- vector_push(vector, 109);
- int size = vector_len(vector);
- EXPECT_EQ(size, 2);
- vector_push(vector, 109);
- size = vector_len(vector);
- EXPECT_EQ(size, 3);
+ vector_init(vector, DEFAULT_SIZE, 0);
+
+ vector_push(vector, 109);
+ int size = vector_len(vector);
+ EXPECT_EQ(size, 1);
+ vector_push(vector, 109);
+ size = vector_len(vector);
+ EXPECT_EQ(size, 2);
+ vector_push(vector, 109);
+ size = vector_len(vector);
+ EXPECT_EQ(size, 3);
+ vector_free(vector);
}
TEST_F(VectorTest, VectorCheckValue)
{
- vector_push(vector, 109);
- vector_push(vector, 200);
- EXPECT_EQ(vector[0], 109);
- EXPECT_EQ(vector[1], 200);
+ vector_init(vector, DEFAULT_SIZE, 0);
+
+ vector_push(vector, 109);
+ vector_push(vector, 200);
+ EXPECT_EQ(vector[0], 109);
+ EXPECT_EQ(vector[1], 200);
+
+ vector_free(vector);
}
TEST_F(VectorTest, VectorEnsurePos)
{
- printf (" %p\n", vector);
- vector_ensure_pos(vector, 1025);
- for (int i = 0; i <1025; i++) {
- printf("i %d\n", i);
+ vector_init(vector, DEFAULT_SIZE, 0);
+
printf (" %p\n", vector);
- vector_push(vector, i);
- }
- int size = vector_len(vector);
- EXPECT_EQ(size, 1025);
+ vector_ensure_pos(vector, 1025);
+ for (int i = 0; i <1025; i++) {
+ //printf("i %d\n", i);
+ //printf (" %p\n", vector);
+ vector_push(vector, i);
+ }
+ int size = vector_len(vector);
+ EXPECT_EQ(size, 1025);
+
+ vector_free(vector);
}
int main(int argc, char **argv)