You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

89 lines
2.9 KiB

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