blob: 25ce5b053e30699fc9dea98ecb69980e2848b277 [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
William A. Kennington IIId040f582023-07-17 03:13:58 -07005#include <format>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05006#include <iostream>
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -07007#include <string>
8#include <string_view>
9
Patrick Williamsd1984dd2023-05-10 16:12:44 -050010#include <gtest/gtest.h>
11
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070012using std::literals::string_literals::operator""s;
13using std::literals::string_view_literals::operator""sv;
14
15namespace stdplus
16{
17
18TEST(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 IIIf2ddf512022-09-20 15:36:44 -070031 EXPECT_EQ("b", const_zstring("b\0"));
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070032 char as[] = "c";
33 EXPECT_EQ("c", zstring(as));
34 EXPECT_EQ("c", const_zstring(as));
35
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070036 auto str2 = "d"s;
37 auto zs = zstring(str2);
38 EXPECT_EQ(str2, zs);
39
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070040 std::cerr << const_zstring(as);
William A. Kennington III62e40d62022-09-24 22:36:31 -070041 EXPECT_EQ("dd", fmt::format("d{}", zs));
42 EXPECT_EQ("dc", fmt::format("d{}", const_zstring(as)));
William A. Kennington IIId040f582023-07-17 03:13:58 -070043 EXPECT_EQ("dd", std::format("d{}", zs));
44 EXPECT_EQ("dc", std::format("d{}", const_zstring(as)));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070045
46 auto from_str = [&](const_zstring cs) { EXPECT_EQ(cs, "ac"); };
47 from_str("ac"s);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070048}
49
50TEST(Zstring, NoTypeCoercion)
51{
52 char empty[] = "";
53 auto zs = zstring(empty);
54 auto czs = const_zstring("");
55
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070056 EXPECT_EQ(zs, "\0");
57 EXPECT_EQ("\0", zs);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070058 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 IIIaaa8e0d2022-09-24 22:36:31 -070062 EXPECT_EQ(czs, "\0");
63 EXPECT_EQ("\0", czs);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070064 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
80TEST(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