Skip to content

Commit

Permalink
fix tests and examples for SHM (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowhatter authored Aug 13, 2024
1 parent 3f0f9d8 commit 4859ca8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
1 change: 0 additions & 1 deletion examples/z_ping_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ int main(int argc, char** argv) {
}
z_mutex_unlock(z_loan_mut(mutex));
z_free(results);
z_free(data);
z_undeclare_subscriber(z_move(sub));
z_undeclare_publisher(z_move(pub));
z_drop(z_move(mutex));
Expand Down
19 changes: 13 additions & 6 deletions tests/z_api_shm_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
return result; \
}

#define ASSERT_ERR(result) \
if (result == Z_OK) { \
assert(false); \
return result; \
}

#define ASSERT_TRUE(expr) \
if (!(expr)) { \
assert(false); \
Expand All @@ -43,15 +49,14 @@ int test_shm_buffer(z_moved_shm_mut_t mbuf) {
assert(mbuf._ptr != NULL);
z_owned_shm_mut_t buf;
z_take(&buf, mbuf);
assert(mbuf._ptr == NULL);
ASSERT_CHECK_ERR(*mbuf._ptr);
ASSERT_CHECK(buf);

{ z_loaned_shm_mut_t* loaned = z_loan_mut(buf); }

z_owned_shm_t immut;
z_shm_from_mut(&immut, z_move(buf));
ASSERT_CHECK(immut);
assert(buf->_ptr == NULL);

{ const z_loaned_shm_t* loaned = z_loan(immut); }

Expand Down Expand Up @@ -89,7 +94,8 @@ int test_layouted_allocation(const z_loaned_alloc_layout_t* alloc_layout) {
z_owned_shm_mut_t shm_buf;
z_alloc_error_t shm_error;

if (z_alloc_layout_alloc_gc(&alloc, alloc_layout) == Z_OK) {
z_alloc_layout_alloc_gc(&alloc, alloc_layout);
if (z_check(alloc.buf)) {
ASSERT_OK(test_shm_buffer(z_move(alloc.buf)));
ASSERT_CHECK_ERR(alloc.buf);
return Z_OK;
Expand All @@ -103,7 +109,8 @@ int test_allocation(const z_loaned_shm_provider_t* provider, size_t size, z_allo
z_owned_shm_mut_t shm_buf;
z_alloc_error_t shm_error;

if (z_shm_provider_alloc_gc(&alloc, provider, size, alignment) == Z_OK) {
z_shm_provider_alloc_gc(&alloc, provider, size, alignment);
if (z_check(alloc.buf)) {
ASSERT_OK(test_shm_buffer(z_move(alloc.buf)));
ASSERT_CHECK_ERR(alloc.buf);
return Z_OK;
Expand All @@ -120,7 +127,7 @@ int test_provider(z_owned_shm_provider_t* provider, z_alloc_alignment_t alignmen

// test allocation ERROR
if (buf_err_size) {
ASSERT_TRUE(!test_allocation(z_loan(*provider), buf_err_size, alignment));
ASSERT_ERR(test_allocation(z_loan(*provider), buf_err_size, alignment));
}

// OK layouted allocations
Expand All @@ -144,7 +151,7 @@ int test_provider(z_owned_shm_provider_t* provider, z_alloc_alignment_t alignmen
ASSERT_OK(z_alloc_layout_new(&alloc_layout, z_loan(*provider), buf_err_size, alignment));
ASSERT_CHECK(alloc_layout);
// test layouted allocation ERROR
ASSERT_TRUE(!test_layouted_allocation(z_loan(alloc_layout)));
ASSERT_ERR(test_layouted_allocation(z_loan(alloc_layout)));
z_drop(z_move(alloc_layout));
ASSERT_CHECK_ERR(alloc_layout);
}
Expand Down

0 comments on commit 4859ca8

Please sign in to comment.