Skip to content

Commit

Permalink
Fix unitest memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Nov 23, 2024
1 parent aa0a472 commit b64706a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions tests/z_api_encoding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void test_with_schema(void) {
z_encoding_to_string(z_encoding_loan_mut(&e), &s);
assert(strncmp("zenoh/bytes;my_schema", z_string_data(z_string_loan(&s)), z_string_len(z_string_loan(&s))) == 0);
z_encoding_drop(z_encoding_move(&e));
z_string_drop(z_string_move(&s));

z_encoding_from_str(&e, "zenoh/string;");
z_encoding_set_schema_from_substr(z_encoding_loan_mut(&e), "my_schema", 3);
Expand Down
7 changes: 4 additions & 3 deletions tests/z_channels_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
//
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#include "zenoh-pico/api/handlers.h"
#include "zenoh-pico/api/macros.h"
#include "zenoh-pico/net/sample.h"
#include "zenoh-pico/collections/bytes.h"

#undef NDEBUG
#include <assert.h>
Expand All @@ -37,6 +35,7 @@
.attachment = _z_bytes_null(), \
}; \
z_call(*z_loan(closure), &sample); \
_z_bytes_drop(&payload); \
} while (0);

#define _RECV(handler, method, buf) \
Expand Down Expand Up @@ -192,11 +191,13 @@ void zero_size_test(void) {
assert(z_fifo_channel_sample_new(&closure, &fifo_handler, 0) != Z_OK);
assert(z_fifo_channel_sample_new(&closure, &fifo_handler, 1) == Z_OK);
z_drop(z_move(fifo_handler));
z_drop(z_move(closure));

z_owned_ring_handler_sample_t ring_handler;
assert(z_ring_channel_sample_new(&closure, &ring_handler, 0) != Z_OK);
assert(z_ring_channel_sample_new(&closure, &ring_handler, 1) == Z_OK);
z_drop(z_move(ring_handler));
z_drop(z_move(closure));
}

int main(void) {
Expand Down
12 changes: 7 additions & 5 deletions tests/z_collections_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "zenoh-pico/collections/fifo.h"
#include "zenoh-pico/collections/lifo.h"
#include "zenoh-pico/collections/list.h"
#include "zenoh-pico/collections/ring.h"
#include "zenoh-pico/collections/string.h"

Expand Down Expand Up @@ -313,10 +312,10 @@ void int_map_iterator_test(void) {
_z_str_intmap_t map;

map = _z_str_intmap_make();
_z_str_intmap_insert(&map, 10, "A");
_z_str_intmap_insert(&map, 20, "B");
_z_str_intmap_insert(&map, 30, "C");
_z_str_intmap_insert(&map, 40, "D");
_z_str_intmap_insert(&map, 10, _z_str_clone("A"));
_z_str_intmap_insert(&map, 20, _z_str_clone("B"));
_z_str_intmap_insert(&map, 30, _z_str_clone("C"));
_z_str_intmap_insert(&map, 40, _z_str_clone("D"));

#define TEST_MAP(map) \
{ \
Expand Down Expand Up @@ -346,6 +345,9 @@ void int_map_iterator_test(void) {

TEST_MAP(map2);

_z_str_intmap_clear(&map);
_z_str_intmap_clear(&map2);

#undef TEST_MAP
}

Expand Down
6 changes: 6 additions & 0 deletions tests/z_data_struct_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "zenoh-pico/api/primitives.h"
#include "zenoh-pico/api/types.h"
Expand All @@ -29,6 +30,7 @@ void entry_list_test(void) {
_z_transport_peer_entry_list_t *root = _z_transport_peer_entry_list_new();
for (int i = 0; i < 10; i++) {
_z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t));
memset(entry, 0, sizeof(_z_transport_peer_entry_t));
root = _z_transport_peer_entry_list_insert(root, entry);
}
_z_transport_peer_entry_list_t *list = root;
Expand All @@ -39,6 +41,7 @@ void entry_list_test(void) {

for (int i = 0; i < 11; i++) {
_z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t));
memset(entry, 0, sizeof(_z_transport_peer_entry_t));
root = _z_transport_peer_entry_list_insert(root, entry);
}
assert(_z_transport_peer_entry_list_head(root)->_peer_id == _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE - 1);
Expand Down Expand Up @@ -140,6 +143,8 @@ void str_vec_list_intmap_test(void) {

_z_str_intmap_clear(&map);
assert(_z_str_intmap_is_empty(&map) == true);

z_free(s);
}

void _z_slice_custom_deleter(void *data, void *context) {
Expand Down Expand Up @@ -243,6 +248,7 @@ void z_id_to_string_test(void) {
assert(z_string_len(z_string_loan(&id_str)) == 32);
assert(strncmp("0f0e0d0c0b0a09080706050403020100", z_string_data(z_string_loan(&id_str)),
z_string_len(z_string_loan(&id_str))) == 0);
z_string_drop(z_string_move(&id_str));
}

int main(void) {
Expand Down
17 changes: 17 additions & 0 deletions tests/z_endpoint_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,32 @@ int main(void) {

str = _z_string_alias_str("");
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_locator_clear(&lc);

str = _z_string_alias_str("/");
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_locator_clear(&lc);

str = _z_string_alias_str("tcp");
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_locator_clear(&lc);

str = _z_string_alias_str("tcp/");
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_locator_clear(&lc);

str = _z_string_alias_str("127.0.0.1:7447");
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_locator_clear(&lc);

str = _z_string_alias_str("tcp/127.0.0.1:7447?");
assert(_z_locator_from_string(&lc, &str) == _Z_RES_OK);
_z_locator_clear(&lc);

// No metadata defined so far... but this is a valid syntax in principle
str = _z_string_alias_str("tcp/127.0.0.1:7447?invalid=ctrl");
assert(_z_locator_from_string(&lc, &str) == _Z_RES_OK);
_z_locator_clear(&lc);

// Endpoint
printf(">>> Testing endpoints...\n");
Expand All @@ -80,25 +87,32 @@ int main(void) {

str = _z_string_alias_str("");
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("/");
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("tcp");
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("tcp/");
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("127.0.0.1:7447");
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("tcp/127.0.0.1:7447?");
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
_z_endpoint_clear(&ep);

// No metadata defined so far... but this is a valid syntax in principle
str = _z_string_alias_str("tcp/127.0.0.1:7447?invalid=ctrl");
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("udp/127.0.0.1:7447#iface=eth0");
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
Expand All @@ -116,12 +130,15 @@ int main(void) {

str = _z_string_alias_str("udp/127.0.0.1:7447#invalid=eth0");
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("udp/127.0.0.1:7447?invalid=ctrl#iface=eth0");
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
_z_endpoint_clear(&ep);

str = _z_string_alias_str("udp/127.0.0.1:7447?invalid=ctrl#invalid=eth0");
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
_z_endpoint_clear(&ep);

return 0;
}
13 changes: 9 additions & 4 deletions tests/z_msgcodec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,23 @@ char *gen_str(size_t size) {
return str;
}

_z_string_t gen_string(size_t len) {
char *str = gen_str(len);
_z_string_t ret = _z_string_copy_from_str(str);
z_free(str);
return ret;
}

_z_string_svec_t gen_str_array(size_t size) {
_z_string_svec_t sa = _z_string_svec_make(size);
for (size_t i = 0; i < size; i++) {
_z_string_t s = _z_string_copy_from_str(gen_str(16));
_z_string_t s = gen_string(16);
_z_string_svec_append(&sa, &s);
}

return sa;
}

_z_string_t gen_string(size_t len) { return _z_string_alias_str(gen_str(len)); }

_z_locator_array_t gen_locator_array(size_t size) {
_z_locator_array_t la = _z_locator_array_make(size);
for (size_t i = 0; i < size; i++) {
Expand Down Expand Up @@ -341,7 +346,7 @@ void assert_eq_locator_array(const _z_locator_array_t *left, const _z_locator_ar
_z_string_t ls = _z_locator_to_string(l);
_z_string_t rs = _z_locator_to_string(r);

printf("%s:%s", _z_string_data(&ls), _z_string_data(&rs));
printf("%.*s:%.*s", (int)_z_string_len(&ls), _z_string_data(&ls), (int)_z_string_len(&rs), _z_string_data(&rs));
if (i < left->_len - 1) printf(" ");

_z_string_clear(&ls);
Expand Down

0 comments on commit b64706a

Please sign in to comment.