]>
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("fooGARBAGE", 3);
38 static_assert( 3 == defString
.length(), "def right size");
39 EXPECT_EQ(0, defString
.to_string().compare(0, defString
.length(), defString
.data(), 3));
41 APT::StringView strString
{std::string
{"foo"}};
42 EXPECT_EQ(3, strString
.length());
43 EXPECT_EQ(0, strString
.to_string().compare(0, strString
.length(), strString
.data(), 3));
45 constexpr char const * const charp
= "fooGARBAGE";
46 constexpr APT::StringView charpString
{charp
, 3};
47 EXPECT_EQ(3, charpString
.length());
48 EXPECT_EQ(0, charpString
.to_string().compare(0, charpString
.length(), charpString
.data(), 3));
50 char * charp2
= strdup("foo");
51 APT::StringView charp2String
{charp2
};
52 EXPECT_EQ(3, charp2String
.length());
53 EXPECT_EQ(0, charp2String
.to_string().compare(0, charp2String
.length(), charp2String
.data(), 3));
56 const APT::StringView charaString
{"foo"};
57 EXPECT_EQ(3, charaString
.length());
58 EXPECT_EQ(0, charaString
.to_string().compare(0, charaString
.length(), charaString
.data(), 3));
60 EXPECT_TRUE(APT::StringView("foo") == "foo");
61 EXPECT_FALSE(APT::StringView("foo") != "foo");
64 TEST(StringViewTest
,SubStr
)
66 const APT::StringView
defString("Hello World!");
67 EXPECT_EQ(defString
.to_string().substr(6), defString
.substr(6).to_string());
68 EXPECT_EQ(defString
.to_string().substr(0,5), defString
.substr(0,5).to_string());
69 EXPECT_EQ(defString
.to_string().substr(6,5), defString
.substr(6,5).to_string());
72 TEST(StringViewTest
,Find
)
74 const APT::StringView
defString("Hello World!");
75 EXPECT_EQ(defString
.to_string().find('l'), defString
.find('l'));
76 EXPECT_EQ(defString
.to_string().find('X'), defString
.find('X'));
77 EXPECT_EQ(defString
.to_string().find('e',3), defString
.find('e',3));
78 EXPECT_EQ(defString
.to_string().find('l',6), defString
.find('l',6));
79 EXPECT_EQ(defString
.to_string().find('l',11), defString
.find('l',11));
82 TEST(StringViewTest
,RFind
)
84 const APT::StringView
defString("Hello World!");
85 EXPECT_EQ(defString
.to_string().rfind('l'), defString
.rfind('l'));
86 EXPECT_EQ(defString
.to_string().rfind('X'), defString
.rfind('X'));
87 EXPECT_EQ(defString
.to_string().rfind('e',3), defString
.rfind('e',3));
88 EXPECT_EQ(defString
.to_string().rfind('l',6), defString
.rfind('l',6));
89 EXPECT_EQ(defString
.to_string().rfind('l',11), defString
.rfind('l',11));