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