Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion sycl/include/sycl/detail/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class string {

const char *c_str() const noexcept { return str ? str : ""; }
const char *data() const noexcept { return c_str(); }
bool empty() { return str ? str[0] : false; }
bool empty() const noexcept { return str == nullptr || *str == '\0'; }

friend bool operator==(const string &lhs, std::string_view rhs) noexcept {
return rhs == lhs.c_str();
Expand Down
1 change: 1 addition & 0 deletions sycl/unittests/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_sycl_unittest(MiscTests SHARED
CircularBuffer.cpp
OsUtils.cpp
PropertyUtils.cpp
SYCLString.cpp
)
endif()
add_subdirectory(LinkGraph)
89 changes: 89 additions & 0 deletions sycl/unittests/misc/SYCLString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <gtest/gtest.h>
#include <sycl/sycl.hpp>

class SYCLDetailStringTest : public ::testing::Test {};

TEST_F(SYCLDetailStringTest, DefaultConstructor) {
sycl::detail::string empty_s;
EXPECT_TRUE(empty_s.empty());
EXPECT_STREQ(empty_s.c_str(), "");
}

TEST_F(SYCLDetailStringTest, StringViewConstructor) {
std::string_view sv = "Hello, World!";
sycl::detail::string s1(sv);
EXPECT_STREQ(s1.c_str(), "Hello, World!");
}

TEST_F(SYCLDetailStringTest, CopyConstructor) {
sycl::detail::string s1("Hello, World!");
sycl::detail::string s2(s1);
EXPECT_STREQ(s2.c_str(), "Hello, World!");

// Check for deep copy: modifying the original should not affect the copy.
s1 = "Changed";
EXPECT_STREQ(s2.c_str(), "Hello, World!");
}

TEST_F(SYCLDetailStringTest, MoveConstructor) {
sycl::detail::string s1("Changed");
sycl::detail::string s2(std::move(s1));
EXPECT_STREQ(s2.c_str(), "Changed");
}

TEST_F(SYCLDetailStringTest, StringViewAssignment) {
sycl::detail::string s;
s = "New String";
EXPECT_STREQ(s.c_str(), "New String");
}

TEST_F(SYCLDetailStringTest, CopyAssignment) {
sycl::detail::string s1("Hello, World!");
sycl::detail::string s2;
s2 = s1;
EXPECT_STREQ(s2.c_str(), "Hello, World!");

// Check for deep copy.
s1 = "Changed";
EXPECT_STREQ(s2.c_str(), "Hello, World!");
}

TEST_F(SYCLDetailStringTest, MoveAssignment) {
sycl::detail::string s1("Changed");
sycl::detail::string s2;
s2 = std::move(s1);
EXPECT_STREQ(s2.c_str(), "Changed");
}

TEST_F(SYCLDetailStringTest, Methods) {
sycl::detail::string s_not_empty("not empty");
sycl::detail::string s_empty;
// Test c_str() and data().
EXPECT_STREQ(s_not_empty.data(), "not empty");
EXPECT_STREQ(s_not_empty.c_str(), "not empty");
EXPECT_EQ(s_not_empty.data(), s_not_empty.c_str());

// Test empty(). Like really.
EXPECT_FALSE(s_not_empty.empty());
EXPECT_TRUE(sycl::detail::string("").empty());
EXPECT_TRUE(sycl::detail::string().empty());
EXPECT_TRUE(s_empty.empty());
}

TEST_F(SYCLDetailStringTest, Swap) {
sycl::detail::string s1("first");
sycl::detail::string s2("second");
swap(s1, s2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to your PR, but I don't get why we define swap instead of a std::swap overload.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(dunno) emoji

EXPECT_STREQ(s1.c_str(), "second");
EXPECT_STREQ(s2.c_str(), "first");
}

TEST_F(SYCLDetailStringTest, ComparisonOperators) {
sycl::detail::string s("match");
std::string_view sv_match("match");
std::string_view sv_no_match("no match");

EXPECT_EQ(s, sv_match);
EXPECT_EQ(sv_match, s);
EXPECT_FALSE(s == sv_no_match);
}
Loading