1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

tests: add basics for dm_hash unit testing

Better code coverage.
This commit is contained in:
Zdenek Kabelac 2024-05-14 17:47:33 +02:00
parent e59c84ca3a
commit a0c5b2c9fd
3 changed files with 65 additions and 1 deletions

View File

@ -1,4 +1,4 @@
# Copyright (C) 2011-2018 Red Hat, Inc. All rights reserved.
# Copyright (C) 2011-2024 Red Hat, Inc. All rights reserved.
#
# This file is part of LVM2.
#
@ -20,6 +20,7 @@ UNIT_SOURCE=\
test/unit/bcache_utils_t.c \
test/unit/bitset_t.c \
test/unit/config_t.c \
test/unit/dmhash_t.c \
test/unit/dmlist_t.c \
test/unit/dmstatus_t.c \
test/unit/framework.c \

61
test/unit/dmhash_t.c Normal file
View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2024 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU General Public License v.2.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "units.h"
#include "device_mapper/all.h"
static void test_hash_insert(void *fixture)
{
static const char _keys[] = { '1', '2', '3', '4', '5' };
static const long _vals[] = { 'a', 'b', 'c', 'd', 'e' };
struct dm_hash_node *node;
unsigned i;
struct dm_hash_table *hash = dm_hash_create(10);
T_ASSERT(hash);
for (i = 0; i < DM_ARRAY_SIZE(_keys); i++)
T_ASSERT(dm_hash_insert_binary(hash, &_keys[i], sizeof(_keys[0]), (void*)_vals[i]));
T_ASSERT(dm_hash_get_num_entries(hash) == DM_ARRAY_SIZE(_keys));
/* list unsorted elements */
for (node = dm_hash_get_first(hash); node; node = dm_hash_get_next(hash, node), --i) {
const char *k = dm_hash_get_key(hash, node);
char key = k[0];
void *d = dm_hash_get_data(hash, node);
//long v = (long) d;
//printf("key: %c val: %c\n", key, (char)v);
T_ASSERT(d == dm_hash_lookup_binary(hash, &key, sizeof(key)));
}
T_ASSERT(i == 0);
dm_hash_destroy(hash);
}
#define T(path, desc, fn) register_test(ts, "/base/data-struct/hash/" path, desc, fn)
void dm_hash_tests(struct dm_list *all_tests)
{
struct test_suite *ts = test_suite_create(NULL, NULL);
if (!ts) {
fprintf(stderr, "out of memory\n");
exit(1);
}
T("insert", "inserting hash elements", test_hash_insert);
dm_list_add(all_tests, &ts->list);
}

View File

@ -25,6 +25,7 @@ void bcache_utils_tests(struct dm_list *suites);
void bitset_tests(struct dm_list *suites);
void config_tests(struct dm_list *suites);
void dm_list_tests(struct dm_list *suites);
void dm_hash_tests(struct dm_list *suites);
void dm_status_tests(struct dm_list *suites);
void io_engine_tests(struct dm_list *suites);
void percent_tests(struct dm_list *suites);
@ -41,6 +42,7 @@ static inline void register_all_tests(struct dm_list *suites)
bitset_tests(suites);
config_tests(suites);
dm_list_tests(suites);
dm_hash_tests(suites);
dm_status_tests(suites);
io_engine_tests(suites);
percent_tests(suites);