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

Added < and > operators to fixed_string. #3623

Merged
merged 3 commits into from
Jul 18, 2023
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
24 changes: 24 additions & 0 deletions include/fastrtps/utils/fixed_size_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ struct fixed_string
return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0;
}

template<size_t N> bool operator < (
const fixed_string<N>& rhs) const noexcept
{
return 0 > compare(rhs);
}

template<size_t N> bool operator > (
const fixed_string<N>& rhs) const noexcept
{
return 0 < compare(rhs);
}

bool operator < (
const std::string& rhs) const noexcept
{
return 0 > compare(rhs);
}

bool operator > (
const std::string& rhs) const noexcept
{
return 0 < compare(rhs);
}

operator const char* () const noexcept {
return c_str();
}
Expand Down
42 changes: 42 additions & 0 deletions test/unittest/utils/FixedSizeStringTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,48 @@ TEST_F(FixedSizeStringTests, comparisons)
ASSERT_GT(0, fixed_b.compare(fixed_large));
}

TEST_F(FixedSizeStringTests, less_than_operator_fixed_string)
{
fixed_string<MAX_CHARS> fixed_string_short = "test string";
fixed_string<MAX_CHARS> fixed_string_long = "test string long";

ASSERT_FALSE(fixed_string_short < fixed_string_short);
ASSERT_FALSE(fixed_string_long < fixed_string_long);
ASSERT_TRUE(fixed_string_short < fixed_string_long);
ASSERT_FALSE(fixed_string_long < fixed_string_short);
}

TEST_F(FixedSizeStringTests, less_than_operator_std_string)
{
fixed_string<MAX_CHARS> fixed_string_short = "test string";
std::string std_string_long = "test string long";
std::string std_string_short = "test";

ASSERT_TRUE(fixed_string_short < std_string_long);
ASSERT_FALSE(fixed_string_short < std_string_short);
}

TEST_F(FixedSizeStringTests, greater_than_operator_fixed_string)
{
fixed_string<MAX_CHARS> fixed_string_short = "test string";
fixed_string<MAX_CHARS> fixed_string_long = "test string long";

ASSERT_FALSE(fixed_string_short > fixed_string_short);
ASSERT_FALSE(fixed_string_long > fixed_string_long);
ASSERT_FALSE(fixed_string_short > fixed_string_long);
ASSERT_TRUE(fixed_string_long > fixed_string_short);
}

TEST_F(FixedSizeStringTests, greater_than_operator_std_string)
{
fixed_string<MAX_CHARS> fixed_string_short = "test string";
std::string std_string_long = "test string long";
std::string std_string_short = "test";

ASSERT_FALSE(fixed_string_short > std_string_long);
ASSERT_TRUE(fixed_string_short > std_string_short);
}

int main(
int argc,
char** argv)
Expand Down