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