]>
Commit | Line | Data |
---|---|---|
ec6a4a83 DK |
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 | ||
ef6cc0e2 JAK |
28 | const APT::StringView charaString{""}; |
29 | EXPECT_EQ(0, charaString.length()); | |
ec6a4a83 DK |
30 | |
31 | EXPECT_TRUE(APT::StringView("") == ""); | |
32 | EXPECT_FALSE(APT::StringView("") != ""); | |
33 | } | |
34 | ||
35 | TEST(StringViewTest,FooString) | |
36 | { | |
5404685b | 37 | constexpr APT::StringView defString("fooGARBAGE", 3); |
ec6a4a83 | 38 | static_assert( 3 == defString.length(), "def right size"); |
5404685b | 39 | EXPECT_EQ(0, defString.to_string().compare(0, defString.length(), defString.data(), 3)); |
ec6a4a83 DK |
40 | |
41 | APT::StringView strString{std::string{"foo"}}; | |
42 | EXPECT_EQ(3, strString.length()); | |
5404685b | 43 | EXPECT_EQ(0, strString.to_string().compare(0, strString.length(), strString.data(), 3)); |
ec6a4a83 | 44 | |
5404685b | 45 | constexpr char const * const charp = "fooGARBAGE"; |
ec6a4a83 | 46 | constexpr APT::StringView charpString{charp, 3}; |
5404685b DK |
47 | EXPECT_EQ(3, charpString.length()); |
48 | EXPECT_EQ(0, charpString.to_string().compare(0, charpString.length(), charpString.data(), 3)); | |
ec6a4a83 | 49 | |
5404685b DK |
50 | char * charp2 = strdup("foo"); |
51 | APT::StringView charp2String{charp2}; | |
ec6a4a83 | 52 | EXPECT_EQ(3, charp2String.length()); |
5404685b DK |
53 | EXPECT_EQ(0, charp2String.to_string().compare(0, charp2String.length(), charp2String.data(), 3)); |
54 | free(charp2); | |
ec6a4a83 | 55 | |
ef6cc0e2 JAK |
56 | const APT::StringView charaString{"foo"}; |
57 | EXPECT_EQ(3, charaString.length()); | |
5404685b | 58 | EXPECT_EQ(0, charaString.to_string().compare(0, charaString.length(), charaString.data(), 3)); |
ec6a4a83 DK |
59 | |
60 | EXPECT_TRUE(APT::StringView("foo") == "foo"); | |
61 | EXPECT_FALSE(APT::StringView("foo") != "foo"); | |
62 | } | |
63 | ||
64 | TEST(StringViewTest,SubStr) | |
65 | { | |
ef6cc0e2 | 66 | const APT::StringView defString("Hello World!"); |
ec6a4a83 DK |
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()); | |
70 | } | |
71 | ||
72 | TEST(StringViewTest,Find) | |
73 | { | |
ef6cc0e2 | 74 | const APT::StringView defString("Hello World!"); |
ec6a4a83 DK |
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)); | |
80 | } | |
81 | ||
82 | TEST(StringViewTest,RFind) | |
83 | { | |
ef6cc0e2 | 84 | const APT::StringView defString("Hello World!"); |
ec6a4a83 DK |
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)); | |
90 | } |