blob: 70f4c8895157abebcb3824e6393324c6facf8839 [file] [log] [blame]
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -07001#include <gtest/gtest.h>
2#include <iostream>
3#include <stdplus/zstring.hpp>
4#include <string>
5#include <string_view>
6
7using std::literals::string_literals::operator""s;
8using std::literals::string_view_literals::operator""sv;
9
10namespace stdplus
11{
12
13TEST(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
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 IIIb6032a12022-09-23 18:27:05 -070040
41 auto from_str = [&](const_zstring cs) { EXPECT_EQ(cs, "ac"); };
42 from_str("ac"s);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070043}
44
45TEST(Zstring, NoTypeCoercion)
46{
47 char empty[] = "";
48 auto zs = zstring(empty);
49 auto czs = const_zstring("");
50
51 EXPECT_NE(zs, "\0");
52 EXPECT_NE("\0", zs);
53 EXPECT_NE(zs, "\0"sv);
54 EXPECT_NE("\0"sv, zs);
55 EXPECT_LT(zs, "\0"sv);
56 EXPECT_GT("\0"sv, zs);
57 EXPECT_NE(czs, "\0");
58 EXPECT_NE("\0", czs);
59 EXPECT_NE(czs, "\0"sv);
60 EXPECT_NE("\0"sv, czs);
61 EXPECT_LT(czs, "\0"sv);
62 EXPECT_GT("\0"sv, czs);
63
64 auto str = "\0"s;
65 EXPECT_NE(zs, str);
66 EXPECT_NE(str, zs);
67 EXPECT_LT(zs, str);
68 EXPECT_GT(str, zs);
69 EXPECT_NE(czs, str);
70 EXPECT_NE(str, czs);
71 EXPECT_LT(czs, str);
72 EXPECT_GT(str, czs);
73}
74
75TEST(Zstring, Comparison)
76{
77 char mut[] = "ac";
78 auto zs = zstring(mut);
79 auto czs = const_zstring("ac");
80
81#define test(cmp, rcmp, str) \
82 EXPECT_##cmp(zs, const_zstring(str)); \
83 EXPECT_##rcmp(const_zstring(str), zs); \
84 EXPECT_##cmp(czs, const_zstring(str)); \
85 EXPECT_##rcmp(const_zstring(str), czs); \
86 EXPECT_##cmp(zs, str); \
87 EXPECT_##rcmp(str, zs); \
88 EXPECT_##cmp(czs, str); \
89 EXPECT_##rcmp(str, czs); \
90 EXPECT_##cmp(zs, str##sv); \
91 EXPECT_##rcmp(str##sv, zs); \
92 EXPECT_##cmp(czs, str##sv); \
93 EXPECT_##rcmp(str##sv, czs); \
94 EXPECT_##cmp(zs, str##s); \
95 EXPECT_##rcmp(str##s, zs); \
96 EXPECT_##cmp(czs, str##s); \
97 EXPECT_##rcmp(str##s, czs); \
98 EXPECT_##cmp(zs, reinterpret_cast<const char*>(str)); \
99 EXPECT_##rcmp(reinterpret_cast<const char*>(str), zs); \
100 EXPECT_##cmp(czs, reinterpret_cast<const char*>(str)); \
101 EXPECT_##rcmp(reinterpret_cast<const char*>(str), czs)
102 test(EQ, EQ, "ac");
103 test(GT, LT, "a");
104 test(LT, GT, "acb");
105 test(LT, GT, "ad");
106 test(GT, LT, "ab");
107#undef test
108}
109
110} // namespace stdplus