summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2016-01-15 17:11:19 +0100
committerDavid Kalnischkies <david@kalnischkies.de>2016-01-15 18:19:16 +0100
commitec6a4a831e57834bf4d7abb91fc8b298c2fbfcc2 (patch)
tree8f989417253ca51e308fe9d2d73e0a66dcebf1a6 /test
parent9b2845e1691b5587d731c3dae9f695ca2db0be1a (diff)
provide a constexpr char[] overload for APT::StringView
The commit also adds a few trivial tests Git-Dch: Ignore
Diffstat (limited to 'test')
-rw-r--r--test/libapt/stringview_test.cc88
1 files changed, 88 insertions, 0 deletions
diff --git a/test/libapt/stringview_test.cc b/test/libapt/stringview_test.cc
new file mode 100644
index 000000000..bac69ec2e
--- /dev/null
+++ b/test/libapt/stringview_test.cc
@@ -0,0 +1,88 @@
+#if !(defined APT_PKG_EXPOSE_STRING_VIEW)
+ #define APT_PKG_EXPOSE_STRING_VIEW
+#endif
+
+#include <config.h>
+#include <apt-pkg/string_view.h>
+#include <string>
+
+#include <type_traits>
+
+#include <gtest/gtest.h>
+
+TEST(StringViewTest,EmptyString)
+{
+ constexpr APT::StringView defString;
+ static_assert( 0 == defString.length(), "def right size");
+
+ APT::StringView strString{std::string{}};
+ EXPECT_EQ(0, strString.length());
+
+ constexpr char const * const charp = "";
+ constexpr APT::StringView charpString{charp, 0};
+ static_assert( 0 == charpString.length(), "charp right size");
+
+ APT::StringView charp2String{charp};
+ EXPECT_EQ(0, strString.length());
+
+ constexpr APT::StringView charaString{""};
+ static_assert( 0 == charaString.length(), "chara right size");
+
+ EXPECT_TRUE(APT::StringView("") == "");
+ EXPECT_FALSE(APT::StringView("") != "");
+}
+
+TEST(StringViewTest,FooString)
+{
+ constexpr APT::StringView defString("foo", 3);
+ static_assert( 3 == defString.length(), "def right size");
+ EXPECT_EQ(defString.to_string(), defString.data());
+
+ APT::StringView strString{std::string{"foo"}};
+ EXPECT_EQ(3, strString.length());
+ EXPECT_EQ(strString.to_string(), strString.data());
+
+ constexpr char const * const charp = "foo";
+ constexpr APT::StringView charpString{charp, 3};
+ static_assert( 3 == charpString.length(), "charp right size");
+ EXPECT_EQ(charpString.to_string(), charpString.data());
+
+ APT::StringView charp2String{charp};
+ EXPECT_EQ(3, charp2String.length());
+ EXPECT_EQ(charp2String.to_string(), charp2String.data());
+
+ constexpr APT::StringView charaString{"foo"};
+ static_assert( 3 == charaString.length(), "chara right size");
+ EXPECT_EQ(charaString.to_string(), charaString.data());
+
+ EXPECT_TRUE(APT::StringView("foo") == "foo");
+ EXPECT_FALSE(APT::StringView("foo") != "foo");
+}
+
+TEST(StringViewTest,SubStr)
+{
+ constexpr APT::StringView defString("Hello World!");
+ EXPECT_EQ(defString.to_string().substr(6), defString.substr(6).to_string());
+ EXPECT_EQ(defString.to_string().substr(0,5), defString.substr(0,5).to_string());
+ EXPECT_EQ(defString.to_string().substr(6,5), defString.substr(6,5).to_string());
+}
+
+TEST(StringViewTest,Find)
+{
+ constexpr APT::StringView defString("Hello World!");
+ EXPECT_EQ(defString.to_string().find('l'), defString.find('l'));
+ EXPECT_EQ(defString.to_string().find('X'), defString.find('X'));
+ EXPECT_EQ(defString.to_string().find('e',3), defString.find('e',3));
+ EXPECT_EQ(defString.to_string().find('l',6), defString.find('l',6));
+ EXPECT_EQ(defString.to_string().find('l',11), defString.find('l',11));
+}
+
+TEST(StringViewTest,RFind)
+{
+ constexpr APT::StringView defString("Hello World!");
+ EXPECT_EQ(defString.to_string().rfind('l'), defString.rfind('l'));
+ EXPECT_EQ(defString.to_string().rfind('X'), defString.rfind('X'));
+ EXPECT_EQ(defString.to_string().rfind('e',3), defString.rfind('e',3));
+ EXPECT_EQ(defString.to_string().rfind('l',6), defString.rfind('l',6));
+ EXPECT_EQ(defString.to_string().rfind('l',11), defString.rfind('l',11));
+}