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; |
William A. Kennington III | a2d2df7 | 2022-09-20 18:49:36 -0700 | [diff] [blame] | 12 | using std::literals::string_view_literals::operator""sv; |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 13 | using zstring_view_literals::operator""_zsv; |
| 14 | |
| 15 | TEST(ZstringView, Basic) |
| 16 | { |
| 17 | auto s1 = zstring_view("ac"); |
| 18 | auto s = "b"s; |
| 19 | auto s2 = zstring_view(s); |
| 20 | std::string_view sv = s1; |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame^] | 21 | char mut[] = "aa"; |
| 22 | auto s3 = zstring_view(mut); |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 23 | |
| 24 | EXPECT_NE(sv, s2); |
| 25 | EXPECT_NE(s1, s2); |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame^] | 26 | EXPECT_EQ(s3, "aa"); |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 27 | |
| 28 | EXPECT_EQ("ac"_zsv, s1); |
| 29 | EXPECT_EQ("ac", s1); |
| 30 | EXPECT_EQ(s1, "ac"); |
| 31 | EXPECT_EQ(s, s2); |
| 32 | EXPECT_EQ(s2, s); |
| 33 | EXPECT_EQ(s1, sv); |
| 34 | EXPECT_EQ(sv, s1); |
| 35 | EXPECT_LT("ab", s1); |
| 36 | EXPECT_GT("ad", s1); |
| 37 | EXPECT_LE(s1, "ac"); |
| 38 | EXPECT_LE(s, s2); |
| 39 | EXPECT_LE(s2, s); |
| 40 | EXPECT_LE(s1, sv); |
| 41 | EXPECT_LE(sv, s1); |
| 42 | |
| 43 | std::cerr << s1; |
| 44 | |
| 45 | std::unordered_set<zstring_view> uset{s1, s2}; |
| 46 | EXPECT_EQ(1, uset.count("ac")); |
| 47 | EXPECT_EQ(1, uset.count("b")); |
| 48 | std::set<zstring_view> set{s2, s2}; |
| 49 | EXPECT_EQ(0, set.count("ac")); |
| 50 | EXPECT_EQ(1, set.count("b")); |
| 51 | } |
| 52 | |
| 53 | TEST(ZstringView, ConstructError) |
| 54 | { |
| 55 | auto s = "hi\0"s; |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame^] | 56 | #ifdef NDEBUG |
| 57 | EXPECT_EQ("hi", zstring_view(s)); |
| 58 | #else |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 59 | EXPECT_THROW((zstring_view(s)), std::invalid_argument); |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame^] | 60 | #endif |
| 61 | |
| 62 | char mut1[] = "aa\0"; |
| 63 | char mut2[] = {'a', 'a'}; |
| 64 | #ifdef NDEBUG |
| 65 | EXPECT_EQ("aa\0", zstring_view(mut1)); |
| 66 | EXPECT_EQ("a", zstring_view(mut2)); |
| 67 | #else |
| 68 | EXPECT_THROW((zstring_view(mut1)), std::invalid_argument); |
| 69 | EXPECT_THROW((zstring_view(mut2)), std::invalid_argument); |
| 70 | #endif |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(ZstringView, Suffix) |
| 74 | { |
| 75 | auto s1 = zstring_view("ac"); |
| 76 | |
| 77 | EXPECT_EQ("ac", s1.suffix(0)); |
| 78 | EXPECT_EQ("c", s1.suffix(1)); |
| 79 | EXPECT_EQ("", s1.suffix(2)); |
| 80 | EXPECT_THROW(s1.suffix(3), std::out_of_range); |
| 81 | } |
| 82 | |
William A. Kennington III | a2d2df7 | 2022-09-20 18:49:36 -0700 | [diff] [blame] | 83 | TEST(ZstringView, NoTypeCoercion) |
| 84 | { |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame^] | 85 | EXPECT_NE(""_zsv, "\0"); |
| 86 | EXPECT_NE("\0", ""_zsv); |
William A. Kennington III | a2d2df7 | 2022-09-20 18:49:36 -0700 | [diff] [blame] | 87 | EXPECT_NE(""_zsv, "\0"sv); |
| 88 | EXPECT_NE("\0"sv, ""_zsv); |
| 89 | EXPECT_LT(""_zsv, "\0"sv); |
| 90 | EXPECT_GT("\0"sv, ""_zsv); |
| 91 | auto str = "\0"s; |
| 92 | EXPECT_NE(""_zsv, str); |
| 93 | EXPECT_NE(str, ""_zsv); |
| 94 | EXPECT_LT(""_zsv, str); |
| 95 | EXPECT_GT(str, ""_zsv); |
| 96 | } |
| 97 | |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 98 | } // namespace stdplus |