blob: 53c3a1457337194bf90c08ead239725c669fae85 [file] [log] [blame]
William A. Kennington III62e40d62022-09-24 22:36:31 -07001#include <fmt/format.h>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -07003#include <stdplus/zstring.hpp>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05004
5#include <iostream>
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -07006#include <string>
7#include <string_view>
8
Patrick Williamsd1984dd2023-05-10 16:12:44 -05009#include <gtest/gtest.h>
10
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070011using std::literals::string_literals::operator""s;
12using std::literals::string_view_literals::operator""sv;
13
14namespace stdplus
15{
16
17TEST(Zstring, Construct)
18{
19 auto str = "a\0"s;
20 const auto cstr = str;
21#ifdef NDEBUG
22 EXPECT_EQ("a", zstring(str));
23 EXPECT_EQ("a", const_zstring(str));
24 EXPECT_EQ("a", const_zstring(cstr));
25#else
26 EXPECT_THROW((zstring(str)), std::invalid_argument);
27 EXPECT_THROW((const_zstring(str)), std::invalid_argument);
28 EXPECT_THROW((const_zstring(cstr)), std::invalid_argument);
29#endif
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070030 EXPECT_EQ("b", const_zstring("b\0"));
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070031 char as[] = "c";
32 EXPECT_EQ("c", zstring(as));
33 EXPECT_EQ("c", const_zstring(as));
34
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070035 auto str2 = "d"s;
36 auto zs = zstring(str2);
37 EXPECT_EQ(str2, zs);
38
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070039 std::cerr << const_zstring(as);
William A. Kennington III62e40d62022-09-24 22:36:31 -070040 EXPECT_EQ("dd", fmt::format("d{}", zs));
41 EXPECT_EQ("dc", fmt::format("d{}", const_zstring(as)));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070042
43 auto from_str = [&](const_zstring cs) { EXPECT_EQ(cs, "ac"); };
44 from_str("ac"s);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070045}
46
47TEST(Zstring, NoTypeCoercion)
48{
49 char empty[] = "";
50 auto zs = zstring(empty);
51 auto czs = const_zstring("");
52
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070053 EXPECT_EQ(zs, "\0");
54 EXPECT_EQ("\0", zs);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070055 EXPECT_NE(zs, "\0"sv);
56 EXPECT_NE("\0"sv, zs);
57 EXPECT_LT(zs, "\0"sv);
58 EXPECT_GT("\0"sv, zs);
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070059 EXPECT_EQ(czs, "\0");
60 EXPECT_EQ("\0", czs);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070061 EXPECT_NE(czs, "\0"sv);
62 EXPECT_NE("\0"sv, czs);
63 EXPECT_LT(czs, "\0"sv);
64 EXPECT_GT("\0"sv, czs);
65
66 auto str = "\0"s;
67 EXPECT_NE(zs, str);
68 EXPECT_NE(str, zs);
69 EXPECT_LT(zs, str);
70 EXPECT_GT(str, zs);
71 EXPECT_NE(czs, str);
72 EXPECT_NE(str, czs);
73 EXPECT_LT(czs, str);
74 EXPECT_GT(str, czs);
75}
76
77TEST(Zstring, Comparison)
78{
79 char mut[] = "ac";
80 auto zs = zstring(mut);
81 auto czs = const_zstring("ac");
82
83#define test(cmp, rcmp, str) \
84 EXPECT_##cmp(zs, const_zstring(str)); \
85 EXPECT_##rcmp(const_zstring(str), zs); \
86 EXPECT_##cmp(czs, const_zstring(str)); \
87 EXPECT_##rcmp(const_zstring(str), czs); \
88 EXPECT_##cmp(zs, str); \
89 EXPECT_##rcmp(str, zs); \
90 EXPECT_##cmp(czs, str); \
91 EXPECT_##rcmp(str, czs); \
92 EXPECT_##cmp(zs, str##sv); \
93 EXPECT_##rcmp(str##sv, zs); \
94 EXPECT_##cmp(czs, str##sv); \
95 EXPECT_##rcmp(str##sv, czs); \
96 EXPECT_##cmp(zs, str##s); \
97 EXPECT_##rcmp(str##s, zs); \
98 EXPECT_##cmp(czs, str##s); \
99 EXPECT_##rcmp(str##s, czs); \
100 EXPECT_##cmp(zs, reinterpret_cast<const char*>(str)); \
101 EXPECT_##rcmp(reinterpret_cast<const char*>(str), zs); \
102 EXPECT_##cmp(czs, reinterpret_cast<const char*>(str)); \
103 EXPECT_##rcmp(reinterpret_cast<const char*>(str), czs)
104 test(EQ, EQ, "ac");
105 test(GT, LT, "a");
106 test(LT, GT, "acb");
107 test(LT, GT, "ad");
108 test(GT, LT, "ab");
109#undef test
110}
111
112} // namespace stdplus