aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAngelo Mantellini <angelo.mantellini@cisco.com>2020-09-18 14:11:42 +0200
committerAngelo Mantellini <angelo.mantellini@cisco.com>2020-09-18 14:26:06 +0200
commit351bac0bbba54bf3e44f89b13edd4df80831fedf (patch)
tree523b6cfce4e968663c40440388bdcec5394470f5
parent91529a777fd3f56aeadef19dc25e196ba2e1cc51 (diff)
[HICN-636] add gtests for vector, pool, hash, khash, bitmap
Signed-off-by: Angelo Mantellini <angelo.mantellini@cisco.com> Change-Id: I6f3dd773a520ac3f4c4c2e69988082ba326cd61e Signed-off-by: Angelo Mantellini <angelo.mantellini@cisco.com>
-rw-r--r--hicn-light/src/hicn/base/pool.h4
-rw-r--r--hicn-light/src/hicn/base/test/CMakeLists.txt5
-rw-r--r--hicn-light/src/hicn/base/test/test-bitmap.cc87
-rw-r--r--hicn-light/src/hicn/base/test/test-hash.cc57
-rw-r--r--hicn-light/src/hicn/base/test/test-khash.cc139
-rw-r--r--hicn-light/src/hicn/base/test/test-pool.cc73
-rw-r--r--hicn-light/src/hicn/base/test/test-vector.cc97
-rw-r--r--hicn-light/src/hicn/base/vector.c7
-rw-r--r--hicn-light/src/hicn/base/vector.h6
9 files changed, 469 insertions, 6 deletions
diff --git a/hicn-light/src/hicn/base/pool.h b/hicn-light/src/hicn/base/pool.h
index 360dedc..cdb0fc3 100644
--- a/hicn-light/src/hicn/base/pool.h
+++ b/hicn-light/src/hicn/base/pool.h
@@ -28,7 +28,9 @@
/** Local variable naming macro. */
#define _pool_var(v) _pool_##v
-
+#ifndef u64
+#define u64 uint64_t
+#endif
typedef struct {
size_t elt_size;
diff --git a/hicn-light/src/hicn/base/test/CMakeLists.txt b/hicn-light/src/hicn/base/test/CMakeLists.txt
index 03c5a89..351e7bd 100644
--- a/hicn-light/src/hicn/base/test/CMakeLists.txt
+++ b/hicn-light/src/hicn/base/test/CMakeLists.txt
@@ -3,7 +3,12 @@
include(BuildMacros)
list(APPEND TESTS
+ test-bitmap
+ test-hash
+ test-khash
test-loop
+ test-pool
+ test-vector
)
foreach(test ${TESTS})
diff --git a/hicn-light/src/hicn/base/test/test-bitmap.cc b/hicn-light/src/hicn/base/test/test-bitmap.cc
new file mode 100644
index 0000000..4aee8a1
--- /dev/null
+++ b/hicn-light/src/hicn/base/test/test-bitmap.cc
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+extern "C" {
+#include <hicn/base/vector.h>
+#include <hicn/base/bitmap.h>
+}
+
+class BitmapTest : public ::testing::Test {
+ protected:
+ BitmapTest() {
+ }
+
+ virtual ~BitmapTest() {
+
+ // You can do clean-up work that doesn't throw exceptions here.
+ }
+
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {
+ bitmap_init(bitmap, 1024);
+ }
+
+ virtual void TearDown() {
+ free(bitmap);
+ }
+ uint32_t *bitmap;
+};
+
+TEST_F(BitmapTest, BitmapSet)
+{
+ bitmap_set(bitmap, 20);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 20) == true);
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 20) == false);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 19) == false);
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 19) == true);
+
+}
+
+TEST_F(BitmapTest, BitmapUnSet) {
+ bitmap_set(bitmap, 20);
+ bitmap_set(bitmap, 19);
+ bitmap_unset(bitmap, 20);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 20) == false);
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 20) == true);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 19) == true);
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 19) == false);
+
+}
+
+TEST_F(BitmapTest, BitmapSetTo) {
+ bitmap_set_to(bitmap, 40);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 20) == true);
+ EXPECT_TRUE(bitmap_is_set(bitmap, 21) == true);
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 41) == true);
+ EXPECT_TRUE(bitmap_is_unset(bitmap, 42) == true);
+}
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/hicn-light/src/hicn/base/test/test-hash.cc b/hicn-light/src/hicn/base/test/test-hash.cc
new file mode 100644
index 0000000..78e3187
--- /dev/null
+++ b/hicn-light/src/hicn/base/test/test-hash.cc
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+extern "C" {
+#include <hicn/base/hash.h>
+}
+
+class HashTest : public ::testing::Test {
+ protected:
+ HashTest() {
+ }
+
+ virtual ~HashTest() {
+ // You can do clean-up work that doesn't throw exceptions here.
+ }
+
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {
+ // Code here will be called immediately after the constructor (right
+ // before each test).
+ }
+
+ virtual void TearDown() {
+ // Code here will be called immediately after each test (right
+ // before the destructor).
+ }
+};
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/hicn-light/src/hicn/base/test/test-khash.cc b/hicn-light/src/hicn/base/test/test-khash.cc
new file mode 100644
index 0000000..798063c
--- /dev/null
+++ b/hicn-light/src/hicn/base/test/test-khash.cc
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+extern "C" {
+#include <hicn/base/khash.h>
+
+}
+
+KHASH_MAP_INIT_INT(int, unsigned char)
+
+typedef struct {
+ unsigned key;
+ unsigned char val;
+} int_unpack_t;
+
+typedef struct {
+ unsigned key;
+ unsigned char val;
+} __attribute__ ((__packed__)) int_packed_t;
+
+#define hash_eq(a, b) ((a).key == (b).key)
+#define hash_func(a) ((a).key)
+
+KHASH_INIT(iun, int_unpack_t, char, 0, hash_func, hash_eq)
+KHASH_INIT(ipk, int_packed_t, char, 0, hash_func, hash_eq)
+
+class KHashTest : public ::testing::Test {
+ protected:
+ KHashTest() {
+ }
+
+ virtual ~KHashTest() {
+ // You can do clean-up work that doesn't throw exceptions here.
+ }
+
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {
+
+ khash = kh_init(int);
+ }
+
+ virtual void TearDown() {
+
+ kh_destroy(int, khash);
+
+ }
+ khash_t(int) *khash;
+};
+
+
+TEST_F(KHashTest, KhashIntSize)
+{
+ int ret;
+ int k;
+ int size = kh_size(khash);
+
+ EXPECT_EQ(size, 0);
+ k = kh_put(int, khash, 10, &ret);
+ if (ret == 1) {
+ kh_val(khash, k) = 10;
+ }
+ size = kh_size(khash);
+ EXPECT_EQ(size, 1);
+
+}
+
+TEST_F(KHashTest, KhashIntPut)
+{
+ int ret;
+ int k;
+ k = kh_put(int, khash, 10, &ret);
+ if (ret == 1) {
+ kh_val(khash, k) = 10;
+ }
+ int size = kh_size(khash);
+ EXPECT_EQ(size, 1);
+ k = kh_put(int, khash, 20, &ret);
+ if (ret == 1) {
+ kh_val(khash, k) = 20;
+ }
+ size = kh_size(khash);
+ EXPECT_EQ(size, 2);
+}
+
+TEST_F(KHashTest, KhashCheckValue)
+{
+ int ret;
+ int k;
+ k = kh_put(int, khash, 10, &ret);
+ if (ret == 1) {
+ kh_val(khash, k) = 100;
+ }
+ k = kh_put(int, khash, 20, &ret);
+ if (ret == 1) {
+ kh_val(khash, k) = 200;
+ }
+
+ k = kh_put(int, khash, 10, &ret);
+ int val = -1;
+ if (!ret)
+ val = kh_val(khash, k);
+ EXPECT_EQ(val, 100);
+
+ k = kh_put(int, khash, 20, &ret);
+ val = -1;
+ if (!ret)
+ val = kh_val(khash, k);
+ EXPECT_EQ(val, 200);
+}
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/hicn-light/src/hicn/base/test/test-pool.cc b/hicn-light/src/hicn/base/test/test-pool.cc
new file mode 100644
index 0000000..86c1c12
--- /dev/null
+++ b/hicn-light/src/hicn/base/test/test-pool.cc
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+extern "C" {
+#include <hicn/base/pool.h>
+}
+
+class PoolTest : public ::testing::Test {
+ protected:
+ PoolTest() {
+ }
+
+ virtual ~PoolTest() {
+ // You can do clean-up work that doesn't throw exceptions here.
+ }
+
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {
+
+ }
+
+ virtual void TearDown() {
+ pool_free(pool);
+ }
+
+ int *pool;
+};
+
+TEST_F(PoolTest, PoolPut)
+{
+ pool_init(pool, 1024);
+ int* elt;
+ pool_get(pool, elt);
+ *elt = 10;
+ printf("2\n");
+ pool_put(pool, elt);
+ printf("3\n");
+
+ //pool_get(pool)
+ //loop_ = loop_create();
+ //EXPECT_TRUE(loop_ != NULL);
+}
+
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/hicn-light/src/hicn/base/test/test-vector.cc b/hicn-light/src/hicn/base/test/test-vector.cc
new file mode 100644
index 0000000..71b38a2
--- /dev/null
+++ b/hicn-light/src/hicn/base/test/test-vector.cc
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+extern "C" {
+#include <hicn/base/vector.h>
+}
+
+class VectorTest : public ::testing::Test {
+ protected:
+ VectorTest() {
+ }
+
+ virtual ~VectorTest() {
+ // You can do clean-up work that doesn't throw exceptions here.
+ }
+
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {;
+ vector_init(vector, 1024);
+ }
+
+ virtual void TearDown() {
+ vector_free(vector);
+ }
+
+ int *vector = NULL;
+
+};
+
+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);
+
+}
+
+TEST_F(VectorTest, VectorCheckValue)
+{
+ vector_push(vector, 109);
+ vector_push(vector, 200);
+ EXPECT_EQ(vector[0], 109);
+ EXPECT_EQ(vector[1], 200);
+
+}
+
+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);
+ printf (" %p\n", vector);
+
+
+ vector_push(vector, i);
+
+ }
+ int size = vector_len(vector);
+ EXPECT_EQ(size, 1025);
+}
+
+
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/hicn-light/src/hicn/base/vector.c b/hicn-light/src/hicn/base/vector.c
index 00ed7c3..d090588 100644
--- a/hicn-light/src/hicn/base/vector.c
+++ b/hicn-light/src/hicn/base/vector.c
@@ -20,6 +20,7 @@
#include <stddef.h> // size_t
#include <stdlib.h> // calloc
+#include <stdio.h>
#include "vector.h"
@@ -27,7 +28,9 @@ void
_vector_init(void ** vector_ptr, size_t elt_size, size_t max_elts)
{
vector_hdr_t * vh = calloc(VECTOR_HDRLEN + elt_size * max_elts, 1);
- *vector_ptr = (uint8_t*)vh - VECTOR_HDRLEN;
+ *vector_ptr = (uint8_t*)vh + VECTOR_HDRLEN;
+ vh->max_elts = max_elts;
+ vh->num_elts = 0;
}
void
@@ -50,5 +53,5 @@ _vector_resize(void ** vector_ptr, size_t elt_size, off_t pos)
vh->max_elts = new_elts;
/* Reassign vector pointer */
- *vector_ptr = (uint8_t*) + VECTOR_HDRLEN;
+ *vector_ptr = (uint8_t*)vh + VECTOR_HDRLEN;
}
diff --git a/hicn-light/src/hicn/base/vector.h b/hicn-light/src/hicn/base/vector.h
index 9b99ba8..47cfdd1 100644
--- a/hicn-light/src/hicn/base/vector.h
+++ b/hicn-light/src/hicn/base/vector.h
@@ -49,7 +49,7 @@ void _vector_resize(void ** vector_ptr, size_t elt_size, off_t pos);
_vector_init((void**)&vector, sizeof(vector[0]), max_elts)
#define vector_free(vector) \
- _vector_free(&vector)
+ _vector_free((void **)&vector)
#define vector_len(vector) (vector_hdr(vector)->num_elts)
@@ -57,13 +57,13 @@ void _vector_resize(void ** vector_ptr, size_t elt_size, off_t pos);
#define vector_ensure_pos(vector, pos) \
do { \
- if ((pos) >= vector_len(vector)) \
+ if ((pos) >= vector_hdr(vector)->max_elts) \
_vector_resize((void**)&(vector), sizeof((vector)[0]), pos); \
} while(0)
#define vector_push(vector, elt) \
do { \
- vector_ensure_pos(vector_len(vector)); \
+ vector_ensure_pos(vector, vector_len(vector)); \
vector[vector_len(vector)++] = elt; \
} while(0)