From 17b807b8cf781b98c682488f63e4dc0687365b79 Mon Sep 17 00:00:00 2001 From: Jeff Ithier Date: Mon, 28 Oct 2024 16:58:03 +0100 Subject: [PATCH] [#490] Test loan_slice_uninit --- .../src/service_publish_subscribe_tests.cpp | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp b/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp index 3a26963b..78980f68 100644 --- a/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp +++ b/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp @@ -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().expect(""); + auto service = + node.service_builder(service_name).template publish_subscribe>().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().expect(""); + auto service = + node.service_builder(service_name).template publish_subscribe>().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(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;