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.
 
 
 
 
 
 

91 lines
3.2 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(0u, 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(0u, strString.length());
  20. const APT::StringView charaString{""};
  21. EXPECT_EQ(0u, charaString.length());
  22. EXPECT_TRUE(APT::StringView("") == "");
  23. EXPECT_FALSE(APT::StringView("") != "");
  24. }
  25. TEST(StringViewTest,FooString)
  26. {
  27. constexpr APT::StringView defString("fooGARBAGE", 3);
  28. static_assert( 3 == defString.length(), "def right size");
  29. EXPECT_EQ(0, defString.to_string().compare(0, defString.length(), defString.data(), 3));
  30. APT::StringView strString{std::string{"foo"}};
  31. EXPECT_EQ(3u, strString.length());
  32. EXPECT_EQ(0, strString.to_string().compare(0, strString.length(), strString.data(), 3));
  33. constexpr char const * const charp = "fooGARBAGE";
  34. constexpr APT::StringView charpString{charp, 3};
  35. EXPECT_EQ(3u, charpString.length());
  36. EXPECT_EQ(0, charpString.to_string().compare(0, charpString.length(), charpString.data(), 3));
  37. char * charp2 = strdup("foo");
  38. APT::StringView charp2String{charp2};
  39. EXPECT_EQ(3u, charp2String.length());
  40. EXPECT_EQ(0, charp2String.to_string().compare(0, charp2String.length(), charp2String.data(), 3));
  41. free(charp2);
  42. const APT::StringView charaString{"foo"};
  43. EXPECT_EQ(3u, charaString.length());
  44. EXPECT_EQ(0, charaString.to_string().compare(0, charaString.length(), charaString.data(), 3));
  45. EXPECT_TRUE(APT::StringView("foo") == "foo");
  46. EXPECT_FALSE(APT::StringView("foo") != "foo");
  47. }
  48. TEST(StringViewTest,SubStr)
  49. {
  50. const APT::StringView defString("Hello World!");
  51. EXPECT_EQ(defString.to_string().substr(6), defString.substr(6).to_string());
  52. EXPECT_EQ(defString.to_string().substr(0,5), defString.substr(0,5).to_string());
  53. EXPECT_EQ(defString.to_string().substr(6,5), defString.substr(6,5).to_string());
  54. }
  55. TEST(StringViewTest,Find)
  56. {
  57. const APT::StringView defString("Hello World!");
  58. EXPECT_EQ(defString.to_string().find('l'), defString.find('l'));
  59. EXPECT_EQ(defString.to_string().find('X'), defString.find('X'));
  60. EXPECT_EQ(defString.to_string().find('e',3), defString.find('e',3));
  61. EXPECT_EQ(defString.to_string().find('l',6), defString.find('l',6));
  62. EXPECT_EQ(defString.to_string().find('l',11), defString.find('l',11));
  63. }
  64. TEST(StringViewTest,RFind)
  65. {
  66. const APT::StringView defString("Hello World!");
  67. EXPECT_EQ(defString.to_string().rfind('l'), defString.rfind('l'));
  68. EXPECT_EQ(defString.to_string().rfind('X'), defString.rfind('X'));
  69. EXPECT_EQ(defString.to_string().rfind('e',3), defString.rfind('e',3));
  70. EXPECT_EQ(defString.to_string().rfind('l',6), defString.rfind('l',6));
  71. EXPECT_EQ(defString.to_string().rfind('l',11), defString.rfind('l',11));
  72. }