Skip to content

Commit

Permalink
[eclipse-iceoryx#490] Test loan_slice_uninit
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Oct 31, 2024
1 parent 83cfcec commit 3696c76
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,97 @@ TYPED_TEST(ServicePublishSubscribeTest, loan_uninit_send_receive_works) {
ASSERT_THAT(**recv_sample, Eq(payload));
}

TYPED_TEST(ServicePublishSubscribeTest, loan_slice_uninit_send_receive_works) {
constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE;
constexpr uint64_t MAX_SLICE_LEN = 10;

constexpr uint64_t DEFAULT_VALUE_A = 42;
constexpr uint64_t DEFAULT_VALUE_B = 777;
constexpr uint64_t DEFAULT_VALUE_Z = 21;

struct MyNestedStruct {
uint64_t a;
uint64_t b;
};
struct MyStruct {
uint64_t z;
MyNestedStruct data;
};

const auto service_name = iox2_testing::generate_service_name();

auto node = NodeBuilder().create<SERVICE_TYPE>().expect("");
auto service =
node.service_builder(service_name).template publish_subscribe<iox::Slice<MyStruct>>().create().expect("");

auto sut_publisher = service.publisher_builder().max_slice_len(MAX_SLICE_LEN).create().expect("");
auto sut_subscriber = service.subscriber_builder().create().expect("");

auto sample = sut_publisher.loan_slice_uninit(MAX_SLICE_LEN).expect("");

auto counter = 0;
for (auto it = sample.payload_slice().begin(); it < sample.payload_slice().end(); ++it) {
new (it) MyStruct { DEFAULT_VALUE_Z + counter,
MyNestedStruct { DEFAULT_VALUE_A + counter, DEFAULT_VALUE_B + counter } };
++counter;
}

send(assume_init(std::move(sample))).expect("");

auto recv_sample = sut_subscriber.receive().expect("");
ASSERT_TRUE(recv_sample.has_value());

counter = 0;
for (auto it = recv_sample.value().payload_slice().begin(); it < recv_sample.value().payload_slice().end(); ++it) {
ASSERT_THAT(it->z, Eq(DEFAULT_VALUE_Z + counter));
ASSERT_THAT(it->data.a, Eq(DEFAULT_VALUE_A + counter));
ASSERT_THAT(it->data.b, Eq(DEFAULT_VALUE_B + counter));
++counter;
}
}

TYPED_TEST(ServicePublishSubscribeTest, loan_slice_uninit_with_bytes_send_receive_works) {
constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE;

constexpr uint64_t DEFAULT_VALUE_A = 42;
constexpr uint64_t DEFAULT_VALUE_B = 777;
constexpr uint64_t DEFAULT_VALUE_Z = 21;

struct MyNestedStruct {
uint64_t a;
uint64_t b;
};
struct MyStruct {
uint64_t z;
MyNestedStruct data;
};
constexpr size_t STRUCT_SIZE = sizeof(MyStruct);

const auto service_name = iox2_testing::generate_service_name();

auto node = NodeBuilder().create<SERVICE_TYPE>().expect("");
auto service =
node.service_builder(service_name).template publish_subscribe<iox::Slice<uint8_t>>().create().expect("");

auto sut_publisher = service.publisher_builder().max_slice_len(STRUCT_SIZE).create().expect("");
auto sut_subscriber = service.subscriber_builder().create().expect("");

auto sample = sut_publisher.loan_slice_uninit(STRUCT_SIZE).expect("");

new (sample.payload_slice().data())
MyStruct { DEFAULT_VALUE_Z, MyNestedStruct { DEFAULT_VALUE_A, DEFAULT_VALUE_B } };
send(assume_init(std::move(sample))).expect("");

auto recv_sample = sut_subscriber.receive().expect("");
ASSERT_TRUE(recv_sample.has_value());

auto recv_payload = reinterpret_cast<MyStruct*>(recv_sample.value().payload_slice().data());

ASSERT_THAT(recv_payload->z, Eq(DEFAULT_VALUE_Z));
ASSERT_THAT(recv_payload->data.a, Eq(DEFAULT_VALUE_A));
ASSERT_THAT(recv_payload->data.b, Eq(DEFAULT_VALUE_B));
}

TYPED_TEST(ServicePublishSubscribeTest, loan_send_receive_works) {
constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE;

Expand Down

0 comments on commit 3696c76

Please sign in to comment.