Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
OpenXG-RAN
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenXG
OpenXG-RAN
Commits
e7f858f6
Commit
e7f858f6
authored
Sep 30, 2024
by
Bartosz Podrygajlo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add iterator to hashtable and hashtable unit tests
parent
2c07f008
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
144 additions
and
2 deletions
+144
-2
common/utils/CMakeLists.txt
common/utils/CMakeLists.txt
+3
-0
common/utils/hashtable/hashtable.c
common/utils/hashtable/hashtable.c
+21
-0
common/utils/hashtable/hashtable.h
common/utils/hashtable/hashtable.h
+9
-2
common/utils/hashtable/tests/CMakeLists.txt
common/utils/hashtable/tests/CMakeLists.txt
+5
-0
common/utils/hashtable/tests/test_hashtable.cpp
common/utils/hashtable/tests/test_hashtable.cpp
+106
-0
No files found.
common/utils/CMakeLists.txt
View file @
e7f858f6
...
...
@@ -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
()
common/utils/hashtable/hashtable.c
View file @
e7f858f6
...
...
@@ -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
;
}
common/utils/hashtable/hashtable.h
View file @
e7f858f6
...
...
@@ -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
common/utils/hashtable/tests/CMakeLists.txt
0 → 100644
View file @
e7f858f6
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
)
common/utils/hashtable/tests/test_hashtable.cpp
0 → 100644
View file @
e7f858f6
/*
* 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
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment