Skip to content

Commit

Permalink
Fix container tests to pass coverage test (#398)
Browse files Browse the repository at this point in the history
* Add *_capacity *_len function usage

* Add tests for *_init, *_free, *_push_drop

* Fix compiler warning
  • Loading branch information
sashacmc authored Apr 8, 2024
1 parent c613907 commit 72c7a50
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/collections/fifo_mt.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int8_t _z_fifo_mt_push(const void *elem, void *context, z_element_free_f element
}
_Z_RETURN_IF_ERR(zp_mutex_unlock(&f->_mutex))
#else // Z_FEATURE_MULTI_THREAD == 1
_z_fifo_push_drop(&f->_fifo, elem, element_free);
_z_fifo_push_drop(&f->_fifo, (void *)elem, element_free);
#endif // Z_FEATURE_MULTI_THREAD == 1

return _Z_RES_OK;
Expand Down
59 changes: 54 additions & 5 deletions tests/z_collections_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ char *d = "d";
// RING
_Z_RING_DEFINE(_z_str, char)

void print_ring(_z_ring_t *r) {
printf("Ring { capacity: %zu, r_idx: %zu, w_idx: %zu, len: %zu }\n", _z_ring_capacity(r), r->_r_idx, r->_w_idx,
_z_ring_len(r));
void print_ring(_z_str_ring_t *r) {
printf("Ring { capacity: %zu, r_idx: %zu, w_idx: %zu, len: %zu }\n", _z_str_ring_capacity(r), r->_r_idx, r->_w_idx,
_z_str_ring_len(r));
}

void ring_test(void) {
Expand Down Expand Up @@ -120,12 +120,28 @@ void ring_test(void) {
printf("%s == %s\n", d, s);
assert(strcmp(d, s) == 0);
assert(_z_str_ring_is_empty(&r));

_z_str_ring_clear(&r);
}

void ring_test_init_free(void) {
_z_str_ring_t *r = (_z_str_ring_t *)malloc(sizeof(_z_str_ring_t));
_z_str_ring_init(r, 1);
assert(r != NULL);

char *str = (char *)calloc(1, sizeof(char));
_z_str_ring_push_force_drop(r, str);

_z_str_ring_free(&r);
assert(r == NULL);
}

// LIFO
_Z_LIFO_DEFINE(_z_str, char)

void print_lifo(_z_lifo_t *r) { printf("Lifo { capacity: %zu, len: %zu }\n", _z_lifo_capacity(r), _z_lifo_len(r)); }
void print_lifo(_z_str_lifo_t *r) {
printf("Lifo { capacity: %zu, len: %zu }\n", _z_str_lifo_capacity(r), _z_str_lifo_len(r));
}

void lifo_test(void) {
_z_str_lifo_t r = _z_str_lifo_make(3);
Expand Down Expand Up @@ -190,12 +206,28 @@ void lifo_test(void) {
printf("%s == %s\n", a, s);
assert(strcmp(a, s) == 0);
assert(_z_str_lifo_is_empty(&r));

_z_str_lifo_clear(&r);
}

void lifo_test_init_free(void) {
_z_str_lifo_t *r = (_z_str_lifo_t *)malloc(sizeof(_z_str_lifo_t));
_z_str_lifo_init(r, 1);
assert(r != NULL);

char *str = (char *)calloc(1, sizeof(char));
_z_str_lifo_push_drop(r, str);

_z_str_lifo_free(&r);
assert(r == NULL);
}

// FIFO
_Z_FIFO_DEFINE(_z_str, char)

void print_fifo(_z_fifo_t *r) { printf("Fifo { capacity: %zu, len: %zu }\n", _z_fifo_capacity(r), _z_fifo_len(r)); }
void print_fifo(_z_str_fifo_t *r) {
printf("Fifo { capacity: %zu, len: %zu }\n", _z_str_fifo_capacity(r), _z_str_fifo_len(r));
}

void fifo_test(void) {
_z_str_fifo_t r = _z_str_fifo_make(3);
Expand Down Expand Up @@ -260,10 +292,27 @@ void fifo_test(void) {
printf("%s == %s\n", c, s);
assert(strcmp(c, s) == 0);
assert(_z_str_fifo_is_empty(&r));

_z_str_fifo_clear(&r);
}

void fifo_test_init_free(void) {
_z_str_fifo_t *r = (_z_str_fifo_t *)malloc(sizeof(_z_str_fifo_t));
_z_str_fifo_init(r, 1);
assert(r != NULL);

char *str = (char *)calloc(1, sizeof(char));
_z_str_fifo_push_drop(r, str);

_z_str_fifo_free(&r);
assert(r == NULL);
}

int main(void) {
ring_test();
ring_test_init_free();
lifo_test();
lifo_test_init_free();
fifo_test();
fifo_test_init_free();
}

0 comments on commit 72c7a50

Please sign in to comment.