]>
git.saurik.com Git - apt.git/blob - test/libapt/stringview_test.cc
1 #if !(defined APT_PKG_EXPOSE_STRING_VIEW)
2 #define APT_PKG_EXPOSE_STRING_VIEW
6 #include <apt-pkg/string_view.h>
11 #include <gtest/gtest.h>
13 TEST(StringViewTest
,EmptyString
)
15 constexpr APT::StringView defString
;
16 static_assert( 0 == defString
.length(), "def right size");
18 APT::StringView strString
{std::string
{}};
19 EXPECT_EQ(0, strString
.length());
21 constexpr char const * const charp
= "";
22 constexpr APT::StringView charpString
{charp
, 0};
23 static_assert( 0 == charpString
.length(), "charp right size");
25 APT::StringView charp2String
{charp
};
26 EXPECT_EQ(0, strString
.length());
28 const APT::StringView charaString
{""};
29 EXPECT_EQ(0, charaString
.length());
31 EXPECT_TRUE(APT::StringView("") == "");
32 EXPECT_FALSE(APT::StringView("") != "");
35 TEST(StringViewTest
,FooString
)
37 constexpr APT::StringView
defString("foo", 3);
38 static_assert( 3 == defString
.length(), "def right size");
39 EXPECT_EQ(defString
.to_string(), defString
.data());
41 APT::StringView strString
{std::string
{"foo"}};
42 EXPECT_EQ(3, strString
.length());
43 EXPECT_EQ(strString
.to_string(), strString
.data());
45 constexpr char const * const charp
= "foo";
46 constexpr APT::StringView charpString
{charp
, 3};
47 EXPECT_EQ( 3, charpString
.length());
48 EXPECT_EQ(charpString
.to_string(), charpString
.data());
50 APT::StringView charp2String
{charp
};
51 EXPECT_EQ(3, charp2String
.length());
52 EXPECT_EQ(charp2String
.to_string(), charp2String
.data());
54 const APT::StringView charaString
{"foo"};
55 EXPECT_EQ(3, charaString
.length());
56 EXPECT_EQ(charaString
.to_string(), charaString
.data());
58 EXPECT_TRUE(APT::StringView("foo") == "foo");
59 EXPECT_FALSE(APT::StringView("foo") != "foo");
62 TEST(StringViewTest
,SubStr
)
64 const APT::StringView
defString("Hello World!");
65 EXPECT_EQ(defString
.to_string().substr(6), defString
.substr(6).to_string());
66 EXPECT_EQ(defString
.to_string().substr(0,5), defString
.substr(0,5).to_string());
67 EXPECT_EQ(defString
.to_string().substr(6,5), defString
.substr(6,5).to_string());
70 TEST(StringViewTest
,Find
)
72 const APT::StringView
defString("Hello World!");
73 EXPECT_EQ(defString
.to_string().find('l'), defString
.find('l'));
74 EXPECT_EQ(defString
.to_string().find('X'), defString
.find('X'));
75 EXPECT_EQ(defString
.to_string().find('e',3), defString
.find('e',3));
76 EXPECT_EQ(defString
.to_string().find('l',6), defString
.find('l',6));
77 EXPECT_EQ(defString
.to_string().find('l',11), defString
.find('l',11));
80 TEST(StringViewTest
,RFind
)
82 const APT::StringView
defString("Hello World!");
83 EXPECT_EQ(defString
.to_string().rfind('l'), defString
.rfind('l'));
84 EXPECT_EQ(defString
.to_string().rfind('X'), defString
.rfind('X'));
85 EXPECT_EQ(defString
.to_string().rfind('e',3), defString
.rfind('e',3));
86 EXPECT_EQ(defString
.to_string().rfind('l',6), defString
.rfind('l',6));
87 EXPECT_EQ(defString
.to_string().rfind('l',11), defString
.rfind('l',11));