1 | //===-- ConstStringTest.cpp -----------------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Utility/ConstString.h" |
10 | #include "llvm/Support/FormatVariadic.h" |
11 | #include "gtest/gtest.h" |
12 | |
13 | using namespace lldb_private; |
14 | |
15 | TEST(ConstStringTest, format_provider) { |
16 | EXPECT_EQ("foo" , llvm::formatv("{0}" , ConstString("foo" )).str()); |
17 | } |
18 | |
19 | TEST(ConstStringTest, MangledCounterpart) { |
20 | ConstString uvw("uvw" ); |
21 | ConstString counterpart; |
22 | EXPECT_FALSE(uvw.GetMangledCounterpart(counterpart)); |
23 | EXPECT_EQ("" , counterpart.GetStringRef()); |
24 | |
25 | ConstString xyz; |
26 | xyz.SetStringWithMangledCounterpart(demangled: "xyz" , mangled: uvw); |
27 | EXPECT_EQ("xyz" , xyz.GetStringRef()); |
28 | |
29 | EXPECT_TRUE(xyz.GetMangledCounterpart(counterpart)); |
30 | EXPECT_EQ("uvw" , counterpart.GetStringRef()); |
31 | |
32 | EXPECT_TRUE(uvw.GetMangledCounterpart(counterpart)); |
33 | EXPECT_EQ("xyz" , counterpart.GetStringRef()); |
34 | } |
35 | |
36 | TEST(ConstStringTest, UpdateMangledCounterpart) { |
37 | { // Add counterpart |
38 | ConstString some1; |
39 | some1.SetStringWithMangledCounterpart(demangled: "some" , mangled: ConstString("" )); |
40 | } |
41 | { // Overwrite empty string |
42 | ConstString some2; |
43 | some2.SetStringWithMangledCounterpart(demangled: "some" , mangled: ConstString("one" )); |
44 | } |
45 | { // Overwrite with identical value |
46 | ConstString some2; |
47 | some2.SetStringWithMangledCounterpart(demangled: "some" , mangled: ConstString("one" )); |
48 | } |
49 | { // Check counterpart is set |
50 | ConstString counterpart; |
51 | EXPECT_TRUE(ConstString("some" ).GetMangledCounterpart(counterpart)); |
52 | EXPECT_EQ("one" , counterpart.GetStringRef()); |
53 | } |
54 | } |
55 | |
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); |
61 | |
62 | ConstString foo(foo_ref); |
63 | |
64 | ConstString bar; |
65 | bar.SetStringWithMangledCounterpart(demangled: bar_ref, mangled: foo); |
66 | EXPECT_EQ("def" , bar.GetStringRef()); |
67 | |
68 | ConstString counterpart; |
69 | EXPECT_TRUE(bar.GetMangledCounterpart(counterpart)); |
70 | EXPECT_EQ("abc" , counterpart.GetStringRef()); |
71 | |
72 | EXPECT_TRUE(foo.GetMangledCounterpart(counterpart)); |
73 | EXPECT_EQ("def" , counterpart.GetStringRef()); |
74 | } |
75 | |
76 | TEST(ConstStringTest, NullAndEmptyStates) { |
77 | ConstString foo("foo" ); |
78 | EXPECT_FALSE(!foo); |
79 | EXPECT_FALSE(foo.IsEmpty()); |
80 | EXPECT_FALSE(foo.IsNull()); |
81 | |
82 | ConstString empty("" ); |
83 | EXPECT_TRUE(!empty); |
84 | EXPECT_TRUE(empty.IsEmpty()); |
85 | EXPECT_FALSE(empty.IsNull()); |
86 | |
87 | ConstString null; |
88 | EXPECT_TRUE(!null); |
89 | EXPECT_TRUE(null.IsEmpty()); |
90 | EXPECT_TRUE(null.IsNull()); |
91 | } |
92 | |
93 | TEST(ConstStringTest, CompareConstString) { |
94 | ConstString foo("foo" ); |
95 | ConstString foo2("foo" ); |
96 | ConstString bar("bar" ); |
97 | |
98 | EXPECT_TRUE(foo == foo2); |
99 | EXPECT_TRUE(foo2 == foo); |
100 | EXPECT_TRUE(foo == ConstString("foo" )); |
101 | |
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()); |
108 | |
109 | ConstString empty("" ); |
110 | EXPECT_FALSE(empty == ConstString("bar" )); |
111 | EXPECT_FALSE(empty == ConstString()); |
112 | EXPECT_TRUE(empty == ConstString("" )); |
113 | |
114 | ConstString null; |
115 | EXPECT_FALSE(null == ConstString("bar" )); |
116 | EXPECT_TRUE(null == ConstString()); |
117 | EXPECT_FALSE(null == ConstString("" )); |
118 | } |
119 | |
120 | TEST(ConstStringTest, CompareStringRef) { |
121 | ConstString foo("foo" ); |
122 | |
123 | EXPECT_TRUE(foo == "foo" ); |
124 | EXPECT_TRUE(foo != "" ); |
125 | EXPECT_FALSE(foo == static_cast<const char *>(nullptr)); |
126 | EXPECT_TRUE(foo != "bar" ); |
127 | |
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" ); |
133 | |
134 | ConstString null; |
135 | EXPECT_FALSE(null == "foo" ); |
136 | EXPECT_TRUE(null != "" ); |
137 | EXPECT_TRUE(null == static_cast<const char *>(nullptr)); |
138 | EXPECT_TRUE(null != "bar" ); |
139 | } |
140 | |
141 | TEST(ConstStringTest, StringConversions) { |
142 | ConstString foo("foo" ); |
143 | |
144 | // Member functions. |
145 | EXPECT_EQ(llvm::StringRef("foo" ), foo.GetStringRef()); |
146 | EXPECT_EQ(std::string("foo" ), foo.GetString()); |
147 | EXPECT_STREQ("foo" , foo.AsCString()); |
148 | |
149 | // Conversion operators. |
150 | EXPECT_EQ(llvm::StringRef("foo" ), llvm::StringRef(foo)); |
151 | EXPECT_EQ(std::string("foo" ), std::string_view(foo)); |
152 | EXPECT_EQ(std::string("foo" ), std::string(foo)); |
153 | } |
154 | |