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