William A. Kennington III | 62e40d6 | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 1 | #include <fmt/format.h> |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 2 | #include <gtest/gtest.h> |
| 3 | #include <iostream> |
| 4 | #include <set> |
| 5 | #include <stdplus/zstring_view.hpp> |
| 6 | #include <string> |
| 7 | #include <unordered_set> |
| 8 | |
| 9 | namespace stdplus |
| 10 | { |
| 11 | |
| 12 | using std::literals::string_literals::operator""s; |
William A. Kennington III | a2d2df7 | 2022-09-20 18:49:36 -0700 | [diff] [blame] | 13 | using std::literals::string_view_literals::operator""sv; |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 14 | using zstring_view_literals::operator""_zsv; |
| 15 | |
William A. Kennington III | b6032a1 | 2022-09-23 18:27:05 -0700 | [diff] [blame] | 16 | TEST(ZstringView, IsString) |
| 17 | { |
| 18 | EXPECT_TRUE(( |
| 19 | detail::same_string<std::string, char, std::char_traits<char>>::value)); |
| 20 | EXPECT_FALSE((detail::same_string<std::string_view, char, |
| 21 | std::char_traits<char>>::value)); |
| 22 | } |
| 23 | |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 24 | TEST(ZstringView, Basic) |
| 25 | { |
| 26 | auto s1 = zstring_view("ac"); |
| 27 | auto s = "b"s; |
| 28 | auto s2 = zstring_view(s); |
| 29 | std::string_view sv = s1; |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame] | 30 | char mut[] = "aa"; |
| 31 | auto s3 = zstring_view(mut); |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 32 | |
William A. Kennington III | 37b2d5e | 2022-10-11 14:07:14 -0700 | [diff] [blame^] | 33 | EXPECT_EQ(1, s1.find('c')); |
| 34 | EXPECT_EQ(0, s3.find('a')); |
| 35 | EXPECT_EQ(1, s3.rfind('a')); |
| 36 | |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 37 | EXPECT_NE(sv, s2); |
| 38 | EXPECT_NE(s1, s2); |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame] | 39 | EXPECT_EQ(s3, "aa"); |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 40 | |
| 41 | EXPECT_EQ("ac"_zsv, s1); |
| 42 | EXPECT_EQ("ac", s1); |
| 43 | EXPECT_EQ(s1, "ac"); |
| 44 | EXPECT_EQ(s, s2); |
| 45 | EXPECT_EQ(s2, s); |
| 46 | EXPECT_EQ(s1, sv); |
| 47 | EXPECT_EQ(sv, s1); |
| 48 | EXPECT_LT("ab", s1); |
| 49 | EXPECT_GT("ad", s1); |
| 50 | EXPECT_LE(s1, "ac"); |
| 51 | EXPECT_LE(s, s2); |
| 52 | EXPECT_LE(s2, s); |
| 53 | EXPECT_LE(s1, sv); |
| 54 | EXPECT_LE(sv, s1); |
| 55 | |
| 56 | std::cerr << s1; |
William A. Kennington III | 62e40d6 | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 57 | EXPECT_EQ("bac", fmt::format("b{}", s1)); |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 58 | |
| 59 | std::unordered_set<zstring_view> uset{s1, s2}; |
| 60 | EXPECT_EQ(1, uset.count("ac")); |
| 61 | EXPECT_EQ(1, uset.count("b")); |
| 62 | std::set<zstring_view> set{s2, s2}; |
| 63 | EXPECT_EQ(0, set.count("ac")); |
| 64 | EXPECT_EQ(1, set.count("b")); |
William A. Kennington III | b6032a1 | 2022-09-23 18:27:05 -0700 | [diff] [blame] | 65 | |
| 66 | auto from_str = [&](zstring_view cs) { EXPECT_EQ(cs, "ac"); }; |
| 67 | from_str("ac"s); |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST(ZstringView, ConstructError) |
| 71 | { |
| 72 | auto s = "hi\0"s; |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame] | 73 | #ifdef NDEBUG |
William A. Kennington III | aaa8e0d | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 74 | EXPECT_EQ("hi\0"sv, zstring_view(s)); |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame] | 75 | #else |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 76 | EXPECT_THROW((zstring_view(s)), std::invalid_argument); |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | char mut1[] = "aa\0"; |
William A. Kennington III | aaa8e0d | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 80 | EXPECT_EQ("aa", zstring_view(mut1)); |
| 81 | #ifndef NDEBUG |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame] | 82 | char mut2[] = {'a', 'a'}; |
William A. Kennington III | e6e44e7 | 2022-09-22 17:54:07 -0700 | [diff] [blame] | 83 | EXPECT_THROW((zstring_view(mut2)), std::invalid_argument); |
| 84 | #endif |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST(ZstringView, Suffix) |
| 88 | { |
| 89 | auto s1 = zstring_view("ac"); |
| 90 | |
| 91 | EXPECT_EQ("ac", s1.suffix(0)); |
| 92 | EXPECT_EQ("c", s1.suffix(1)); |
| 93 | EXPECT_EQ("", s1.suffix(2)); |
| 94 | EXPECT_THROW(s1.suffix(3), std::out_of_range); |
| 95 | } |
| 96 | |
William A. Kennington III | a2d2df7 | 2022-09-20 18:49:36 -0700 | [diff] [blame] | 97 | TEST(ZstringView, NoTypeCoercion) |
| 98 | { |
William A. Kennington III | aaa8e0d | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 99 | EXPECT_EQ(""_zsv, "\0"); |
| 100 | EXPECT_EQ("\0", ""_zsv); |
William A. Kennington III | a2d2df7 | 2022-09-20 18:49:36 -0700 | [diff] [blame] | 101 | EXPECT_NE(""_zsv, "\0"sv); |
| 102 | EXPECT_NE("\0"sv, ""_zsv); |
| 103 | EXPECT_LT(""_zsv, "\0"sv); |
| 104 | EXPECT_GT("\0"sv, ""_zsv); |
| 105 | auto str = "\0"s; |
| 106 | EXPECT_NE(""_zsv, str); |
| 107 | EXPECT_NE(str, ""_zsv); |
| 108 | EXPECT_LT(""_zsv, str); |
| 109 | EXPECT_GT(str, ""_zsv); |
| 110 | } |
| 111 | |
William A. Kennington III | 61a81bc | 2022-09-19 17:46:49 -0700 | [diff] [blame] | 112 | } // namespace stdplus |