llvm-project

Форк
0
/
ConstStringTest.cpp 
154 строки · 4.4 Кб
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("xyz", 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("some", ConstString(""));
40
  }
41
  { // Overwrite empty string
42
    ConstString some2;
43
    some2.SetStringWithMangledCounterpart("some", ConstString("one"));
44
  }
45
  { // Overwrite with identical value
46
    ConstString some2;
47
    some2.SetStringWithMangledCounterpart("some", 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(bar_ref, 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
  EXPECT_TRUE(null.GetString().empty());
92
}
93

94
TEST(ConstStringTest, CompareConstString) {
95
  ConstString foo("foo");
96
  ConstString foo2("foo");
97
  ConstString bar("bar");
98

99
  EXPECT_TRUE(foo == foo2);
100
  EXPECT_TRUE(foo2 == foo);
101
  EXPECT_TRUE(foo == ConstString("foo"));
102

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());
109

110
  ConstString empty("");
111
  EXPECT_FALSE(empty == ConstString("bar"));
112
  EXPECT_FALSE(empty == ConstString());
113
  EXPECT_TRUE(empty == ConstString(""));
114

115
  ConstString null;
116
  EXPECT_FALSE(null == ConstString("bar"));
117
  EXPECT_TRUE(null == ConstString());
118
  EXPECT_FALSE(null == ConstString(""));
119
}
120

121
TEST(ConstStringTest, CompareStringRef) {
122
  ConstString foo("foo");
123

124
  EXPECT_TRUE(foo == "foo");
125
  EXPECT_TRUE(foo != "");
126
  EXPECT_FALSE(foo == static_cast<const char *>(nullptr));
127
  EXPECT_TRUE(foo != "bar");
128

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");
134

135
  ConstString null;
136
  EXPECT_FALSE(null == "foo");
137
  EXPECT_TRUE(null != "");
138
  EXPECT_TRUE(null == static_cast<const char *>(nullptr));
139
  EXPECT_TRUE(null != "bar");
140
}
141

142
TEST(ConstStringTest, StringConversions) {
143
  ConstString foo("foo");
144

145
  // Member functions.
146
  EXPECT_EQ(llvm::StringRef("foo"), foo.GetStringRef());
147
  EXPECT_EQ(std::string("foo"), foo.GetString());
148
  EXPECT_STREQ("foo", foo.AsCString());
149

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));
154
}
155

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.