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