Skip to content

Commit bb6e44c

Browse files
[SYCL] Fix for sycl::detail::string and tests (#20076)
how have we made it this far?
1 parent 3ee3f1f commit bb6e44c

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

sycl/include/sycl/detail/string.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class string {
5858

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

6363
friend bool operator==(const string &lhs, std::string_view rhs) noexcept {
6464
return rhs == lhs.c_str();

sycl/unittests/misc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_sycl_unittest(MiscTests SHARED
66
CircularBuffer.cpp
77
OsUtils.cpp
88
PropertyUtils.cpp
9+
DetailString.cpp
910
)
1011
endif()
1112
add_subdirectory(LinkGraph)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <gtest/gtest.h>
2+
#include <sycl/detail/string.hpp>
3+
4+
class SYCLDetailStringTest : public ::testing::Test {};
5+
6+
TEST_F(SYCLDetailStringTest, DefaultConstructor) {
7+
sycl::detail::string empty_s;
8+
EXPECT_TRUE(empty_s.empty());
9+
EXPECT_STREQ(empty_s.c_str(), "");
10+
}
11+
12+
TEST_F(SYCLDetailStringTest, StringViewConstructor) {
13+
std::string_view sv = "Hello, World!";
14+
sycl::detail::string s1(sv);
15+
EXPECT_STREQ(s1.c_str(), "Hello, World!");
16+
}
17+
18+
TEST_F(SYCLDetailStringTest, CopyConstructor) {
19+
sycl::detail::string s1("Hello, World!");
20+
sycl::detail::string s2(s1);
21+
EXPECT_STREQ(s2.c_str(), "Hello, World!");
22+
23+
// Check for deep copy: modifying the original should not affect the copy.
24+
s1 = "Changed";
25+
EXPECT_STREQ(s2.c_str(), "Hello, World!");
26+
}
27+
28+
TEST_F(SYCLDetailStringTest, MoveConstructor) {
29+
sycl::detail::string s1("Changed");
30+
sycl::detail::string s2(std::move(s1));
31+
EXPECT_STREQ(s2.c_str(), "Changed");
32+
}
33+
34+
TEST_F(SYCLDetailStringTest, StringViewAssignment) {
35+
sycl::detail::string s;
36+
s = "New String";
37+
EXPECT_STREQ(s.c_str(), "New String");
38+
std::string_view sv = "From String View";
39+
s = sv;
40+
EXPECT_STREQ(s.c_str(), "From String View");
41+
}
42+
43+
TEST_F(SYCLDetailStringTest, CopyAssignment) {
44+
sycl::detail::string s1("Hello, World!");
45+
sycl::detail::string s2;
46+
s2 = s1;
47+
EXPECT_STREQ(s2.c_str(), "Hello, World!");
48+
49+
// Check for deep copy.
50+
s1 = "Changed";
51+
EXPECT_STREQ(s2.c_str(), "Hello, World!");
52+
}
53+
54+
TEST_F(SYCLDetailStringTest, MoveAssignment) {
55+
sycl::detail::string s1("Changed");
56+
sycl::detail::string s2;
57+
s2 = std::move(s1);
58+
EXPECT_STREQ(s2.c_str(), "Changed");
59+
}
60+
61+
TEST_F(SYCLDetailStringTest, Methods) {
62+
sycl::detail::string s_not_empty("not empty");
63+
sycl::detail::string s_empty;
64+
// Test c_str() and data().
65+
EXPECT_STREQ(s_not_empty.data(), "not empty");
66+
EXPECT_STREQ(s_not_empty.c_str(), "not empty");
67+
EXPECT_EQ(s_not_empty.data(), s_not_empty.c_str());
68+
69+
// Test empty(). Like really.
70+
EXPECT_FALSE(s_not_empty.empty());
71+
EXPECT_TRUE(sycl::detail::string("").empty());
72+
EXPECT_TRUE(sycl::detail::string().empty());
73+
EXPECT_TRUE(s_empty.empty());
74+
}
75+
76+
TEST_F(SYCLDetailStringTest, Swap) {
77+
sycl::detail::string s1("first");
78+
sycl::detail::string s2("second");
79+
swap(s1, s2);
80+
EXPECT_STREQ(s1.c_str(), "second");
81+
EXPECT_STREQ(s2.c_str(), "first");
82+
}
83+
84+
TEST_F(SYCLDetailStringTest, ComparisonOperators) {
85+
sycl::detail::string s("match");
86+
std::string_view sv_match("match");
87+
std::string_view sv_no_match("no match");
88+
89+
EXPECT_EQ(s, sv_match);
90+
EXPECT_EQ(sv_match, s);
91+
EXPECT_FALSE(s == sv_no_match);
92+
}

0 commit comments

Comments
 (0)