blob: 0ef7fb73eba5298d9426029227a8e53acd2d37c7 [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 III61a81bc2022-09-19 17:46:49 -07005#include <iostream>
6#include <set>
William A. Kennington III61a81bc2022-09-19 17:46:49 -07007#include <string>
8#include <unordered_set>
9
Patrick Williamsd1984dd2023-05-10 16:12:44 -050010#include <gtest/gtest.h>
11
William A. Kennington III61a81bc2022-09-19 17:46:49 -070012namespace stdplus
13{
14
15using std::literals::string_literals::operator""s;
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070016using std::literals::string_view_literals::operator""sv;
William A. Kennington III61a81bc2022-09-19 17:46:49 -070017
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070018TEST(ZstringView, IsString)
19{
20 EXPECT_TRUE((
21 detail::same_string<std::string, char, std::char_traits<char>>::value));
22 EXPECT_FALSE((detail::same_string<std::string_view, char,
23 std::char_traits<char>>::value));
24}
25
William A. Kennington III61a81bc2022-09-19 17:46:49 -070026TEST(ZstringView, Basic)
27{
28 auto s1 = zstring_view("ac");
29 auto s = "b"s;
30 auto s2 = zstring_view(s);
31 std::string_view sv = s1;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070032 char mut[] = "aa";
33 auto s3 = zstring_view(mut);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070034
William A. Kennington III37b2d5e2022-10-11 14:07:14 -070035 EXPECT_EQ(1, s1.find('c'));
36 EXPECT_EQ(0, s3.find('a'));
37 EXPECT_EQ(1, s3.rfind('a'));
38
William A. Kennington III61a81bc2022-09-19 17:46:49 -070039 EXPECT_NE(sv, s2);
40 EXPECT_NE(s1, s2);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070041 EXPECT_EQ(s3, "aa");
William A. Kennington III61a81bc2022-09-19 17:46:49 -070042
43 EXPECT_EQ("ac"_zsv, s1);
44 EXPECT_EQ("ac", s1);
45 EXPECT_EQ(s1, "ac");
46 EXPECT_EQ(s, s2);
47 EXPECT_EQ(s2, s);
48 EXPECT_EQ(s1, sv);
49 EXPECT_EQ(sv, s1);
50 EXPECT_LT("ab", s1);
51 EXPECT_GT("ad", s1);
52 EXPECT_LE(s1, "ac");
53 EXPECT_LE(s, s2);
54 EXPECT_LE(s2, s);
55 EXPECT_LE(s1, sv);
56 EXPECT_LE(sv, s1);
57
58 std::cerr << s1;
William A. Kennington III62e40d62022-09-24 22:36:31 -070059 EXPECT_EQ("bac", fmt::format("b{}", s1));
William A. Kennington III61a81bc2022-09-19 17:46:49 -070060
61 std::unordered_set<zstring_view> uset{s1, s2};
62 EXPECT_EQ(1, uset.count("ac"));
63 EXPECT_EQ(1, uset.count("b"));
64 std::set<zstring_view> set{s2, s2};
65 EXPECT_EQ(0, set.count("ac"));
66 EXPECT_EQ(1, set.count("b"));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070067
68 auto from_str = [&](zstring_view cs) { EXPECT_EQ(cs, "ac"); };
69 from_str("ac"s);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070070}
71
72TEST(ZstringView, ConstructError)
73{
74 auto s = "hi\0"s;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070075#ifdef NDEBUG
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070076 EXPECT_EQ("hi\0"sv, zstring_view(s));
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070077#else
William A. Kennington III61a81bc2022-09-19 17:46:49 -070078 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070079#endif
80
81 char mut1[] = "aa\0";
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070082 EXPECT_EQ("aa", zstring_view(mut1));
83#ifndef NDEBUG
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070084 char mut2[] = {'a', 'a'};
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070085 EXPECT_THROW((zstring_view(mut2)), std::invalid_argument);
86#endif
William A. Kennington III61a81bc2022-09-19 17:46:49 -070087}
88
89TEST(ZstringView, Suffix)
90{
91 auto s1 = zstring_view("ac");
92
93 EXPECT_EQ("ac", s1.suffix(0));
94 EXPECT_EQ("c", s1.suffix(1));
95 EXPECT_EQ("", s1.suffix(2));
96 EXPECT_THROW(s1.suffix(3), std::out_of_range);
97}
98
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070099TEST(ZstringView, NoTypeCoercion)
100{
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -0700101 EXPECT_EQ(""_zsv, "\0");
102 EXPECT_EQ("\0", ""_zsv);
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700103 EXPECT_NE(""_zsv, "\0"sv);
104 EXPECT_NE("\0"sv, ""_zsv);
105 EXPECT_LT(""_zsv, "\0"sv);
106 EXPECT_GT("\0"sv, ""_zsv);
107 auto str = "\0"s;
108 EXPECT_NE(""_zsv, str);
109 EXPECT_NE(str, ""_zsv);
110 EXPECT_LT(""_zsv, str);
111 EXPECT_GT(str, ""_zsv);
112}
113
William A. Kennington III61a81bc2022-09-19 17:46:49 -0700114} // namespace stdplus