1 //===-- ConstStringTest.cpp -----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/ConstString.h"
10 #include "llvm/Support/FormatVariadic.h"
11 #include "gtest/gtest.h"
13 using namespace lldb_private
;
15 TEST(ConstStringTest
, format_provider
) {
16 EXPECT_EQ("foo", llvm::formatv("{0}", ConstString("foo")).str());
19 TEST(ConstStringTest
, MangledCounterpart
) {
20 ConstString
uvw("uvw");
21 ConstString counterpart
;
22 EXPECT_FALSE(uvw
.GetMangledCounterpart(counterpart
));
23 EXPECT_EQ("", counterpart
.GetStringRef());
26 xyz
.SetStringWithMangledCounterpart("xyz", uvw
);
27 EXPECT_EQ("xyz", xyz
.GetStringRef());
29 EXPECT_TRUE(xyz
.GetMangledCounterpart(counterpart
));
30 EXPECT_EQ("uvw", counterpart
.GetStringRef());
32 EXPECT_TRUE(uvw
.GetMangledCounterpart(counterpart
));
33 EXPECT_EQ("xyz", counterpart
.GetStringRef());
36 TEST(ConstStringTest
, UpdateMangledCounterpart
) {
39 some1
.SetStringWithMangledCounterpart("some", ConstString(""));
41 { // Overwrite empty string
43 some2
.SetStringWithMangledCounterpart("some", ConstString("one"));
45 { // Overwrite with identical value
47 some2
.SetStringWithMangledCounterpart("some", ConstString("one"));
49 { // Check counterpart is set
50 ConstString counterpart
;
51 EXPECT_TRUE(ConstString("some").GetMangledCounterpart(counterpart
));
52 EXPECT_EQ("one", counterpart
.GetStringRef());
56 TEST(ConstStringTest
, FromMidOfBufferStringRef
) {
57 // StringRef's into bigger buffer: no null termination
58 const char *buffer
= "abcdefghi";
59 llvm::StringRef
foo_ref(buffer
, 3);
60 llvm::StringRef
bar_ref(buffer
+ 3, 3);
62 ConstString
foo(foo_ref
);
65 bar
.SetStringWithMangledCounterpart(bar_ref
, foo
);
66 EXPECT_EQ("def", bar
.GetStringRef());
68 ConstString counterpart
;
69 EXPECT_TRUE(bar
.GetMangledCounterpart(counterpart
));
70 EXPECT_EQ("abc", counterpart
.GetStringRef());
72 EXPECT_TRUE(foo
.GetMangledCounterpart(counterpart
));
73 EXPECT_EQ("def", counterpart
.GetStringRef());
76 TEST(ConstStringTest
, NullAndEmptyStates
) {
77 ConstString
foo("foo");
79 EXPECT_FALSE(foo
.IsEmpty());
80 EXPECT_FALSE(foo
.IsNull());
82 ConstString
empty("");
84 EXPECT_TRUE(empty
.IsEmpty());
85 EXPECT_FALSE(empty
.IsNull());
89 EXPECT_TRUE(null
.IsEmpty());
90 EXPECT_TRUE(null
.IsNull());
91 EXPECT_TRUE(null
.GetString().empty());
94 TEST(ConstStringTest
, CompareConstString
) {
95 ConstString
foo("foo");
96 ConstString
foo2("foo");
97 ConstString
bar("bar");
99 EXPECT_TRUE(foo
== foo2
);
100 EXPECT_TRUE(foo2
== foo
);
101 EXPECT_TRUE(foo
== ConstString("foo"));
103 EXPECT_FALSE(foo
== bar
);
104 EXPECT_FALSE(foo2
== bar
);
105 EXPECT_FALSE(foo
== ConstString("bar"));
106 EXPECT_FALSE(foo
== ConstString("different"));
107 EXPECT_FALSE(foo
== ConstString(""));
108 EXPECT_FALSE(foo
== ConstString());
110 ConstString
empty("");
111 EXPECT_FALSE(empty
== ConstString("bar"));
112 EXPECT_FALSE(empty
== ConstString());
113 EXPECT_TRUE(empty
== ConstString(""));
116 EXPECT_FALSE(null
== ConstString("bar"));
117 EXPECT_TRUE(null
== ConstString());
118 EXPECT_FALSE(null
== ConstString(""));
121 TEST(ConstStringTest
, CompareStringRef
) {
122 ConstString
foo("foo");
124 EXPECT_TRUE(foo
== "foo");
125 EXPECT_TRUE(foo
!= "");
126 EXPECT_FALSE(foo
== static_cast<const char *>(nullptr));
127 EXPECT_TRUE(foo
!= "bar");
129 ConstString
empty("");
130 EXPECT_FALSE(empty
== "foo");
131 EXPECT_FALSE(empty
!= "");
132 EXPECT_FALSE(empty
== static_cast<const char *>(nullptr));
133 EXPECT_TRUE(empty
!= "bar");
136 EXPECT_FALSE(null
== "foo");
137 EXPECT_TRUE(null
!= "");
138 EXPECT_TRUE(null
== static_cast<const char *>(nullptr));
139 EXPECT_TRUE(null
!= "bar");
142 TEST(ConstStringTest
, StringConversions
) {
143 ConstString
foo("foo");
146 EXPECT_EQ(llvm::StringRef("foo"), foo
.GetStringRef());
147 EXPECT_EQ(std::string("foo"), foo
.GetString());
148 EXPECT_STREQ("foo", foo
.AsCString());
150 // Conversion operators.
151 EXPECT_EQ(llvm::StringRef("foo"), llvm::StringRef(foo
));
152 EXPECT_EQ(std::string("foo"), std::string_view(foo
));
153 EXPECT_EQ(std::string("foo"), std::string(foo
));