blob: 34046afd14cb4ea7460fc2f8fc33701139d4b662 [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
3#include <stdplus/zstring_view.hpp>
4
William A. Kennington IIId040f582023-07-17 03:13:58 -07005#include <format>
William A. Kennington III61a81bc2022-09-19 17:46:49 -07006#include <iostream>
7#include <set>
William A. Kennington III61a81bc2022-09-19 17:46:49 -07008#include <string>
9#include <unordered_set>
10
Patrick Williamsd1984dd2023-05-10 16:12:44 -050011#include <gtest/gtest.h>
12
William A. Kennington III61a81bc2022-09-19 17:46:49 -070013namespace stdplus
14{
15
16using std::literals::string_literals::operator""s;
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070017using std::literals::string_view_literals::operator""sv;
William A. Kennington III61a81bc2022-09-19 17:46:49 -070018
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070019TEST(ZstringView, IsString)
20{
21 EXPECT_TRUE((
22 detail::same_string<std::string, char, std::char_traits<char>>::value));
23 EXPECT_FALSE((detail::same_string<std::string_view, char,
24 std::char_traits<char>>::value));
25}
26
William A. Kennington IIId70a4512023-12-07 19:22:32 -080027constexpr auto ezsv = ""_zsv;
28constexpr auto aczsv = "ac"_zsv;
29
William A. Kennington III61a81bc2022-09-19 17:46:49 -070030TEST(ZstringView, Basic)
31{
32 auto s1 = zstring_view("ac");
33 auto s = "b"s;
34 auto s2 = zstring_view(s);
35 std::string_view sv = s1;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070036 char mut[] = "aa";
37 auto s3 = zstring_view(mut);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070038
William A. Kennington III37b2d5e2022-10-11 14:07:14 -070039 EXPECT_EQ(1, s1.find('c'));
40 EXPECT_EQ(0, s3.find('a'));
41 EXPECT_EQ(1, s3.rfind('a'));
42
William A. Kennington III61a81bc2022-09-19 17:46:49 -070043 EXPECT_NE(sv, s2);
44 EXPECT_NE(s1, s2);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070045 EXPECT_EQ(s3, "aa");
William A. Kennington III61a81bc2022-09-19 17:46:49 -070046
William A. Kennington IIId70a4512023-12-07 19:22:32 -080047 EXPECT_EQ(aczsv, s1);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070048 EXPECT_EQ("ac", s1);
49 EXPECT_EQ(s1, "ac");
50 EXPECT_EQ(s, s2);
51 EXPECT_EQ(s2, s);
52 EXPECT_EQ(s1, sv);
53 EXPECT_EQ(sv, s1);
54 EXPECT_LT("ab", s1);
55 EXPECT_GT("ad", s1);
56 EXPECT_LE(s1, "ac");
57 EXPECT_LE(s, s2);
58 EXPECT_LE(s2, s);
59 EXPECT_LE(s1, sv);
60 EXPECT_LE(sv, s1);
61
62 std::cerr << s1;
William A. Kennington III62e40d62022-09-24 22:36:31 -070063 EXPECT_EQ("bac", fmt::format("b{}", s1));
William A. Kennington IIId040f582023-07-17 03:13:58 -070064 EXPECT_EQ("bac", std::format("b{}", s1));
William A. Kennington III61a81bc2022-09-19 17:46:49 -070065
66 std::unordered_set<zstring_view> uset{s1, s2};
67 EXPECT_EQ(1, uset.count("ac"));
68 EXPECT_EQ(1, uset.count("b"));
69 std::set<zstring_view> set{s2, s2};
70 EXPECT_EQ(0, set.count("ac"));
71 EXPECT_EQ(1, set.count("b"));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070072
73 auto from_str = [&](zstring_view cs) { EXPECT_EQ(cs, "ac"); };
74 from_str("ac"s);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070075}
76
77TEST(ZstringView, ConstructError)
78{
79 auto s = "hi\0"s;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070080#ifdef NDEBUG
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070081 EXPECT_EQ("hi\0"sv, zstring_view(s));
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070082#else
William A. Kennington III61a81bc2022-09-19 17:46:49 -070083 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070084#endif
85
86 char mut1[] = "aa\0";
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070087 EXPECT_EQ("aa", zstring_view(mut1));
88#ifndef NDEBUG
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070089 char mut2[] = {'a', 'a'};
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070090 EXPECT_THROW((zstring_view(mut2)), std::invalid_argument);
91#endif
William A. Kennington III61a81bc2022-09-19 17:46:49 -070092}
93
94TEST(ZstringView, Suffix)
95{
96 auto s1 = zstring_view("ac");
97
98 EXPECT_EQ("ac", s1.suffix(0));
99 EXPECT_EQ("c", s1.suffix(1));
100 EXPECT_EQ("", s1.suffix(2));
101 EXPECT_THROW(s1.suffix(3), std::out_of_range);
102}
103
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700104TEST(ZstringView, NoTypeCoercion)
105{
William A. Kennington IIId70a4512023-12-07 19:22:32 -0800106 EXPECT_EQ(ezsv, "\0");
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -0700107 EXPECT_EQ("\0", ""_zsv);
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700108 EXPECT_NE(""_zsv, "\0"sv);
109 EXPECT_NE("\0"sv, ""_zsv);
110 EXPECT_LT(""_zsv, "\0"sv);
111 EXPECT_GT("\0"sv, ""_zsv);
112 auto str = "\0"s;
113 EXPECT_NE(""_zsv, str);
114 EXPECT_NE(str, ""_zsv);
115 EXPECT_LT(""_zsv, str);
116 EXPECT_GT(str, ""_zsv);
117}
118
William A. Kennington III61a81bc2022-09-19 17:46:49 -0700119} // namespace stdplus