]> git.saurik.com Git - apt.git/blob - test/libapt/stringview_test.cc
test/integration/test-srcrecord: Make executable
[apt.git] / test / libapt / stringview_test.cc
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("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));
40
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));
44
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));
49
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));
54 free(charp2);
55
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));
59
60 EXPECT_TRUE(APT::StringView("foo") == "foo");
61 EXPECT_FALSE(APT::StringView("foo") != "foo");
62 }
63
64 TEST(StringViewTest,SubStr)
65 {
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());
70 }
71
72 TEST(StringViewTest,Find)
73 {
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));
80 }
81
82 TEST(StringViewTest,RFind)
83 {
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));
90 }