]>
Commit | Line | Data |
---|---|---|
1 | #if !(defined APT_PKG_EXPOSE_STRING_VIEW) | |
2 | #define APT_PKG_EXPOSE_STRING_VIEW | |
3 | #endif | |
4 | ||
5 | #include <config.h> | |
6 | #include <apt-pkg/string_view.h> | |
7 | #include <string> | |
8 | ||
9 | #include <type_traits> | |
10 | ||
11 | #include <gtest/gtest.h> | |
12 | ||
13 | TEST(StringViewTest,EmptyString) | |
14 | { | |
15 | constexpr APT::StringView defString; | |
16 | static_assert( 0 == defString.length(), "def right size"); | |
17 | ||
18 | APT::StringView strString{std::string{}}; | |
19 | EXPECT_EQ(0, strString.length()); | |
20 | ||
21 | constexpr char const * const charp = ""; | |
22 | constexpr APT::StringView charpString{charp, 0}; | |
23 | static_assert( 0 == charpString.length(), "charp right size"); | |
24 | ||
25 | APT::StringView charp2String{charp}; | |
26 | EXPECT_EQ(0, strString.length()); | |
27 | ||
28 | const APT::StringView charaString{""}; | |
29 | EXPECT_EQ(0, charaString.length()); | |
30 | ||
31 | EXPECT_TRUE(APT::StringView("") == ""); | |
32 | EXPECT_FALSE(APT::StringView("") != ""); | |
33 | } | |
34 | ||
35 | TEST(StringViewTest,FooString) | |
36 | { | |
37 | constexpr APT::StringView defString("foo", 3); | |
38 | static_assert( 3 == defString.length(), "def right size"); | |
39 | EXPECT_EQ(defString.to_string(), defString.data()); | |
40 | ||
41 | APT::StringView strString{std::string{"foo"}}; | |
42 | EXPECT_EQ(3, strString.length()); | |
43 | EXPECT_EQ(strString.to_string(), strString.data()); | |
44 | ||
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()); | |
49 | ||
50 | APT::StringView charp2String{charp}; | |
51 | EXPECT_EQ(3, charp2String.length()); | |
52 | EXPECT_EQ(charp2String.to_string(), charp2String.data()); | |
53 | ||
54 | const APT::StringView charaString{"foo"}; | |
55 | EXPECT_EQ(3, charaString.length()); | |
56 | EXPECT_EQ(charaString.to_string(), charaString.data()); | |
57 | ||
58 | EXPECT_TRUE(APT::StringView("foo") == "foo"); | |
59 | EXPECT_FALSE(APT::StringView("foo") != "foo"); | |
60 | } | |
61 | ||
62 | TEST(StringViewTest,SubStr) | |
63 | { | |
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()); | |
68 | } | |
69 | ||
70 | TEST(StringViewTest,Find) | |
71 | { | |
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)); | |
78 | } | |
79 | ||
80 | TEST(StringViewTest,RFind) | |
81 | { | |
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)); | |
88 | } |