From e7f858f6b2c02579f7e9229131e8057acfee600d Mon Sep 17 00:00:00 2001 From: Bartosz Podrygajlo Date: Mon, 30 Sep 2024 20:50:57 +0200 Subject: [PATCH] Add iterator to hashtable and hashtable unit tests --- common/utils/CMakeLists.txt | 3 + common/utils/hashtable/hashtable.c | 21 ++++ common/utils/hashtable/hashtable.h | 11 +- common/utils/hashtable/tests/CMakeLists.txt | 5 + .../utils/hashtable/tests/test_hashtable.cpp | 106 ++++++++++++++++++ 5 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 common/utils/hashtable/tests/CMakeLists.txt create mode 100644 common/utils/hashtable/tests/test_hashtable.cpp diff --git a/common/utils/CMakeLists.txt b/common/utils/CMakeLists.txt index 025091249b..7cf067dd14 100644 --- a/common/utils/CMakeLists.txt +++ b/common/utils/CMakeLists.txt @@ -16,3 +16,6 @@ add_subdirectory(threadPool) add_library(utils utils.c system.c time_meas.c time_stat.c tun_if.c) target_include_directories(utils PUBLIC .) target_link_libraries(utils PRIVATE ${T_LIB}) +if (ENABLE_TESTS) + add_subdirectory(hashtable/tests) +endif() diff --git a/common/utils/hashtable/hashtable.c b/common/utils/hashtable/hashtable.c index e9641f8e0c..bbd03692e4 100644 --- a/common/utils/hashtable/hashtable.c +++ b/common/utils/hashtable/hashtable.c @@ -305,3 +305,24 @@ hashtable_rc_t hashtable_get(const hash_table_t *const hashtblP, const hash_key_ *dataP = NULL; return HASH_TABLE_KEY_NOT_EXISTS; } + +hash_table_iterator_s hashtable_get_iterator(const hash_table_t *const hashtbl) +{ + return (hash_table_iterator_s){.index = 0, .node = hashtbl->nodes[0], .hashtbl = hashtbl}; +} + +bool hashtable_iterator_getnext(hash_table_iterator_s *iterator, void **dataP) +{ + // Iterate over table indexes + while (iterator->node == NULL) { + iterator->index++; + if (iterator->index >= iterator->hashtbl->size) { + *dataP = NULL; + return false; + } + iterator->node = iterator->hashtbl->nodes[iterator->index]; + } + *dataP = iterator->node->data; + iterator->node = iterator->node->next; + return true; +} diff --git a/common/utils/hashtable/hashtable.h b/common/utils/hashtable/hashtable.h index 09f8623ce2..c50c23ae7b 100644 --- a/common/utils/hashtable/hashtable.h +++ b/common/utils/hashtable/hashtable.h @@ -24,6 +24,7 @@ #include #include #include +#include typedef size_t hash_size_t; typedef uint64_t hash_key_t; @@ -54,6 +55,12 @@ typedef struct hash_table_s { void (*freefunc)(void *); } hash_table_t; +typedef struct hash_table_iterator_s { + int index; + hash_node_t* node; + const hash_table_t* const hashtbl; +} hash_table_iterator_s; + char *hashtable_rc_code2string(hashtable_rc_t rcP); void hash_free_int_func(void *memoryP); hash_table_t *hashtable_create (const hash_size_t size, hash_size_t (*hashfunc)(const hash_key_t ), void (*freefunc)(void *)); @@ -63,8 +70,8 @@ hashtable_rc_t hashtable_dump_content (const hash_table_t *const hashtblP, char hashtable_rc_t hashtable_insert (hash_table_t *const hashtbl, const hash_key_t key, void *data); hashtable_rc_t hashtable_remove (hash_table_t *const hashtbl, const hash_key_t key); hashtable_rc_t hashtable_get (const hash_table_t *const hashtbl, const hash_key_t key, void **dataP); - - +hash_table_iterator_s hashtable_get_iterator(const hash_table_t *const hashtbl); +bool hashtable_iterator_getnext(hash_table_iterator_s* iterator, void **dataP); #endif diff --git a/common/utils/hashtable/tests/CMakeLists.txt b/common/utils/hashtable/tests/CMakeLists.txt new file mode 100644 index 0000000000..baf2461262 --- /dev/null +++ b/common/utils/hashtable/tests/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(test_hashtable test_hashtable.cpp) +add_dependencies(tests test_hashtable) +target_link_libraries(test_hashtable PRIVATE HASHTABLE GTest::gtest) +add_test(NAME test_hashtable + COMMAND ./test_hashtable) diff --git a/common/utils/hashtable/tests/test_hashtable.cpp b/common/utils/hashtable/tests/test_hashtable.cpp new file mode 100644 index 0000000000..aa63caa331 --- /dev/null +++ b/common/utils/hashtable/tests/test_hashtable.cpp @@ -0,0 +1,106 @@ +/* + * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The OpenAirInterface Software Alliance licenses this file to You under + * the OAI Public License, Version 1.1 (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.openairinterface.org/?page_id=698 + * + * 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. + *------------------------------------------------------------------------------- + * For more information about the OpenAirInterface (OAI) Software Alliance: + * contact@openairinterface.org + */ + +#include +extern "C" { +#include "hashtable.h" +} + +TEST(hashtable, test_insert_remove) { + hash_table_t* table = hashtable_create(250, NULL, NULL); + int* a = (int*)calloc(1, sizeof(*a)); + EXPECT_EQ(hashtable_insert(table, 1, a), HASH_TABLE_OK); + EXPECT_EQ(hashtable_remove(table, 1), HASH_TABLE_OK); + EXPECT_EQ(hashtable_destroy(&table), HASH_TABLE_OK); +} + +TEST(hashtable, test_insert_iterate_remove) { + hash_table_t* table = hashtable_create(250, NULL, NULL); + int* a = (int*)calloc(1, sizeof(*a)); + EXPECT_EQ(hashtable_insert(table, 1, a), HASH_TABLE_OK); + auto iterator = hashtable_get_iterator(table); + void* ptr; + int num_items = 0; + while(hashtable_iterator_getnext(&iterator, &ptr)) { + num_items++; + } + EXPECT_EQ(num_items, 1); + EXPECT_EQ(hashtable_remove(table, 1), HASH_TABLE_OK); + num_items = 0; + + auto iterator2 = hashtable_get_iterator(table); + while(hashtable_iterator_getnext(&iterator2, &ptr)) { + num_items++; + } + EXPECT_EQ(num_items, 0); + EXPECT_EQ(hashtable_destroy(&table), HASH_TABLE_OK); +} + +TEST(hashtable, test_insert_three_iterate_remove) { + hash_table_t* table = hashtable_create(250, NULL, NULL); + int* a1 = (int*)calloc(1, sizeof(*a1)); + int* a2 = (int*)calloc(1, sizeof(*a2)); + int* a3 = (int*)calloc(1, sizeof(*a3)); + EXPECT_EQ(hashtable_insert(table, 1, a1), HASH_TABLE_OK); + EXPECT_EQ(hashtable_insert(table, 2, a2), HASH_TABLE_OK); + EXPECT_EQ(hashtable_insert(table, 3, a3), HASH_TABLE_OK); + auto iterator = hashtable_get_iterator(table); + void* ptr; + int num_items = 0; + while(hashtable_iterator_getnext(&iterator, &ptr)) { + num_items++; + } + EXPECT_EQ(num_items, 3); + EXPECT_EQ(hashtable_remove(table, 1), HASH_TABLE_OK); + num_items = 0; + + auto iterator2 = hashtable_get_iterator(table); + while(hashtable_iterator_getnext(&iterator2, &ptr)) { + num_items++; + } + EXPECT_EQ(num_items, 2); + EXPECT_EQ(hashtable_destroy(&table), HASH_TABLE_OK); +} + +TEST(hashtable, test_insert_empty_insert_interate) { + hash_table_t* table = hashtable_create(250, NULL, NULL); + int* a = (int*)calloc(1, sizeof(*a)); + EXPECT_EQ(hashtable_insert(table, 1, a), HASH_TABLE_OK); + EXPECT_EQ(hashtable_remove(table, 1), HASH_TABLE_OK); + + int* a2 = (int*)calloc(1, sizeof(*a2)); + EXPECT_EQ(hashtable_insert(table, 1, a2), HASH_TABLE_OK); + auto iterator3 = hashtable_get_iterator(table); + int num_items = 0; + void* ptr; + while(hashtable_iterator_getnext(&iterator3, &ptr)) { + num_items++; + } + EXPECT_EQ(num_items, 1); + + EXPECT_EQ(hashtable_destroy(&table), HASH_TABLE_OK); +} + +int main(int argc, char** argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}