blob: f8567573777f64af5c4f8247b8c8866488c86bcd [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 III61a81bc2022-09-19 17:46:49 -070027TEST(ZstringView, Basic)
28{
29 auto s1 = zstring_view("ac");
30 auto s = "b"s;
31 auto s2 = zstring_view(s);
32 std::string_view sv = s1;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070033 char mut[] = "aa";
34 auto s3 = zstring_view(mut);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070035
William A. Kennington III37b2d5e2022-10-11 14:07:14 -070036 EXPECT_EQ(1, s1.find('c'));
37 EXPECT_EQ(0, s3.find('a'));
38 EXPECT_EQ(1, s3.rfind('a'));
39
William A. Kennington III61a81bc2022-09-19 17:46:49 -070040 EXPECT_NE(sv, s2);
41 EXPECT_NE(s1, s2);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070042 EXPECT_EQ(s3, "aa");
William A. Kennington III61a81bc2022-09-19 17:46:49 -070043
44 EXPECT_EQ("ac"_zsv, s1);
45 EXPECT_EQ("ac", s1);
46 EXPECT_EQ(s1, "ac");
47 EXPECT_EQ(s, s2);
48 EXPECT_EQ(s2, s);
49 EXPECT_EQ(s1, sv);
50 EXPECT_EQ(sv, s1);
51 EXPECT_LT("ab", s1);
52 EXPECT_GT("ad", s1);
53 EXPECT_LE(s1, "ac");
54 EXPECT_LE(s, s2);
55 EXPECT_LE(s2, s);
56 EXPECT_LE(s1, sv);
57 EXPECT_LE(sv, s1);
58
59 std::cerr << s1;
William A. Kennington III62e40d62022-09-24 22:36:31 -070060 EXPECT_EQ("bac", fmt::format("b{}", s1));
William A. Kennington IIId040f582023-07-17 03:13:58 -070061 EXPECT_EQ("bac", std::format("b{}", s1));
William A. Kennington III61a81bc2022-09-19 17:46:49 -070062
63 std::unordered_set<zstring_view> uset{s1, s2};
64 EXPECT_EQ(1, uset.count("ac"));
65 EXPECT_EQ(1, uset.count("b"));
66 std::set<zstring_view> set{s2, s2};
67 EXPECT_EQ(0, set.count("ac"));
68 EXPECT_EQ(1, set.count("b"));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070069
70 auto from_str = [&](zstring_view cs) { EXPECT_EQ(cs, "ac"); };
71 from_str("ac"s);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070072}
73
74TEST(ZstringView, ConstructError)
75{
76 auto s = "hi\0"s;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070077#ifdef NDEBUG
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070078 EXPECT_EQ("hi\0"sv, zstring_view(s));
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070079#else
William A. Kennington III61a81bc2022-09-19 17:46:49 -070080 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070081#endif
82
83 char mut1[] = "aa\0";
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070084 EXPECT_EQ("aa", zstring_view(mut1));
85#ifndef NDEBUG
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070086 char mut2[] = {'a', 'a'};
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070087 EXPECT_THROW((zstring_view(mut2)), std::invalid_argument);
88#endif
William A. Kennington III61a81bc2022-09-19 17:46:49 -070089}
90
91TEST(ZstringView, Suffix)
92{
93 auto s1 = zstring_view("ac");
94
95 EXPECT_EQ("ac", s1.suffix(0));
96 EXPECT_EQ("c", s1.suffix(1));
97 EXPECT_EQ("", s1.suffix(2));
98 EXPECT_THROW(s1.suffix(3), std::out_of_range);
99}
100
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700101TEST(ZstringView, NoTypeCoercion)
102{
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -0700103 EXPECT_EQ(""_zsv, "\0");
104 EXPECT_EQ("\0", ""_zsv);
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700105 EXPECT_NE(""_zsv, "\0"sv);
106 EXPECT_NE("\0"sv, ""_zsv);
107 EXPECT_LT(""_zsv, "\0"sv);
108 EXPECT_GT("\0"sv, ""_zsv);
109 auto str = "\0"s;
110 EXPECT_NE(""_zsv, str);
111 EXPECT_NE(str, ""_zsv);
112 EXPECT_LT(""_zsv, str);
113 EXPECT_GT(str, ""_zsv);
114}
115
William A. Kennington III61a81bc2022-09-19 17:46:49 -0700116} // namespace stdplus