Skip to content

Commit

Permalink
Add channel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Apr 4, 2024
1 parent 19ddef3 commit afb24bf
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ if(UNIX OR MSVC)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c)
add_executable(z_channels_test ${PROJECT_SOURCE_DIR}/tests/z_channels_test.c)
add_executable(z_collections_test ${PROJECT_SOURCE_DIR}/tests/z_collections_test.c)
add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c)
add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c)
Expand All @@ -344,6 +345,7 @@ if(UNIX OR MSVC)
add_executable(z_perf_rx ${PROJECT_SOURCE_DIR}/tests/z_perf_rx.c)

target_link_libraries(z_data_struct_test ${Libname})
target_link_libraries(z_channels_test ${Libname})
target_link_libraries(z_collections_test ${Libname})
target_link_libraries(z_endpoint_test ${Libname})
target_link_libraries(z_iobuf_test ${Libname})
Expand All @@ -363,6 +365,8 @@ if(UNIX OR MSVC)

enable_testing()
add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test)
add_test(z_channels_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_channels_test)
add_test(z_collections_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_collections_test)
add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test)
add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test)
add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test)
Expand Down Expand Up @@ -477,4 +481,4 @@ if(PACKAGING)
include(CPack)
endif()

endif()
endif()
8 changes: 4 additions & 4 deletions include/zenoh-pico/api/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ z_owned_sample_t *_z_sample_to_owned_ptr(const _z_sample_t *src);
static inline void _z_##name##_elem_move(void *dst, const void *src) { \
elem_move_f((recv_type *)dst, (const recv_type *)src); \
} \
static inline void _z_##name##_push(const send_type *elem, void *context) { \
static inline void _z_##name##_send(const send_type *elem, void *context) { \
void *internal_elem = elem_convert_f(elem); \
if (internal_elem == NULL) { \
return; \
Expand All @@ -54,7 +54,7 @@ z_owned_sample_t *_z_sample_to_owned_ptr(const _z_sample_t *src);
_Z_ERROR("%s failed: %i", #collection_push_f, res); \
} \
} \
static inline void _z_##name##_pull(recv_type *elem, void *context) { \
static inline void _z_##name##_recv(recv_type *elem, void *context) { \
int8_t res = collection_pull_f(elem, context, _z_##name##_elem_move); \
if (res) { \
_Z_ERROR("%s failed: %i", #collection_pull_f, res); \
Expand All @@ -64,8 +64,8 @@ z_owned_sample_t *_z_sample_to_owned_ptr(const _z_sample_t *src);
static inline z_owned_##name##_t z_##name(size_t capacity) { \
z_owned_##name##_t channel; \
channel.collection = collection_new_f(capacity); \
channel.send = z_##send_closure_name(_z_##name##_push, NULL, channel.collection); \
channel.recv = z_##recv_closure_name(_z_##name##_pull, NULL, channel.collection); \
channel.send = z_##send_closure_name(_z_##name##_send, NULL, channel.collection); \
channel.recv = z_##recv_closure_name(_z_##name##_recv, NULL, channel.collection); \
return channel; \
} \
static inline z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \
Expand Down
118 changes: 118 additions & 0 deletions tests/z_channels_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#include "zenoh-pico/api/handlers.h"

#undef NDEBUG
#include <assert.h>

#define SEND(channel, v) \
do { \
z_sample_t sample; \
sample.payload.start = (const uint8_t *)v; \
sample.payload.len = strlen(v); \
sample.keyexpr = _z_rname("key"); \
z_call(channel.send, &sample); \
} while (0);

#define RECV(channel, buf) \
do { \
z_owned_sample_t sample = z_sample_null(); \
z_call(channel.recv, &sample); \
if (z_check(sample)) { \
strncpy(buf, (const char *)z_loan(sample).payload.start, (size_t)z_loan(sample).payload.len); \
buf[z_loan(sample).payload.len] = '\0'; \
z_drop(z_move(sample)); \
} else { \
buf[0] = '\0'; \
} \
} while (0);

void sample_fifo_channel_test(void) {
z_owned_sample_fifo_channel_t channel = z_sample_fifo_channel(10);

SEND(channel, "v1");
SEND(channel, "v22");
SEND(channel, "v333");
SEND(channel, "v4444");

char buf[100];

RECV(channel, buf);
assert(strcmp(buf, "v1") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v22") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v333") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v4444") == 0);

z_drop(z_move(channel));
}

void sample_ring_channel_test_in_size(void) {
z_owned_sample_ring_channel_t channel = z_sample_ring_channel(10);

SEND(channel, "v1");
SEND(channel, "v22");
SEND(channel, "v333");
SEND(channel, "v4444");

char buf[100];

RECV(channel, buf);
assert(strcmp(buf, "v1") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v22") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v333") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v4444") == 0);
RECV(channel, buf);
assert(strcmp(buf, "") == 0);

z_drop(z_move(channel));
}

void sample_ring_channel_test_over_size(void) {
z_owned_sample_ring_channel_t channel = z_sample_ring_channel(3);

SEND(channel, "v1");
SEND(channel, "v22");
SEND(channel, "v333");
SEND(channel, "v4444");

char buf[100];

RECV(channel, buf);
assert(strcmp(buf, "v22") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v333") == 0);
RECV(channel, buf);
assert(strcmp(buf, "v4444") == 0);
RECV(channel, buf);
assert(strcmp(buf, "") == 0);

z_drop(z_move(channel));
}

int main(void) {
sample_fifo_channel_test();
sample_ring_channel_test_in_size();
sample_ring_channel_test_over_size();
}

0 comments on commit afb24bf

Please sign in to comment.