blob: 40ad4d4d628583fc5d00cf7c5c41f9d4c803a10e [file] [log] [blame]
William A. Kennington III62e40d62022-09-24 22:36:31 -07001#include <fmt/format.h>
William A. Kennington III61a81bc2022-09-19 17:46:49 -07002#include <gtest/gtest.h>
3#include <iostream>
4#include <set>
5#include <stdplus/zstring_view.hpp>
6#include <string>
7#include <unordered_set>
8
9namespace stdplus
10{
11
12using std::literals::string_literals::operator""s;
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070013using std::literals::string_view_literals::operator""sv;
William A. Kennington III61a81bc2022-09-19 17:46:49 -070014using zstring_view_literals::operator""_zsv;
15
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070016TEST(ZstringView, IsString)
17{
18 EXPECT_TRUE((
19 detail::same_string<std::string, char, std::char_traits<char>>::value));
20 EXPECT_FALSE((detail::same_string<std::string_view, char,
21 std::char_traits<char>>::value));
22}
23
William A. Kennington III61a81bc2022-09-19 17:46:49 -070024TEST(ZstringView, Basic)
25{
26 auto s1 = zstring_view("ac");
27 auto s = "b"s;
28 auto s2 = zstring_view(s);
29 std::string_view sv = s1;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070030 char mut[] = "aa";
31 auto s3 = zstring_view(mut);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070032
William A. Kennington III37b2d5e2022-10-11 14:07:14 -070033 EXPECT_EQ(1, s1.find('c'));
34 EXPECT_EQ(0, s3.find('a'));
35 EXPECT_EQ(1, s3.rfind('a'));
36
William A. Kennington III61a81bc2022-09-19 17:46:49 -070037 EXPECT_NE(sv, s2);
38 EXPECT_NE(s1, s2);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070039 EXPECT_EQ(s3, "aa");
William A. Kennington III61a81bc2022-09-19 17:46:49 -070040
41 EXPECT_EQ("ac"_zsv, s1);
42 EXPECT_EQ("ac", s1);
43 EXPECT_EQ(s1, "ac");
44 EXPECT_EQ(s, s2);
45 EXPECT_EQ(s2, s);
46 EXPECT_EQ(s1, sv);
47 EXPECT_EQ(sv, s1);
48 EXPECT_LT("ab", s1);
49 EXPECT_GT("ad", s1);
50 EXPECT_LE(s1, "ac");
51 EXPECT_LE(s, s2);
52 EXPECT_LE(s2, s);
53 EXPECT_LE(s1, sv);
54 EXPECT_LE(sv, s1);
55
56 std::cerr << s1;
William A. Kennington III62e40d62022-09-24 22:36:31 -070057 EXPECT_EQ("bac", fmt::format("b{}", s1));
William A. Kennington III61a81bc2022-09-19 17:46:49 -070058
59 std::unordered_set<zstring_view> uset{s1, s2};
60 EXPECT_EQ(1, uset.count("ac"));
61 EXPECT_EQ(1, uset.count("b"));
62 std::set<zstring_view> set{s2, s2};
63 EXPECT_EQ(0, set.count("ac"));
64 EXPECT_EQ(1, set.count("b"));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070065
66 auto from_str = [&](zstring_view cs) { EXPECT_EQ(cs, "ac"); };
67 from_str("ac"s);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070068}
69
70TEST(ZstringView, ConstructError)
71{
72 auto s = "hi\0"s;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070073#ifdef NDEBUG
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070074 EXPECT_EQ("hi\0"sv, zstring_view(s));
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070075#else
William A. Kennington III61a81bc2022-09-19 17:46:49 -070076 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070077#endif
78
79 char mut1[] = "aa\0";
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070080 EXPECT_EQ("aa", zstring_view(mut1));
81#ifndef NDEBUG
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070082 char mut2[] = {'a', 'a'};
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070083 EXPECT_THROW((zstring_view(mut2)), std::invalid_argument);
84#endif
William A. Kennington III61a81bc2022-09-19 17:46:49 -070085}
86
87TEST(ZstringView, Suffix)
88{
89 auto s1 = zstring_view("ac");
90
91 EXPECT_EQ("ac", s1.suffix(0));
92 EXPECT_EQ("c", s1.suffix(1));
93 EXPECT_EQ("", s1.suffix(2));
94 EXPECT_THROW(s1.suffix(3), std::out_of_range);
95}
96
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070097TEST(ZstringView, NoTypeCoercion)
98{
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070099 EXPECT_EQ(""_zsv, "\0");
100 EXPECT_EQ("\0", ""_zsv);
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700101 EXPECT_NE(""_zsv, "\0"sv);
102 EXPECT_NE("\0"sv, ""_zsv);
103 EXPECT_LT(""_zsv, "\0"sv);
104 EXPECT_GT("\0"sv, ""_zsv);
105 auto str = "\0"s;
106 EXPECT_NE(""_zsv, str);
107 EXPECT_NE(str, ""_zsv);
108 EXPECT_LT(""_zsv, str);
109 EXPECT_GT(str, ""_zsv);
110}
111
William A. Kennington III61a81bc2022-09-19 17:46:49 -0700112} // namespace stdplus