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());
93 TEST(ConstStringTest
, CompareConstString
) {
94 ConstString
foo("foo");
95 ConstString
foo2("foo");
96 ConstString
bar("bar");
98 EXPECT_TRUE(foo
== foo2
);
99 EXPECT_TRUE(foo2
== foo
);
100 EXPECT_TRUE(foo
== ConstString("foo"));
102 EXPECT_FALSE(foo
== bar
);
103 EXPECT_FALSE(foo2
== bar
);
104 EXPECT_FALSE(foo
== ConstString("bar"));
105 EXPECT_FALSE(foo
== ConstString("different"));
106 EXPECT_FALSE(foo
== ConstString(""));
107 EXPECT_FALSE(foo
== ConstString());
109 ConstString
empty("");
110 EXPECT_FALSE(empty
== ConstString("bar"));
111 EXPECT_FALSE(empty
== ConstString());
112 EXPECT_TRUE(empty
== ConstString(""));
115 EXPECT_FALSE(null
== ConstString("bar"));
116 EXPECT_TRUE(null
== ConstString());
117 EXPECT_FALSE(null
== ConstString(""));
120 TEST(ConstStringTest
, CompareStringRef
) {
121 ConstString
foo("foo");
123 EXPECT_TRUE(foo
== "foo");
124 EXPECT_TRUE(foo
!= "");
125 EXPECT_FALSE(foo
== static_cast<const char *>(nullptr));
126 EXPECT_TRUE(foo
!= "bar");
128 ConstString
empty("");
129 EXPECT_FALSE(empty
== "foo");
130 EXPECT_FALSE(empty
!= "");
131 EXPECT_FALSE(empty
== static_cast<const char *>(nullptr));
132 EXPECT_TRUE(empty
!= "bar");
135 EXPECT_FALSE(null
== "foo");
136 EXPECT_TRUE(null
!= "");
137 EXPECT_TRUE(null
== static_cast<const char *>(nullptr));
138 EXPECT_TRUE(null
!= "bar");