From 9b8815935a23a81c440b1d892469b772184e7250 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 22 Jul 2019 14:04:56 -0400 Subject: [PATCH] Unit test for hashing function Signed-off-by: Adam Rutkowski --- .../metadata_collision.c/metadata_collision.c | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/unit/tests/metadata/metadata_collision.c/metadata_collision.c diff --git a/tests/unit/tests/metadata/metadata_collision.c/metadata_collision.c b/tests/unit/tests/metadata/metadata_collision.c/metadata_collision.c new file mode 100644 index 0000000..a07dce4 --- /dev/null +++ b/tests/unit/tests/metadata/metadata_collision.c/metadata_collision.c @@ -0,0 +1,65 @@ +/* + * src/metadata/metadata_collision.c + * ocf_metadata_hash_func + * + * ocf_metadata_get_hash + * + */ + +#undef static + +#undef inline + + +#include +#include +#include +#include +#include "print_desc.h" + +#include "ocf/ocf.h" +#include "metadata.h" +#include "../utils/utils_cache_line.h" + +#include "metadata/metadata_collision.c/metadata_collision_generated_warps.c" + +static void metadata_hash_func_test01(void **state) +{ + struct ocf_cache cache; + bool wrap = false; + ocf_cache_line_t i; + ocf_cache_line_t hash_cur, hash_next; + unsigned c; + ocf_core_id_t core_ids[] = {0, 1, 2, 100, OCF_CORE_MAX}; + ocf_core_id_t core_id; + + print_test_description("Verify that hash function increments by 1 and generates" + "collision after 'hash_table_entries' successive core lines"); + + cache.device = malloc(sizeof(*cache.device)); + cache.device->hash_table_entries = 10; + + for (c = 0; c < sizeof(core_ids) / sizeof(core_ids[0]); c++) { + core_id = core_ids[c]; + for (i = 0; i < cache.device->hash_table_entries + 1; i++) { + hash_cur = ocf_metadata_hash_func(&cache, i, core_id); + hash_next = ocf_metadata_hash_func(&cache, i + 1, core_id); + /* make sure hash value is within expected range */ + assert(hash_cur < cache.device->hash_table_entries); + assert(hash_next < cache.device->hash_table_entries); + /* hash should either increment by 1 or overflow to 0 */ + assert(hash_next == (hash_cur + 1) % cache.device->hash_table_entries); + } + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(metadata_hash_func_test01) + }; + + print_message("Unit test of src/metadata/metadata_collision.c"); + + return cmocka_run_group_tests(tests, NULL, NULL); +}