William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame^] | 1 | #include <gtest/gtest.h> |
| 2 | #include <iostream> |
| 3 | #include <set> |
| 4 | #include <stdplus/zstring_view.hpp> |
| 5 | #include <string> |
| 6 | #include <unordered_set> |
| 7 | |
| 8 | namespace stdplus |
| 9 | { |
| 10 | |
| 11 | using std::literals::string_literals::operator""s; |
| 12 | using zstring_view_literals::operator""_zsv; |
| 13 | |
| 14 | TEST(ZstringView, Basic) |
| 15 | { |
| 16 | auto s1 = zstring_view("ac"); |
| 17 | auto s = "b"s; |
| 18 | auto s2 = zstring_view(s); |
| 19 | std::string_view sv = s1; |
| 20 | |
| 21 | EXPECT_NE(sv, s2); |
| 22 | EXPECT_NE(s1, s2); |
| 23 | |
| 24 | EXPECT_EQ("ac"_zsv, s1); |
| 25 | EXPECT_EQ("ac", s1); |
| 26 | EXPECT_EQ(s1, "ac"); |
| 27 | EXPECT_EQ(s, s2); |
| 28 | EXPECT_EQ(s2, s); |
| 29 | EXPECT_EQ(s1, sv); |
| 30 | EXPECT_EQ(sv, s1); |
| 31 | EXPECT_LT("ab", s1); |
| 32 | EXPECT_GT("ad", s1); |
| 33 | EXPECT_LE(s1, "ac"); |
| 34 | EXPECT_LE(s, s2); |
| 35 | EXPECT_LE(s2, s); |
| 36 | EXPECT_LE(s1, sv); |
| 37 | EXPECT_LE(sv, s1); |
| 38 | |
| 39 | std::cerr << s1; |
| 40 | |
| 41 | std::unordered_set<zstring_view> uset{s1, s2}; |
| 42 | EXPECT_EQ(1, uset.count("ac")); |
| 43 | EXPECT_EQ(1, uset.count("b")); |
| 44 | std::set<zstring_view> set{s2, s2}; |
| 45 | EXPECT_EQ(0, set.count("ac")); |
| 46 | EXPECT_EQ(1, set.count("b")); |
| 47 | } |
| 48 | |
| 49 | TEST(ZstringView, ConstructError) |
| 50 | { |
| 51 | auto s = "hi\0"s; |
| 52 | EXPECT_THROW((zstring_view(s)), std::invalid_argument); |
| 53 | } |
| 54 | |
| 55 | TEST(ZstringView, Suffix) |
| 56 | { |
| 57 | auto s1 = zstring_view("ac"); |
| 58 | |
| 59 | EXPECT_EQ("ac", s1.suffix(0)); |
| 60 | EXPECT_EQ("c", s1.suffix(1)); |
| 61 | EXPECT_EQ("", s1.suffix(2)); |
| 62 | EXPECT_THROW(s1.suffix(3), std::out_of_range); |
| 63 | } |
| 64 | |
| 65 | } // namespace stdplus |