blob: 14a1e04a05f203b6c50770cbdc307f3da983ed7a [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
33 EXPECT_NE(sv, s2);
34 EXPECT_NE(s1, s2);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070035 EXPECT_EQ(s3, "aa");
William A. Kennington III61a81bc2022-09-19 17:46:49 -070036
37 EXPECT_EQ("ac"_zsv, s1);
38 EXPECT_EQ("ac", s1);
39 EXPECT_EQ(s1, "ac");
40 EXPECT_EQ(s, s2);
41 EXPECT_EQ(s2, s);
42 EXPECT_EQ(s1, sv);
43 EXPECT_EQ(sv, s1);
44 EXPECT_LT("ab", s1);
45 EXPECT_GT("ad", s1);
46 EXPECT_LE(s1, "ac");
47 EXPECT_LE(s, s2);
48 EXPECT_LE(s2, s);
49 EXPECT_LE(s1, sv);
50 EXPECT_LE(sv, s1);
51
52 std::cerr << s1;
William A. Kennington III62e40d62022-09-24 22:36:31 -070053 EXPECT_EQ("bac", fmt::format("b{}", s1));
William A. Kennington III61a81bc2022-09-19 17:46:49 -070054
55 std::unordered_set<zstring_view> uset{s1, s2};
56 EXPECT_EQ(1, uset.count("ac"));
57 EXPECT_EQ(1, uset.count("b"));
58 std::set<zstring_view> set{s2, s2};
59 EXPECT_EQ(0, set.count("ac"));
60 EXPECT_EQ(1, set.count("b"));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070061
62 auto from_str = [&](zstring_view cs) { EXPECT_EQ(cs, "ac"); };
63 from_str("ac"s);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070064}
65
66TEST(ZstringView, ConstructError)
67{
68 auto s = "hi\0"s;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070069#ifdef NDEBUG
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070070 EXPECT_EQ("hi\0"sv, zstring_view(s));
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070071#else
William A. Kennington III61a81bc2022-09-19 17:46:49 -070072 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070073#endif
74
75 char mut1[] = "aa\0";
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070076 EXPECT_EQ("aa", zstring_view(mut1));
77#ifndef NDEBUG
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070078 char mut2[] = {'a', 'a'};
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070079 EXPECT_THROW((zstring_view(mut2)), std::invalid_argument);
80#endif
William A. Kennington III61a81bc2022-09-19 17:46:49 -070081}
82
83TEST(ZstringView, Suffix)
84{
85 auto s1 = zstring_view("ac");
86
87 EXPECT_EQ("ac", s1.suffix(0));
88 EXPECT_EQ("c", s1.suffix(1));
89 EXPECT_EQ("", s1.suffix(2));
90 EXPECT_THROW(s1.suffix(3), std::out_of_range);
91}
92
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070093TEST(ZstringView, NoTypeCoercion)
94{
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070095 EXPECT_EQ(""_zsv, "\0");
96 EXPECT_EQ("\0", ""_zsv);
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070097 EXPECT_NE(""_zsv, "\0"sv);
98 EXPECT_NE("\0"sv, ""_zsv);
99 EXPECT_LT(""_zsv, "\0"sv);
100 EXPECT_GT("\0"sv, ""_zsv);
101 auto str = "\0"s;
102 EXPECT_NE(""_zsv, str);
103 EXPECT_NE(str, ""_zsv);
104 EXPECT_LT(""_zsv, str);
105 EXPECT_GT(str, ""_zsv);
106}
107
William A. Kennington III61a81bc2022-09-19 17:46:49 -0700108} // namespace stdplus