llvm-project
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
13using namespace lldb_private;14
15TEST(ConstStringTest, format_provider) {16EXPECT_EQ("foo", llvm::formatv("{0}", ConstString("foo")).str());17}
18
19TEST(ConstStringTest, MangledCounterpart) {20ConstString uvw("uvw");21ConstString counterpart;22EXPECT_FALSE(uvw.GetMangledCounterpart(counterpart));23EXPECT_EQ("", counterpart.GetStringRef());24
25ConstString xyz;26xyz.SetStringWithMangledCounterpart("xyz", uvw);27EXPECT_EQ("xyz", xyz.GetStringRef());28
29EXPECT_TRUE(xyz.GetMangledCounterpart(counterpart));30EXPECT_EQ("uvw", counterpart.GetStringRef());31
32EXPECT_TRUE(uvw.GetMangledCounterpart(counterpart));33EXPECT_EQ("xyz", counterpart.GetStringRef());34}
35
36TEST(ConstStringTest, UpdateMangledCounterpart) {37{ // Add counterpart38ConstString some1;39some1.SetStringWithMangledCounterpart("some", ConstString(""));40}41{ // Overwrite empty string42ConstString some2;43some2.SetStringWithMangledCounterpart("some", ConstString("one"));44}45{ // Overwrite with identical value46ConstString some2;47some2.SetStringWithMangledCounterpart("some", ConstString("one"));48}49{ // Check counterpart is set50ConstString counterpart;51EXPECT_TRUE(ConstString("some").GetMangledCounterpart(counterpart));52EXPECT_EQ("one", counterpart.GetStringRef());53}54}
55
56TEST(ConstStringTest, FromMidOfBufferStringRef) {57// StringRef's into bigger buffer: no null termination58const char *buffer = "abcdefghi";59llvm::StringRef foo_ref(buffer, 3);60llvm::StringRef bar_ref(buffer + 3, 3);61
62ConstString foo(foo_ref);63
64ConstString bar;65bar.SetStringWithMangledCounterpart(bar_ref, foo);66EXPECT_EQ("def", bar.GetStringRef());67
68ConstString counterpart;69EXPECT_TRUE(bar.GetMangledCounterpart(counterpart));70EXPECT_EQ("abc", counterpart.GetStringRef());71
72EXPECT_TRUE(foo.GetMangledCounterpart(counterpart));73EXPECT_EQ("def", counterpart.GetStringRef());74}
75
76TEST(ConstStringTest, NullAndEmptyStates) {77ConstString foo("foo");78EXPECT_FALSE(!foo);79EXPECT_FALSE(foo.IsEmpty());80EXPECT_FALSE(foo.IsNull());81
82ConstString empty("");83EXPECT_TRUE(!empty);84EXPECT_TRUE(empty.IsEmpty());85EXPECT_FALSE(empty.IsNull());86
87ConstString null;88EXPECT_TRUE(!null);89EXPECT_TRUE(null.IsEmpty());90EXPECT_TRUE(null.IsNull());91EXPECT_TRUE(null.GetString().empty());92}
93
94TEST(ConstStringTest, CompareConstString) {95ConstString foo("foo");96ConstString foo2("foo");97ConstString bar("bar");98
99EXPECT_TRUE(foo == foo2);100EXPECT_TRUE(foo2 == foo);101EXPECT_TRUE(foo == ConstString("foo"));102
103EXPECT_FALSE(foo == bar);104EXPECT_FALSE(foo2 == bar);105EXPECT_FALSE(foo == ConstString("bar"));106EXPECT_FALSE(foo == ConstString("different"));107EXPECT_FALSE(foo == ConstString(""));108EXPECT_FALSE(foo == ConstString());109
110ConstString empty("");111EXPECT_FALSE(empty == ConstString("bar"));112EXPECT_FALSE(empty == ConstString());113EXPECT_TRUE(empty == ConstString(""));114
115ConstString null;116EXPECT_FALSE(null == ConstString("bar"));117EXPECT_TRUE(null == ConstString());118EXPECT_FALSE(null == ConstString(""));119}
120
121TEST(ConstStringTest, CompareStringRef) {122ConstString foo("foo");123
124EXPECT_TRUE(foo == "foo");125EXPECT_TRUE(foo != "");126EXPECT_FALSE(foo == static_cast<const char *>(nullptr));127EXPECT_TRUE(foo != "bar");128
129ConstString empty("");130EXPECT_FALSE(empty == "foo");131EXPECT_FALSE(empty != "");132EXPECT_FALSE(empty == static_cast<const char *>(nullptr));133EXPECT_TRUE(empty != "bar");134
135ConstString null;136EXPECT_FALSE(null == "foo");137EXPECT_TRUE(null != "");138EXPECT_TRUE(null == static_cast<const char *>(nullptr));139EXPECT_TRUE(null != "bar");140}
141
142TEST(ConstStringTest, StringConversions) {143ConstString foo("foo");144
145// Member functions.146EXPECT_EQ(llvm::StringRef("foo"), foo.GetStringRef());147EXPECT_EQ(std::string("foo"), foo.GetString());148EXPECT_STREQ("foo", foo.AsCString());149
150// Conversion operators.151EXPECT_EQ(llvm::StringRef("foo"), llvm::StringRef(foo));152EXPECT_EQ(std::string("foo"), std::string_view(foo));153EXPECT_EQ(std::string("foo"), std::string(foo));154}
155