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 | |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 3 | #include <stdplus/zstring.hpp> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 4 | |
William A. Kennington III | d040f58 | 2023-07-17 03:13:58 -0700 | [diff] [blame] | 5 | #include <format> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 6 | #include <iostream> |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <string_view> |
| 9 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 12 | using std::literals::string_literals::operator""s; |
| 13 | using std::literals::string_view_literals::operator""sv; |
| 14 | |
| 15 | namespace stdplus |
| 16 | { |
| 17 | |
| 18 | TEST(Zstring, Construct) |
| 19 | { |
| 20 | auto str = "a\0"s; |
| 21 | const auto cstr = str; |
| 22 | #ifdef NDEBUG |
| 23 | EXPECT_EQ("a", zstring(str)); |
| 24 | EXPECT_EQ("a", const_zstring(str)); |
| 25 | EXPECT_EQ("a", const_zstring(cstr)); |
| 26 | #else |
| 27 | EXPECT_THROW((zstring(str)), std::invalid_argument); |
| 28 | EXPECT_THROW((const_zstring(str)), std::invalid_argument); |
| 29 | EXPECT_THROW((const_zstring(cstr)), std::invalid_argument); |
| 30 | #endif |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 31 | EXPECT_EQ("b", const_zstring("b\0")); |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 32 | char as[] = "c"; |
| 33 | EXPECT_EQ("c", zstring(as)); |
| 34 | EXPECT_EQ("c", const_zstring(as)); |
| 35 | |
William A. Kennington III | b6032a1 | 2022-09-23 18:27:05 -0700 | [diff] [blame] | 36 | auto str2 = "d"s; |
| 37 | auto zs = zstring(str2); |
| 38 | EXPECT_EQ(str2, zs); |
| 39 | |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 40 | std::cerr << const_zstring(as); |
William A. Kennington III | 62e40d6 | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 41 | EXPECT_EQ("dd", fmt::format("d{}", zs)); |
| 42 | EXPECT_EQ("dc", fmt::format("d{}", const_zstring(as))); |
William A. Kennington III | d040f58 | 2023-07-17 03:13:58 -0700 | [diff] [blame] | 43 | EXPECT_EQ("dd", std::format("d{}", zs)); |
| 44 | EXPECT_EQ("dc", std::format("d{}", const_zstring(as))); |
William A. Kennington III | b6032a1 | 2022-09-23 18:27:05 -0700 | [diff] [blame] | 45 | |
| 46 | auto from_str = [&](const_zstring cs) { EXPECT_EQ(cs, "ac"); }; |
| 47 | from_str("ac"s); |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | TEST(Zstring, NoTypeCoercion) |
| 51 | { |
| 52 | char empty[] = ""; |
| 53 | auto zs = zstring(empty); |
| 54 | auto czs = const_zstring(""); |
| 55 | |
William A. Kennington III | aaa8e0d | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 56 | EXPECT_EQ(zs, "\0"); |
| 57 | EXPECT_EQ("\0", zs); |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 58 | EXPECT_NE(zs, "\0"sv); |
| 59 | EXPECT_NE("\0"sv, zs); |
| 60 | EXPECT_LT(zs, "\0"sv); |
| 61 | EXPECT_GT("\0"sv, zs); |
William A. Kennington III | aaa8e0d | 2022-09-24 22:36:31 -0700 | [diff] [blame] | 62 | EXPECT_EQ(czs, "\0"); |
| 63 | EXPECT_EQ("\0", czs); |
William A. Kennington III | f2ddf51 | 2022-09-20 15:36:44 -0700 | [diff] [blame] | 64 | EXPECT_NE(czs, "\0"sv); |
| 65 | EXPECT_NE("\0"sv, czs); |
| 66 | EXPECT_LT(czs, "\0"sv); |
| 67 | EXPECT_GT("\0"sv, czs); |
| 68 | |
| 69 | auto str = "\0"s; |
| 70 | EXPECT_NE(zs, str); |
| 71 | EXPECT_NE(str, zs); |
| 72 | EXPECT_LT(zs, str); |
| 73 | EXPECT_GT(str, zs); |
| 74 | EXPECT_NE(czs, str); |
| 75 | EXPECT_NE(str, czs); |
| 76 | EXPECT_LT(czs, str); |
| 77 | EXPECT_GT(str, czs); |
| 78 | } |
| 79 | |
| 80 | TEST(Zstring, Comparison) |
| 81 | { |
| 82 | char mut[] = "ac"; |
| 83 | auto zs = zstring(mut); |
| 84 | auto czs = const_zstring("ac"); |
| 85 | |
| 86 | #define test(cmp, rcmp, str) \ |
| 87 | EXPECT_##cmp(zs, const_zstring(str)); \ |
| 88 | EXPECT_##rcmp(const_zstring(str), zs); \ |
| 89 | EXPECT_##cmp(czs, const_zstring(str)); \ |
| 90 | EXPECT_##rcmp(const_zstring(str), czs); \ |
| 91 | EXPECT_##cmp(zs, str); \ |
| 92 | EXPECT_##rcmp(str, zs); \ |
| 93 | EXPECT_##cmp(czs, str); \ |
| 94 | EXPECT_##rcmp(str, czs); \ |
| 95 | EXPECT_##cmp(zs, str##sv); \ |
| 96 | EXPECT_##rcmp(str##sv, zs); \ |
| 97 | EXPECT_##cmp(czs, str##sv); \ |
| 98 | EXPECT_##rcmp(str##sv, czs); \ |
| 99 | EXPECT_##cmp(zs, str##s); \ |
| 100 | EXPECT_##rcmp(str##s, zs); \ |
| 101 | EXPECT_##cmp(czs, str##s); \ |
| 102 | EXPECT_##rcmp(str##s, czs); \ |
| 103 | EXPECT_##cmp(zs, reinterpret_cast<const char*>(str)); \ |
| 104 | EXPECT_##rcmp(reinterpret_cast<const char*>(str), zs); \ |
| 105 | EXPECT_##cmp(czs, reinterpret_cast<const char*>(str)); \ |
| 106 | EXPECT_##rcmp(reinterpret_cast<const char*>(str), czs) |
| 107 | test(EQ, EQ, "ac"); |
| 108 | test(GT, LT, "a"); |
| 109 | test(LT, GT, "acb"); |
| 110 | test(LT, GT, "ad"); |
| 111 | test(GT, LT, "ab"); |
| 112 | #undef test |
| 113 | } |
| 114 | |
| 115 | } // namespace stdplus |