| #include <gtest/gtest.h> |
| #include <iostream> |
| #include <set> |
| #include <stdplus/zstring_view.hpp> |
| #include <string> |
| #include <unordered_set> |
| |
| namespace stdplus |
| { |
| |
| using std::literals::string_literals::operator""s; |
| using std::literals::string_view_literals::operator""sv; |
| using zstring_view_literals::operator""_zsv; |
| |
| TEST(ZstringView, Basic) |
| { |
| auto s1 = zstring_view("ac"); |
| auto s = "b"s; |
| auto s2 = zstring_view(s); |
| std::string_view sv = s1; |
| |
| EXPECT_NE(sv, s2); |
| EXPECT_NE(s1, s2); |
| |
| EXPECT_EQ("ac"_zsv, s1); |
| EXPECT_EQ("ac", s1); |
| EXPECT_EQ(s1, "ac"); |
| EXPECT_EQ(s, s2); |
| EXPECT_EQ(s2, s); |
| EXPECT_EQ(s1, sv); |
| EXPECT_EQ(sv, s1); |
| EXPECT_LT("ab", s1); |
| EXPECT_GT("ad", s1); |
| EXPECT_LE(s1, "ac"); |
| EXPECT_LE(s, s2); |
| EXPECT_LE(s2, s); |
| EXPECT_LE(s1, sv); |
| EXPECT_LE(sv, s1); |
| |
| std::cerr << s1; |
| |
| std::unordered_set<zstring_view> uset{s1, s2}; |
| EXPECT_EQ(1, uset.count("ac")); |
| EXPECT_EQ(1, uset.count("b")); |
| std::set<zstring_view> set{s2, s2}; |
| EXPECT_EQ(0, set.count("ac")); |
| EXPECT_EQ(1, set.count("b")); |
| } |
| |
| TEST(ZstringView, ConstructError) |
| { |
| auto s = "hi\0"s; |
| EXPECT_THROW((zstring_view(s)), std::invalid_argument); |
| } |
| |
| TEST(ZstringView, Suffix) |
| { |
| auto s1 = zstring_view("ac"); |
| |
| EXPECT_EQ("ac", s1.suffix(0)); |
| EXPECT_EQ("c", s1.suffix(1)); |
| EXPECT_EQ("", s1.suffix(2)); |
| EXPECT_THROW(s1.suffix(3), std::out_of_range); |
| } |
| |
| TEST(ZstringView, NoTypeCoercion) |
| { |
| EXPECT_NE(""_zsv, "\0"sv); |
| EXPECT_NE("\0"sv, ""_zsv); |
| EXPECT_LT(""_zsv, "\0"sv); |
| EXPECT_GT("\0"sv, ""_zsv); |
| auto str = "\0"s; |
| EXPECT_NE(""_zsv, str); |
| EXPECT_NE(str, ""_zsv); |
| EXPECT_LT(""_zsv, str); |
| EXPECT_GT(str, ""_zsv); |
| } |
| |
| } // namespace stdplus |