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 "llvm/Support/YAMLParser.h"
12 #include "gtest/gtest.h"
14 using namespace lldb_private
;
16 TEST(ConstStringTest
, format_provider
) {
17 EXPECT_EQ("foo", llvm::formatv("{0}", ConstString("foo")).str());
20 TEST(ConstStringTest
, MangledCounterpart
) {
21 ConstString
uvw("uvw");
22 ConstString counterpart
;
23 EXPECT_FALSE(uvw
.GetMangledCounterpart(counterpart
));
24 EXPECT_EQ("", counterpart
.GetStringRef());
27 xyz
.SetStringWithMangledCounterpart("xyz", uvw
);
28 EXPECT_EQ("xyz", xyz
.GetStringRef());
30 EXPECT_TRUE(xyz
.GetMangledCounterpart(counterpart
));
31 EXPECT_EQ("uvw", counterpart
.GetStringRef());
33 EXPECT_TRUE(uvw
.GetMangledCounterpart(counterpart
));
34 EXPECT_EQ("xyz", counterpart
.GetStringRef());
37 TEST(ConstStringTest
, UpdateMangledCounterpart
) {
40 some1
.SetStringWithMangledCounterpart("some", ConstString(""));
42 { // Overwrite empty string
44 some2
.SetStringWithMangledCounterpart("some", ConstString("one"));
46 { // Overwrite with identical value
48 some2
.SetStringWithMangledCounterpart("some", ConstString("one"));
50 { // Check counterpart is set
51 ConstString counterpart
;
52 EXPECT_TRUE(ConstString("some").GetMangledCounterpart(counterpart
));
53 EXPECT_EQ("one", counterpart
.GetStringRef());
57 TEST(ConstStringTest
, FromMidOfBufferStringRef
) {
58 // StringRef's into bigger buffer: no null termination
59 const char *buffer
= "abcdefghi";
60 llvm::StringRef
foo_ref(buffer
, 3);
61 llvm::StringRef
bar_ref(buffer
+ 3, 3);
63 ConstString
foo(foo_ref
);
66 bar
.SetStringWithMangledCounterpart(bar_ref
, foo
);
67 EXPECT_EQ("def", bar
.GetStringRef());
69 ConstString counterpart
;
70 EXPECT_TRUE(bar
.GetMangledCounterpart(counterpart
));
71 EXPECT_EQ("abc", counterpart
.GetStringRef());
73 EXPECT_TRUE(foo
.GetMangledCounterpart(counterpart
));
74 EXPECT_EQ("def", counterpart
.GetStringRef());
77 TEST(ConstStringTest
, NullAndEmptyStates
) {
78 ConstString
foo("foo");
80 EXPECT_FALSE(foo
.IsEmpty());
81 EXPECT_FALSE(foo
.IsNull());
83 ConstString
empty("");
85 EXPECT_TRUE(empty
.IsEmpty());
86 EXPECT_FALSE(empty
.IsNull());
90 EXPECT_TRUE(null
.IsEmpty());
91 EXPECT_TRUE(null
.IsNull());
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
, YAML
) {
144 llvm::raw_string_ostream
os(buffer
);
147 std::vector
<ConstString
> strings
= {ConstString("foo"), ConstString("bar"),
149 llvm::yaml::Output
yout(os
);
154 std::vector
<ConstString
> deserialized
;
155 llvm::yaml::Input
yin(buffer
);
158 EXPECT_EQ(strings
, deserialized
);