blob: 69b3c9fae98d24c40ef069da2535192de22199ba [file] [log] [blame]
William A. Kennington III62e40d62022-09-24 22:36:31 -07001#include <fmt/format.h>
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -07002#include <gtest/gtest.h>
3#include <iostream>
4#include <stdplus/zstring.hpp>
5#include <string>
6#include <string_view>
7
8using std::literals::string_literals::operator""s;
9using std::literals::string_view_literals::operator""sv;
10
11namespace stdplus
12{
13
14TEST(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 IIIf2ddf512022-09-20 15:36:44 -070027 EXPECT_EQ("b", const_zstring("b\0"));
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070028 char as[] = "c";
29 EXPECT_EQ("c", zstring(as));
30 EXPECT_EQ("c", const_zstring(as));
31
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070032 auto str2 = "d"s;
33 auto zs = zstring(str2);
34 EXPECT_EQ(str2, zs);
35
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070036 std::cerr << const_zstring(as);
William A. Kennington III62e40d62022-09-24 22:36:31 -070037 EXPECT_EQ("dd", fmt::format("d{}", zs));
38 EXPECT_EQ("dc", fmt::format("d{}", const_zstring(as)));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070039
40 auto from_str = [&](const_zstring cs) { EXPECT_EQ(cs, "ac"); };
41 from_str("ac"s);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070042}
43
44TEST(Zstring, NoTypeCoercion)
45{
46 char empty[] = "";
47 auto zs = zstring(empty);
48 auto czs = const_zstring("");
49
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070050 EXPECT_EQ(zs, "\0");
51 EXPECT_EQ("\0", zs);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070052 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 IIIaaa8e0d2022-09-24 22:36:31 -070056 EXPECT_EQ(czs, "\0");
57 EXPECT_EQ("\0", czs);
William A. Kennington IIIf2ddf512022-09-20 15:36:44 -070058 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
74TEST(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