Commit e7f858f6 authored by Bartosz Podrygajlo's avatar Bartosz Podrygajlo

Add iterator to hashtable and hashtable unit tests

parent 2c07f008
......@@ -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()
......@@ -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;
}
......@@ -24,6 +24,7 @@
#include<stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
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
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)
/*
* 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 <gtest/gtest.h>
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();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment