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