rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 4a8310402508ac014c3f5a0863a61e0e4f1f8345
parent 525d69c93fc2c998bbd5650da0d4f808f1b78194
Author: vaplv <vaplv@free.fr>
Date:   Fri, 15 Jul 2016 11:47:53 +0200

Add and test the copy[_and_<clear|release>] hash table functions

Diffstat:
Msrc/hash_table.h | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/test_hash_table.c | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 145 insertions(+), 2 deletions(-)

diff --git a/src/hash_table.h b/src/hash_table.h @@ -509,6 +509,69 @@ HTABLE_FUNC__(size_get)(const struct HTABLE__* htbl) return htbl->table_size_in_use; } +static INLINE res_T +HTABLE_FUNC__(copy)(struct HTABLE__* dst, const struct HTABLE__* src) +{ + res_T res = RES_OK; + ASSERT(dst && src); + + res = HTABLE_DATA_FUNC__(copy)(&dst->table, &src->table); + if(res != RES_OK) goto error; + + res = darray_char_copy(&dst->table_slot_is_used, &src->table_slot_is_used); + if(res != RES_OK) goto error; + + dst->table_size_in_use = src->table_size_in_use; + +exit: + return res; +error: + HTABLE_FUNC__(clear)(dst); + goto exit; +} + +static INLINE res_T +HTABLE_FUNC__(copy_and_clear)(struct HTABLE__* dst, struct HTABLE__* src) +{ + res_T res = RES_OK; + ASSERT(dst && src); + if(dst == src) { + HTABLE_FUNC__(clear)(dst); + return RES_OK; + } + + res = HTABLE_DATA_FUNC__(copy_and_clear)(&dst->table, &src->table); + if(res != RES_OK) goto error; + + res = darray_char_copy_and_clear + (&dst->table_slot_is_used, &src->table_slot_is_used); + if(res != RES_OK) goto error; + + dst->table_size_in_use = src->table_size_in_use; + src->table_size_in_use = 0; + +exit: + return res; +error: + HTABLE_FUNC__(clear)(dst); + goto exit; +} + +static INLINE res_T +HTABLE_FUNC__(copy_and_release)(struct HTABLE__*dst, struct HTABLE__* src) +{ + res_T res = RES_OK; + ASSERT(dst && src); + if(dst == src) { + HTABLE_FUNC__(release)(dst); + } else { + res = HTABLE_FUNC__(copy_and_clear)(dst, src); + if(res == RES_OK) + HTABLE_FUNC__(release)(src); + } + return res; +} + static INLINE void HTABLE_FUNC__(begin) (struct HTABLE__* htbl, diff --git a/src/test_hash_table.c b/src/test_hash_table.c @@ -285,7 +285,7 @@ static void test_htbl_str_darray(void) { struct mem_allocator allocator_proxy; - struct htable_str_darray htbl; + struct htable_str_darray htbl, htbl2, htbl3; struct htable_str_darray_iterator it0; struct htable_str_darray_iterator it1; struct darray_char darray; @@ -355,6 +355,8 @@ ANGVBA BS QRZBAF EHA NZBX VA BHE PVGVRF.", mem_init_proxy_allocator(&allocator_proxy, &mem_default_allocator); htable_str_darray_init(&allocator_proxy, &htbl); + htable_str_darray_init(&allocator_proxy, &htbl2); + htable_str_darray_init(&allocator_proxy, &htbl3); darray_char_init(&allocator_proxy, &darray); str_init(&allocator_proxy, &tmp); @@ -367,6 +369,52 @@ ANGVBA BS QRZBAF EHA NZBX VA BHE PVGVRF.", CHECK(htable_str_darray_set(&htbl, &tmp, &darray), RES_OK); } + CHECK(htable_str_darray_size_get(&htbl), nstrs); + CHECK(htable_str_darray_size_get(&htbl2), 0); + CHECK(htable_str_darray_size_get(&htbl3), 0); + + CHECK(htable_str_darray_copy(&htbl3, &htbl), RES_OK); + CHECK(htable_str_darray_copy(&htbl, &htbl2), RES_OK); + + CHECK(htable_str_darray_size_get(&htbl), 0); + CHECK(htable_str_darray_size_get(&htbl2), 0); + CHECK(htable_str_darray_size_get(&htbl3), nstrs); + + CHECK(htable_str_darray_copy(&htbl, &htbl3), RES_OK); + CHECK(htable_str_darray_copy_and_clear(&htbl2, &htbl3), RES_OK); + + CHECK(htable_str_darray_size_get(&htbl), nstrs); + CHECK(htable_str_darray_size_get(&htbl2), nstrs); + CHECK(htable_str_darray_size_get(&htbl3), 0); + + FOR_EACH(i, 0, nstrs) { + darray_char_clear(&darray); + FOR_EACH(j, 0, strlen(str[i]) + 1) { + CHECK(darray_char_push_back(&darray, str[i] + j), RES_OK); + } + str_set(&tmp, str[i]); + CHECK(htable_str_darray_set(&htbl3, &tmp, &darray), RES_OK); + } + + CHECK(htable_str_darray_size_get(&htbl3), nstrs); + + FOR_EACH(i, 0, nstrs) { + struct darray_char* data; + str_set(&tmp, str[i]); + + data = htable_str_darray_find(&htbl, &tmp); + NCHECK(data, NULL); + CHECK(strcmp(darray_char_cdata_get(data), str[i]), 0); + + data = htable_str_darray_find(&htbl2, &tmp); + NCHECK(data, NULL); + CHECK(strcmp(darray_char_cdata_get(data), str[i]), 0); + + data = htable_str_darray_find(&htbl3, &tmp); + NCHECK(data, NULL); + CHECK(strcmp(darray_char_cdata_get(data), str[i]), 0); + } + FOR_EACH(j, 0, nerase) { for(;;) { i = (size_t)rand() % nstrs; @@ -384,7 +432,24 @@ ANGVBA BS QRZBAF EHA NZBX VA BHE PVGVRF.", CHECK(htable_str_darray_erase(&htbl, &tmp), 1); CHECK(htable_str_darray_erase(&htbl, &tmp), 0); } - CHECK(htable_str_darray_size_get(&htbl), nstrs - 3); + CHECK(htable_str_darray_size_get(&htbl), nstrs - nerase); + + FOR_EACH(j, 0, nerase) { + struct darray_char* data; + str_set(&tmp, str[buf[j]]); + + data = htable_str_darray_find(&htbl, &tmp); + CHECK(data, NULL); + + data = htable_str_darray_find(&htbl2, &tmp); + NCHECK(data, NULL); + CHECK(strcmp(darray_char_cdata_get(data), str[buf[j]]), 0); + + data = htable_str_darray_find(&htbl3, &tmp); + NCHECK(data, NULL); + CHECK(strcmp(darray_char_cdata_get(data), str[buf[j]]), 0); + } + htable_str_darray_begin(&htbl, &it0); htable_str_darray_end(&htbl, &it1); @@ -410,9 +475,24 @@ ANGVBA BS QRZBAF EHA NZBX VA BHE PVGVRF.", CHECK(strcmp(darray_char_cdata_get(data), str_cget(&tmp)), 0); } + htable_str_darray_copy_and_release(&htbl, &htbl3); + FOR_EACH(i, 0, nstrs) { + struct darray_char* data; + str_set(&tmp, str[i]); + + data = htable_str_darray_find(&htbl, &tmp); + NCHECK(data, NULL); + CHECK(strcmp(darray_char_cdata_get(data), str[i]), 0); + + data = htable_str_darray_find(&htbl2, &tmp); + NCHECK(data, NULL); + CHECK(strcmp(darray_char_cdata_get(data), str[i]), 0); + } + str_release(&tmp); darray_char_release(&darray); htable_str_darray_release(&htbl); + htable_str_darray_release(&htbl2); check_memory_allocator(&allocator_proxy); mem_shutdown_proxy_allocator(&allocator_proxy); }