Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iox #1196 fix serialization warnings #1459

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class Serialization

/// @brief string conversion operator, returns the raw serialized string
/// @return serialized string
// the whole file will be refactored to remove 'std::string'; this lint will also be handled with at the refactoring
// NOLINTNEXTLINE(hicpp-explicit-conversions)
operator std::string() const noexcept;

/// @brief Create Serialization if every arguments is convertable to string
Expand Down Expand Up @@ -114,7 +116,7 @@ class Serialization

private:
std::string m_value;
static constexpr char separator = ':';
static constexpr char SEPARATOR = ':';

private:
static std::string serializer() noexcept;
Expand Down
30 changes: 10 additions & 20 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/serialization.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef IOX_HOOFS_CXX_SERIALIZATION_INL
#define IOX_HOOFS_CXX_SERIALIZATION_INL

#include "iceoryx_hoofs/cxx/serialization.hpp"

namespace iox
{
namespace cxx
Expand Down Expand Up @@ -74,19 +76,12 @@ inline std::string Serialization::serializer(const T& t, const Targs&... args) n
std::string serializedString = getString(t);
std::string serializedStringLength = convert::toString(serializedString.size());

return serializedStringLength + separator + serializedString + serializer(args...);
return serializedStringLength + SEPARATOR + serializedString + serializer(args...);
}

inline bool Serialization::deserialize(const std::string& serializedString) noexcept
{
if (serializedString.empty())
{
return true;
}
else
{
return false;
}
return serializedString.empty();
}

template <typename T, typename... Targs>
Expand All @@ -110,25 +105,25 @@ inline bool Serialization::deserialize(const std::string& serializedString, T& t

inline bool Serialization::removeFirstEntry(std::string& firstEntry, std::string& remainder) noexcept
{
uint64_t pos = remainder.find_first_of(separator);
uint64_t pos = remainder.find_first_of(SEPARATOR);
if (pos == std::string::npos)
{
return false;
}

uint64_t length;
uint64_t length{0};
if (!convert::fromString(remainder.substr(0, pos).c_str(), length))
{
return false;
}

if (remainder.size() < pos + length + 1u)
if (remainder.size() < pos + length + 1U)
{
return false;
}

firstEntry = remainder.substr(pos + 1u, length);
remainder = remainder.substr(pos + 1u + length);
firstEntry = remainder.substr(pos + 1U, length);
remainder = remainder.substr(pos + 1U + length);

return true;
}
Expand All @@ -146,12 +141,7 @@ inline bool Serialization::getNth(const unsigned int index, T& t) const noexcept
}
}

if (!convert::fromString(entry.c_str(), t))
{
return false;
}

