Skip to content

Commit

Permalink
Extend hashlist_destroy().
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Rule (VM/EMT3) <[email protected]>
  • Loading branch information
timrulebosch committed Oct 23, 2024
1 parent b902d9a commit 71a75af
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
7 changes: 6 additions & 1 deletion dse/clib/collections/hashlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ static __inline__ void hashlist_destroy(HashList* h)
if (h) hashmap_destroy(&h->hash_map);
}

static __inline__ void hashlist_destroy_ext(HashList* h, HashMapDestroyItemCallback cb, void* data)
{
if (h) hashmap_destroy_ext(&h->hash_map, cb, data);
}

static __inline__ uint32_t hashlist_length(HashList* h)
{
if (h) return h->hash_map.used_nodes;
Expand Down Expand Up @@ -69,7 +74,7 @@ static __inline__ void* hashlist_ntl(

// Optionally destroy the original HashList.
if (destroy) {
hashlist_destroy(h);
hashlist_destroy_ext(h, NULL, NULL);
}

return array;
Expand Down
10 changes: 6 additions & 4 deletions tests/collections/test_hashlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ void test_hashlist_ntl(void** state)
{
UNUSED(state);

NamedObject foo = { .name = "foo" };
NamedObject bar = { .name = "bar" };
NamedObject* foo = malloc(sizeof(NamedObject));
NamedObject* bar = malloc(sizeof(NamedObject));
*foo = (NamedObject) { .name = "foo" };
*bar = (NamedObject) { .name = "bar" };

HashList h;
hashlist_init(&h, 2);
hashlist_append(&h, &foo);
hashlist_append(&h, &bar);
hashlist_append(&h, foo);
hashlist_append(&h, bar);

// Call hashlist_ntl to convert to NTL.
NamedObject* ntl = hashlist_ntl(&h, sizeof(NamedObject), false);
Expand Down
24 changes: 12 additions & 12 deletions tests/data/test_marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,12 +685,12 @@ void test_marshal__signalmap_generate(void** state)
MarshalMapSpec source = { .count = tc[i].source.count,
.name = tc[i].source.name,
.signal = tc[i].source.signal,
.scalar = &sou_s_ptr };
.scalar = sou_s_ptr };
double* sig_s_ptr = tc[i].signal.scalar;
MarshalMapSpec signal = { .count = tc[i].signal.count,
.name = tc[i].signal.name,
.signal = tc[i].signal.signal,
.scalar = &sig_s_ptr };
.scalar = sig_s_ptr };

SimpleSet ex_signals;
set_init(&ex_signals);
Expand All @@ -706,8 +706,8 @@ void test_marshal__signalmap_generate(void** state)
} else {
assert_string_equal(msm->name, signal.name);
assert_int_equal(msm->count, tc[i].expect.count);
assert_ptr_equal(msm->signal.scalar, &sig_s_ptr);
assert_ptr_equal(msm->source.scalar, &sou_s_ptr);
assert_ptr_equal(msm->signal.scalar, sig_s_ptr);
assert_ptr_equal(msm->source.scalar, sou_s_ptr);

for (size_t x = 0; x < tc[i].expect.count; x++) {
assert_int_equal(
Expand Down Expand Up @@ -794,14 +794,14 @@ void test_marshal__signalmap_scalar_out(void** state)
msm[0].name = (char*)tc[i].name;
msm[0].count = tc[i].count;
msm[0].signal.index = tc[i].signal_idx;
msm[0].signal.scalar = &sig_s_ptr;
msm[0].signal.scalar = sig_s_ptr;
msm[0].source.index = tc[i].source_idx;
msm[0].source.scalar = &src_s_ptr;
msm[0].source.scalar = src_s_ptr;

assert_ptr_equal(sig_s_ptr, tc[i].signal_scalar);
assert_ptr_equal(sig_s_ptr, *msm[0].signal.scalar);
assert_ptr_equal(sig_s_ptr, msm[0].signal.scalar);
assert_ptr_equal(src_s_ptr, tc[i].source_scalar);
assert_ptr_equal(src_s_ptr, *msm[0].source.scalar);
assert_ptr_equal(src_s_ptr, msm[0].source.scalar);

for (size_t j = 0; j < msm[0].count; j++) {
assert_double_equal(0, src_s_ptr[tc[i].source_idx[j]], 0.0);
Expand Down Expand Up @@ -862,14 +862,14 @@ void test_marshal__signalmap_scalar_in(void** state)
msm[0].name = (char*)tc[i].name;
msm[0].count = tc[i].count;
msm[0].signal.index = tc[i].signal_idx;
msm[0].signal.scalar = &sig_s_ptr;
msm[0].signal.scalar = sig_s_ptr;
msm[0].source.index = tc[i].source_idx;
msm[0].source.scalar = &src_s_ptr;
msm[0].source.scalar = src_s_ptr;

assert_ptr_equal(sig_s_ptr, tc[i].signal_scalar);
assert_ptr_equal(sig_s_ptr, *msm[0].signal.scalar);
assert_ptr_equal(sig_s_ptr, msm[0].signal.scalar);
assert_ptr_equal(src_s_ptr, tc[i].source_scalar);
assert_ptr_equal(src_s_ptr, *msm[0].source.scalar);
assert_ptr_equal(src_s_ptr, msm[0].source.scalar);

for (size_t j = 0; j < msm[0].count; j++) {
assert_double_equal(0, sig_s_ptr[tc[i].signal_idx[j]], 0.0);
Expand Down

0 comments on commit 71a75af

Please sign in to comment.