From a0c5b2c9fd052e694f14d4672962f2cc84a95cfa Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Tue, 14 May 2024 17:47:33 +0200 Subject: [PATCH] tests: add basics for dm_hash unit testing Better code coverage. --- test/unit/Makefile | 3 ++- test/unit/dmhash_t.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ test/unit/units.h | 2 ++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/unit/dmhash_t.c diff --git a/test/unit/Makefile b/test/unit/Makefile index 75d6642a3..7298b4ed0 100644 --- a/test/unit/Makefile +++ b/test/unit/Makefile @@ -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 \ diff --git a/test/unit/dmhash_t.c b/test/unit/dmhash_t.c new file mode 100644 index 000000000..e71ed2bf5 --- /dev/null +++ b/test/unit/dmhash_t.c @@ -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); +} diff --git a/test/unit/units.h b/test/unit/units.h index d7ac6adc3..debfd566a 100644 --- a/test/unit/units.h +++ b/test/unit/units.h @@ -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);