BLI: Clarify and test StringRefNull comparisons
Add some comments that clarify that `StringRefNull` can be compared with other `StringRefNull` and with `StringRef` instances. Add unit tests that cover these cases. Pull Request: https://projects.blender.org/blender/blender/pulls/118515
This commit is contained in:
@@ -105,6 +105,8 @@ class StringRefBase {
|
||||
|
||||
/**
|
||||
* References a null-terminated const char array.
|
||||
*
|
||||
* StringRefNull can be compared with StringRef and StringRefNull.
|
||||
*/
|
||||
class StringRefNull : public StringRefBase {
|
||||
|
||||
@@ -120,6 +122,8 @@ class StringRefNull : public StringRefBase {
|
||||
|
||||
/**
|
||||
* References a const char array. It might not be null terminated.
|
||||
*
|
||||
* StringRef can be compared with StringRef and StringRefNull.
|
||||
*/
|
||||
class StringRef : public StringRefBase {
|
||||
public:
|
||||
@@ -588,7 +592,10 @@ inline std::string operator+(StringRef a, StringRef b)
|
||||
/* This does not compare StringRef and std::string_view, because of ambiguous overloads. This is
|
||||
* not a problem when std::string_view is only used at api boundaries. To compare a StringRef and a
|
||||
* std::string_view, one should convert the std::string_view to StringRef (which is very cheap).
|
||||
* Ideally, we only use StringRef in our code to avoid this problem altogether. */
|
||||
* Ideally, we only use StringRef in our code to avoid this problem altogether.
|
||||
*
|
||||
* NOTE: these functions are also suitable for StringRefNull comparisons, as these are
|
||||
* implicitly converted to StringRef by the compiler. */
|
||||
constexpr bool operator==(StringRef a, StringRef b)
|
||||
{
|
||||
return std::string_view(a) == std::string_view(b);
|
||||
|
||||
@@ -34,6 +34,71 @@ TEST(string_ref_null, CStringLengthConstructor)
|
||||
EXPECT_EQ(ref.data(), str);
|
||||
}
|
||||
|
||||
TEST(string_ref_null, CompareEqual)
|
||||
{
|
||||
StringRefNull ref1("test");
|
||||
StringRefNull ref2("test");
|
||||
StringRefNull ref3("other");
|
||||
EXPECT_TRUE(ref1 == ref2);
|
||||
EXPECT_FALSE(ref1 == ref3);
|
||||
EXPECT_TRUE(ref1 != ref3);
|
||||
EXPECT_FALSE(ref1 != ref2);
|
||||
}
|
||||
|
||||
TEST(string_ref_null, CompareEqualCharPtr1)
|
||||
{
|
||||
StringRefNull ref("test");
|
||||
EXPECT_TRUE(ref == "test");
|
||||
EXPECT_FALSE(ref == "other");
|
||||
EXPECT_TRUE(ref != "other");
|
||||
EXPECT_FALSE(ref != "test");
|
||||
}
|
||||
|
||||
TEST(string_ref_null, CompareEqualCharPtr2)
|
||||
{
|
||||
StringRefNull ref("test");
|
||||
EXPECT_TRUE("test" == ref);
|
||||
EXPECT_FALSE("other" == ref);
|
||||
EXPECT_TRUE(ref != "other");
|
||||
EXPECT_FALSE(ref != "test");
|
||||
}
|
||||
|
||||
TEST(string_ref_null, CompareEqualString1)
|
||||
{
|
||||
StringRefNull ref("test");
|
||||
EXPECT_TRUE(ref == std::string("test"));
|
||||
EXPECT_FALSE(ref == std::string("other"));
|
||||
EXPECT_TRUE(ref != std::string("other"));
|
||||
EXPECT_FALSE(ref != std::string("test"));
|
||||
}
|
||||
|
||||
TEST(string_ref_null, CompareEqualString2)
|
||||
{
|
||||
StringRefNull ref("test");
|
||||
EXPECT_TRUE(std::string("test") == ref);
|
||||
EXPECT_FALSE(std::string("other") == ref);
|
||||
EXPECT_TRUE(std::string("other") != ref);
|
||||
EXPECT_FALSE(std::string("test") != ref);
|
||||
}
|
||||
|
||||
TEST(string_ref_null, CompareEqualStringRef1)
|
||||
{
|
||||
StringRefNull ref("test");
|
||||
EXPECT_TRUE(ref == StringRef("test"));
|
||||
EXPECT_FALSE(ref == StringRef("other"));
|
||||
EXPECT_TRUE(ref != StringRef("other"));
|
||||
EXPECT_FALSE(ref != StringRef("test"));
|
||||
}
|
||||
|
||||
TEST(string_ref_null, CompareEqualStringRef2)
|
||||
{
|
||||
StringRefNull ref("test");
|
||||
EXPECT_TRUE(StringRef("test") == ref);
|
||||
EXPECT_FALSE(StringRef("other") == ref);
|
||||
EXPECT_TRUE(StringRef("other") != ref);
|
||||
EXPECT_FALSE(StringRef("test") != ref);
|
||||
}
|
||||
|
||||
TEST(string_ref, DefaultConstructor)
|
||||
{
|
||||
StringRef ref;
|
||||
|
||||
Reference in New Issue
Block a user