return true;
return convert::fromString(entry.c_str(), t);
}
} // namespace cxx
} // namespace iox
Expand Down
85 changes: 48 additions & 37 deletions iceoryx_hoofs/test/moduletests/test_cxx_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ using namespace ::testing;
class Serialization_test : public Test
{
public:
void SetUp()
void SetUp() override
{
internal::CaptureStderr();
}
virtual void TearDown()
void TearDown() override
{
std::string output = internal::GetCapturedStderr();
if (Test::HasFailure())
Expand All @@ -49,94 +49,106 @@ TEST_F(Serialization_test, CreateSingleEntry)
TEST_F(Serialization_test, CreateMultiEntry)
{
::testing::Test::RecordProperty("TEST_ID", "5baf4079-20f4-4ec2-a5f9-38f61460b61b");
auto serial = iox::cxx::Serialization::create("hello world", 12345);
EXPECT_THAT(static_cast<std::string>(serial), Eq("11:hello world5:12345"));
const auto serial = iox::cxx::Serialization::create("hello world", 12345);
EXPECT_THAT(serial.toString(), StrEq("11:hello world5:12345"));
}

TEST_F(Serialization_test, ExtractSingleEntry)
{
::testing::Test::RecordProperty("TEST_ID", "47b75a1a-f133-453b-adad-c82e5ea77565");
auto serial = iox::cxx::Serialization::create(12345);
int i;
constexpr uint64_t NUMBER{12345};
auto serial = iox::cxx::Serialization::create(NUMBER);
uint64_t i{0};
EXPECT_THAT(serial.extract(i), Eq(true));
EXPECT_THAT(i, Eq(12345));
EXPECT_THAT(i, Eq(NUMBER));
}

TEST_F(Serialization_test, ExtractSingleEntryWrongType)
{
::testing::Test::RecordProperty("TEST_ID", "4bba7ed7-0485-48fc-b979-76a7b8e97b2a");
auto serial = iox::cxx::Serialization::create("asd");
int i;
uint64_t i{0};
EXPECT_THAT(serial.extract(i), Eq(false));
}

TEST_F(Serialization_test, ExtractMultiEntry)
{
::testing::Test::RecordProperty("TEST_ID", "cf0be6f1-e986-499c-b96e-339cdc9cb534");
auto serial = iox::cxx::Serialization::create(12345, 'c', "aasd");
int i;
char c;
constexpr uint64_t I{1234};
constexpr char C{'c'};
constexpr const char* S{"aasd"};
const auto serial = iox::cxx::Serialization::create(I, C, S);
uint64_t i{0};
char c{'a'};
std::string s;
EXPECT_THAT(serial.extract(i, c, s), Eq(true));
EXPECT_THAT(i, Eq(12345));
EXPECT_THAT(c, Eq('c'));
EXPECT_THAT(s, Eq("aasd"));
EXPECT_THAT(i, Eq(I));
EXPECT_THAT(c, Eq(C));
EXPECT_THAT(s, StrEq(S));
}

TEST_F(Serialization_test, ExtractMultiEntryWrongType)
{
::testing::Test::RecordProperty("TEST_ID", "8e0cdb0f-7a6f-4fce-a04d-bb665f5692e1");
auto serial = iox::cxx::Serialization::create(12345, 'c', "aasd");
int i;
char c;
char s;
EXPECT_THAT(serial.extract(i, c, s), Eq(false));
constexpr uint64_t I{12345};
constexpr char C{'x'};
constexpr const char* S{"asdasd"};
auto serial = iox::cxx::Serialization::create(I, C, S);
uint64_t i{0};
char c1{'a'};
char c2{'a'};
EXPECT_THAT(serial.extract(i, c1, c2), Eq(false));
}

TEST_F(Serialization_test, GetNthSingleEntry)
{
::testing::Test::RecordProperty("TEST_ID", "39b3a9d5-e3e1-4131-8f1a-5bbeaf199fcb");
auto serial = iox::cxx::Serialization::create(12345);
int i;
constexpr uint64_t I{123456};
auto serial = iox::cxx::Serialization::create(I);
uint64_t i{0};
EXPECT_THAT(serial.getNth(0, i), Eq(true));
EXPECT_THAT(i, Eq(12345));
EXPECT_THAT(i, Eq(I));
}

TEST_F(Serialization_test, GetNthSingleEntryWrongType)
{
::testing::Test::RecordProperty("TEST_ID", "83d32bc1-d732-483b-a8e8-ca71b9ca9c9c");
auto serial = iox::cxx::Serialization::create("a1234a5");
int i;
uint64_t i{0};
EXPECT_THAT(serial.getNth(0, i), Eq(false));
}

TEST_F(Serialization_test, GetNthMultiEntry)
{
::testing::Test::RecordProperty("TEST_ID", "00fb34ec-2135-4824-bd8f-a681ac21d215");
auto serial = iox::cxx::Serialization::create(12345, "asdasd", 'x', -123);
int v1;
constexpr uint64_t V1{12345};
constexpr const char* V2{"asdasd"};
constexpr char V3{'x'};
constexpr int64_t V4{-123};
const auto serial = iox::cxx::Serialization::create(V1, V2, V3, V4);
uint64_t v1{0};
std::string v2;
char v3;
int v4;
char v3{'a'};
int64_t v4{0};
EXPECT_THAT(serial.getNth(0, v1), Eq(true));
EXPECT_THAT(serial.getNth(1, v2), Eq(true));
EXPECT_THAT(serial.getNth(2, v3), Eq(true));
EXPECT_THAT(serial.getNth(3, v4), Eq(true));

EXPECT_THAT(v1, Eq(12345));
EXPECT_THAT(v2, Eq("asdasd"));
EXPECT_THAT(v3, Eq('x'));
EXPECT_THAT(v4, Eq(-123));
EXPECT_THAT(v1, Eq(V1));
EXPECT_THAT(v2, StrEq(V2));
EXPECT_THAT(v3, Eq(V3));
EXPECT_THAT(v4, Eq(V4));
}

TEST_F(Serialization_test, ExtractFromGivenSerialization)
{
::testing::Test::RecordProperty("TEST_ID", "38d6ffe5-6ca2-4dd6-9b2d-0002eb4e312f");
iox::cxx::Serialization serial("6:hello!4:1234");
std::string v1;
int v2;
uint64_t v2{0};
EXPECT_THAT(serial.extract(v1, v2), Eq(true));
EXPECT_THAT(v1, Eq("hello!"));
EXPECT_THAT(v1, StrEq("hello!"));
EXPECT_THAT(v2, Eq(1234));
}

Expand All @@ -145,12 +157,11 @@ TEST_F(Serialization_test, SerializeSerializableClass)
::testing::Test::RecordProperty("TEST_ID", "f9a4d22a-a4ca-4451-8f35-27ffff65cd2d");
struct A
{
A()
{
}
A(const iox::cxx::Serialization&)
A() = default;
explicit A(const iox::cxx::Serialization& serialized IOX_MAYBE_UNUSED)
{
}
// NOLINTNEXTLINE(hicpp-explicit-conversions) required by the Serialization API
operator iox::cxx::Serialization() const
{
return iox::cxx::Serialization("5:asdgg");
Expand Down